1

Using std::regex_match on a big enough target sequence is causing a segmentation fault here. I guess its due to stackoverflow. I noticed, that most often the segmentation fault is due to a "stupid" regex pattern in combination with large enough data.

Is there any way to prevent the code from crashing? So i would like to warn the user when std::regex_match is failing due to stackoverflow and then continue normally. Up to now I can't avoid a fatal SIGSEGV termination exactly due to this std::regex_match failure.

I tried already the error_stack constant, but it seems like it is not working (std::regex_match is not throwing?!).

Following an illustrative example for std::regex_match SIGSEGV due to a too large target sequence

#include <iostream>
#include <regex>

int main()
{
    std::regex r("^.*$");
    std::smatch m;
    std::string str;
    for(size_t i = 0; i < 2e4; ++i)
        str += char(rand()%(127-32)+32);
    std::regex_match(str,m,r);
    return 0;
}

Thank you in advance!

  • 4
    Check [С++ regex segfault on long sequences](https://stackoverflow.com/questions/36304204/%D0%A1-regex-segfault-on-long-sequences) – Wiktor Stribiżew Feb 10 '18 at 09:59
  • I already did, thank you! So i think its clear it is a bug. So the cases where this bug is happening are anyway not accepted by my programm, in the sense that i would prompt him for different pattern etc. . But the problem is that i cant ask him for different pattern, because of the immediate termination due to this SIGSEGV. I hope you somehow understand what i mean :D. So i would like to prevent the crash if possible. –  Feb 10 '18 at 10:13
  • @Wiktor Stribiżew Is there a reason why regex_match is not throwing anything? I would at least expect to catch a regex_error of type error_stack, as it is described in http://en.cppreference.com/w/cpp/regex/error_type... –  Feb 10 '18 at 13:19

0 Answers0