stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Travis Vitek" <tvi...@quovadx.com>
Subject RE: [PATCH] Add overflow checking to basic_string append and push_back
Date Thu, 20 Sep 2007 23:57:57 GMT

Stupid outlook.
 
>
>[21.3.5.2 p4] Effects: Determines the effective length rlen of the
>string to append as the smaller of n and str.size() - pos. The function
>then throws length_error if size() >= npos - rlen.
>
>The append that I'm invoking is described to behave as if it had called
>that function. The attached test case shows that we are not following
>through on that requirement.
>

#include <string>
#include <stdexcept>

template <class T>
struct Xallocator: std::allocator<T>
{
    //typedef typename std::allocator<T>::size_type size_type;
    typedef unsigned char size_type;

    Xallocator (): std::allocator<T>() { } 
    Xallocator (const Xallocator &rhs): std::allocator<T>(rhs) { }
    template <class U>
    Xallocator (const Xallocator<U> &rhs): std::allocator<T>(rhs) { }

    template <class U> 
    struct rebind { typedef Xallocator<U> other; };

    size_type max_size () const { return 255; }
};

typedef std::basic_string<char, std::char_traits<char>, Xallocator<char>
>
String;


#include <stdio.h>
#include <assert.h>

int main()
{
    bool throw_required = false;
    bool throw_happened = false;

    try {
        String a (240, 'a');
        String b ( 20, 'b');

        const String::size_type rlen = b.size();

        if (a.size () >= String::npos - rlen)
            throw_required = true;

        // this throws correctly
        //a.append (b, 0, String::npos);

        // this does not
        a.append (b.c_str (), b.size ());

        // yet they are supposed to be equivalent
    }
    catch (const std::length_error&) {
        throw_happened = true;
    }
    catch (...) {
    }

    if (throw_required) {
        assert (throw_happened);
    }

    return 0;
}

Mime
View raw message