For starters, I'm using primarily GCC 4.8 on Ubuntu 14.04. Everything standard.
OK, so I looked around SO and it seems that GCC's support for regexes is somewhat faulty. However, the problem I as having is so basic, that it makes me wonder very much wonder if I am the one doing something that is not permitted, or if the C++ library might be even more compromised than just the regex part.
I had two simple regexes that would be used in different parts of the program, so I defined them in the global namespace as follows.
regex DATAFILE_PATTERN("^(.+?)(|\\.(db|[ng]?dbm|dir|pag|newhash))$");
regex TEMP_FILES("^.*(tmp|ypxfr_map).*$");
The code compiled nicely, without a single warning. However, when I ran it, it failed with std::regex_error
(malformed regex) before even reaching main()
.
That exception was outrageous, since that was nothing wrong with the regexes, as you can see. Even then, I simplified them a little to try to find if how it could be malformed. The result was that, depending on the expression -- all for the valid! --, I would sometimes get the std::regex_error
, and sometimes just a segmentation fault, without any language-level exception.
Then I moved the regexes from the namespace scope to inside the functions where they were used, and now they worked nicely.
Is there any explanation for that better just "it's a GCC bug"? I mean something like a stackframe limit during initialization of globals and/or static data that might have been reached due to regex's complicated internals. If so, could it be overcome with some tunable?
I tried the same code with clang++ 3.4 with the same libstdc++, and the results were similar, so it makes me think that the compiler might not be the one to blame (unless clang uses tunables similar to the ones in GCC). I tried to install LLVM's libc++, but so far I could get it to work (lots of undefined symbols at link time, that I could not yet figure why -- any clues would also be appreciated).
I wonder if other compilers, such as MSVS or a properly configured Clang, would have similar issues.