1

I am getting a "Segmentation fault" when I try to use some regular expressions. In this example, the first regular expression worked fine, but the second produced a segmentation fault:

#include <regex>
#include <iostream>

using namespace std;

void out(const string& s, const string& d,bool b) { cout << s << ", " << d << (b ? "=found" : "=not found") << endl; }

int
main()
{
    const auto exp1 = string{"<.*>.*</.*>"};
    const auto reg1 = regex{exp1};
    const auto data1 = string{"<tag>value</tag>"};
    const auto found1 = regex_match(data1, reg1);
    out(exp1, data1, found1);

    const auto exp2 = string{"<(.*)>.*</\\1>"};

    cout << "About to create the regex!" << endl;
    const auto reg2 = regex{exp2};
    cout << "done." << endl;

    const auto data2 = string{"<tag>value</tag>"};
    const auto found2 = regex_match(data2, reg2);
    out(exp2, data2, found2);

    cout << "All done!" << endl;
}

I compile it like this:

g++ -O0 -g -Wall -Wpedantic -std=c++11    try3.cpp   -o try3

And when I run it I get this:

$ ./try3
<.*>.*</.*>, <tag>value</tag>=found
About to create the regex!
Segmentation fault

I am running on CentOS release 5.11 with libstdc++-4.1.2-55.el5

Scott Nelson
  • 568
  • 2
  • 17
  • That is a problem with the compiled regex library, not the regex itself. You can tell if it's this if you put a try/catch around that line, but it is never caught. –  Feb 10 '17 at 18:19
  • @sln: true, I tried earlier with a try statement and it does not catch anything. Do you (or others) have a work-around? – Scott Nelson Feb 10 '17 at 19:21
  • Not for sure, but google the problem, it's probably either the lib version or what flags were set when compiled. Somebody should pass by that knows for sure. –  Feb 10 '17 at 19:25
  • Do you also have the same issue with `<[^>]*>[^<]*[^>]*>` or `"<([^<]+)>[\\s\\S]*?\\1>"`? If yes, the problem is with the outdated regex library. – Wiktor Stribiżew Feb 10 '17 at 20:32

1 Answers1

1

This is because GCC 4.1 was starting to implement regex but it was not ready for use until later.

See Is gcc 4.8 or earlier buggy about regular expressions?

Community
  • 1
  • 1
Scott Nelson
  • 568
  • 2
  • 17