http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49021
Summary: [4.6 regression] BOOST_FOREACH over vector segfaults at runtime Product: gcc Version: 4.6.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: mi...@it.uu.se The following test case, which I'll also attach, segfaults at runtime when compiled by gcc-4.6-20110513 on x86_64-linux or i686-linux. When compiled by gcc-4.5-20110512 it works as expected. > cat comm-bug-test.cc #include <boost/foreach.hpp> #include <vector> #include <stdio.h> std::vector<int> v; std::vector<int> x; const inline std::vector<int *> getv() { const size_t num_accesses(x.size()); std::vector<int *> ret(num_accesses); for (size_t i = 0; i < num_accesses; i++) ret[i] = &v[x[i]]; return ret; } int main(int argc, char **argv) { x.push_back(0); x.push_back(1); x.push_back(2); x.push_back(3); x.push_back(4); x.push_back(5); v.push_back(11); v.push_back(12); v.push_back(13); v.push_back(14); v.push_back(15); v.push_back(16); BOOST_FOREACH(int *w, getv()) printf("%d\n", *w); return 0; } > /mnt/misc/install45/bin/g++ -O2 -Wall comm-bug-test.cc; ./a.out 11 12 13 14 15 16 > /mnt/misc/install46/bin/g++ -O2 -Wall comm-bug-test.cc; ./a.out Segmentation fault > /mnt/misc/install46/bin/g++ -v Using built-in specs. COLLECT_GCC=/mnt/misc/install46/bin/g++ COLLECT_LTO_WRAPPER=/mnt/misc/install46/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /mnt/misc/gcc-4.6-20110513/configure --prefix=/mnt/misc/install46 --with-gmp=/home/mikpe/pkgs/linux-x86_64/gmp-4.3.2 --with-mpfr=/home/mikpe/pkgs/linux-x86_64/mpfr-2.4.2 --with-mpc=/home/mikpe/pkgs/linux-x86_64/mpc-0.8.2 --disable-plugin --disable-lto --disable-nls --disable-libmudflap --enable-threads=posix --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran,java,ada Thread model: posix gcc version 4.6.1 20110513 (prerelease) (GCC) Initially found by a colleague when testing our code on Fedora 15 with Red Hat's gcc-4.6.0 (20110428) and boost 1.46. I then confirmed the bug on Fedora 13 with a self-compiled gcc-4.6-20110513 and boost-1.41. -m32 or -m64 makes no difference.