https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68175
Bug ID: 68175 Summary: g++ 5.2.1 produces broken executables with devirtualization enabled Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: ipa Assignee: unassigned at gcc dot gnu.org Reporter: bnagaev at gmail dot com Target Milestone: --- The following code compiles to a broken executable: ```cpp #include <fstream> const int BUFFER_SIZE = 4096; char buffer_[BUFFER_SIZE]; struct BufferedIfstream : public std::ifstream { BufferedIfstream() { rdbuf()->pubsetbuf(buffer_, BUFFER_SIZE); } }; int main() { BufferedIfstream fff; return 0; } ``` Compilation command: $ g++-5 bug.cpp -O2 -o bug.exe Run it: $ ./bug.exe Segmentation fault Options `-Wall -Wextra` produce no warnings. Options `-fno-strict-aliasing -fwrapv` change nothing. Without `-O2` (or `-O3`) the bug disappears. With `-fno-devirtualize` the bug disappears. With `-O1` the bug disappears but with `-O1 -fdevirtualize` appears again. Without a class the bug disappears. The following code works correctly: ```cpp #include <fstream> const int BUFFER_SIZE = 4096; char buffer_[BUFFER_SIZE]; int main() { std::ifstream fff; fff.rdbuf()->pubsetbuf(buffer_, BUFFER_SIZE); return 0; } ``` I have reproduced this bug in gcc 5.1.0, 5.2.0 (MXE build, MinGW-w64 [1]) and in gcc 5.2.1 (Debian Stretch, amd64 Linux, 5.2.1-22). I believe this bug was introduced in gcc 5, when devirtualization was refactored [2]. The issue seems to be related to the mailing thread about -O3 (MXE): stack smashing on i686 with -O3 since da82d07dbe [3]. [1] https://github.com/mxe/mxe/issues/964 [2] https://gcc.gnu.org/gcc-5/changes.html [3] http://lists.nongnu.org/archive/html/mingw-cross-env-list/2015-10/msg00020.html