10

I'm in the process of converting some older Boost regex code to C++11, and I stumbled upon an issue with one of my test cases. Here is a scenario which causes a stack overflow exception using std::regex, but worked fine with boost::regex. I have not changed the regular expression pattern, and have verified the pattern is what I want. It seems this particular string input fragment is causing the stack overflow. Using VS2012, x64 debug build:

std::regex regx( "(^|\\})(([^\\{:])+:)+([^\\{]*\\{)" );

    const std::string testinput = " COLOR: #000; BACKGROUND-COLOR: #FFF; FONT-FAMILY: VERDANA, ARIAL, HELVETICA, SANS-SERIF; BACKGROUND:URL(URL(___FOO___)); BACKGROUND-2:URL(URL(___FOO___)); BORDER: 0 0 0 0; BORDER-2: 0 0 0; BORDER-3: 0 0; BORDER-4: 0PX; BORDER-5: 0.6; FILTER:PROGID:DXIMAGETRANSFORM.MICROSOFT.ALPHA(OPACITY=100); } ";
    std::smatch what;
    // this next line causes a stack overflow
    std::regex_search( testinput.cbegin(), testinput.cend(), what, regx );  

Looking at the call stack after the exception, there seems to be some type of infinite recursion going on in the regex implementation. I don't currently have GCC to test this with. What am I doing wrong?

Update: After the suggestions below, I pasted this code into a console app, VS 2012 x64 debug and I get the stack overflow. If I change it to x64 release, or Win32 debug or release it runs fine. Huh??? Do i need to reinstall VS and/or the platform SDK? I'm on Win7 x64.

Update #2: Somewhat related post: Why does std::regex_iterator cause a stack overflow with this data? I suppose if I rewrite my regex, it might help. I'm still not sure why the bitness matters though. And why it works for others, but not for me on my system. Sigh.

7
  • 2
    I'm not reproducing this crash in my installation of VS 2012 with a 64-bit debug build. A direct copy of the code above, inside a main function, simply returns false from the call to regex_search - no match. Mar 29, 2013 at 4:05
  • 1
    Works fine on mine (32-bit) as well.
    – W.B.
    Mar 29, 2013 at 8:56
  • @DanNissenbaum, thanks for confirming. I updated my question; seems to only happen in x64 debug. x64 release or the Win32 builds run fine. I don't get it?
    – Tom
    Mar 29, 2013 at 17:31
  • Perhaps a clean solution (i.e., full rebuild)? Mar 29, 2013 at 20:08
  • 1
    I'm also unable to reproduce this in my installation of VS 2012. Looks to be specific to your app, or maybe to your some configuration settings within your project?
    – Kyle C
    Mar 29, 2013 at 21:32

1 Answer 1

2

I've been reproduced this with x64 debug build, and I belive this is a real stack overflow.

When you change your stack size to 10MB or so (linker command line option /STACK:"10000000"), it will work fine.

3
  • 1
    Thanks, I agree this is a valid workaround. Makes me wonder, though, if this is the actual solution, or just a way to cover up defects in <regex>, especially since boost regex runs fine in the same app. Since release mode apparently reuses the stack, it shouldn't be a problem I suppose...thanks again
    – Tom
    Apr 2, 2013 at 17:25
  • 1
    I think this is a some kind of defect. I've not investigate the <regex> code, but even x86/Release very simple regex pattern will cause stack overflow. Please check this: using namespace std; regex r("(x)+"); string t(512, 'x'); regex_search(t, r);
    – t-mat
    Apr 2, 2013 at 19:15
  • Yes, I can reproduce the same problem in x86 and x64 debug and release modes. Interesting...
    – Tom
    Apr 2, 2013 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.