[Bug c++/49021] [4.6 regression] BOOST_FOREACH over vector segfaults at runtime

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue May 17 17:57:00 GMT 2011


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49021

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.05.17 17:24:37
     Ever Confirmed|0                           |1

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-05-17 17:24:37 UTC ---
slightly reduced, but boost/foreach.hpp still includes thousands of lines

in this version vector<T> zeros its data member on destruction, so using it
after destruction is obvious

#include <boost/foreach.hpp>

template<typename T>
struct vector
{
    typedef const T* const_iterator;

    ~vector() { data = T(); }

    T data;

    const_iterator begin() const { return &data; }
    const_iterator end() const { return &data + 1; }
};

vector<int> v = { 1 };

const vector<int *>
getv()
{
    vector<int *> ret = { &v.data };
    return ret;
}

int
main(int argc, char **argv)
{
    BOOST_FOREACH(int *w, getv())
        __builtin_printf("%d\n", *w);

    return 0;
}



More information about the Gcc-bugs mailing list