gcc std::regex with -fpack-struct seg faults

Viewed 644

Consider the following simple c++ program

#include <iostream>
#include <regex>
int main(int argc, char * argv[])
{
    std::regex foobar( "[A]+");

    return 0;
}

When compiling with -fpack-struct=1 it seg faults

g++-5 -std=gnu++14 ./fpack_regex.cpp -fpack-struct=1 -o a.out && a.out
Segmentation fault (core dumped)

While

g++-5 -std=gnu++14 ./fpack_regex.cpp -o a.out && a.out

works just fine.

Any clue why the pack-struct=1 option might cause this failure?

1 Answers

The switch -fpack-struct can be very dangerous, eg. see gcc documentation warning about it:

The main problem I see is that your code is not binary compatible with standard library (it is usually not compiled with structs packed), so calls (with transfering structs) to it may fail (as they actually do).

It is recommended not to pack all structs with this switch, but if you need packing structure, pack only those you need. I also read that recompiling libstd and/or libs you use with the same fpack-struct could help, but that is a risky option anyway.

Some information is also here (an old gcc bug concerning fpack-struct), it is outdated, but may be useful: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14173

Related