Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

gcc

c++11

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?

like image 221
JeffCharter Avatar asked Sep 29 '17 22:09

JeffCharter


1 Answers

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

  • https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
  • https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html

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

like image 76
pe3k Avatar answered Nov 05 '22 12:11

pe3k