Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<regex>: Grouping within repetition causes regex stack error #997

Open
AlexGuteniev opened this issue Jul 5, 2020 · 2 comments
Open

<regex>: Grouping within repetition causes regex stack error #997

AlexGuteniev opened this issue Jul 5, 2020 · 2 comments
Labels
bug Something isn't working vNext Breaks binary compatibility

Comments

@AlexGuteniev
Copy link
Contributor

AlexGuteniev commented Jul 5, 2020

Describe the bug
std::regex with satisfactory large input and certain compiler options overflows the stack

Command-line test case


d:\Temp2>type repro.cpp
#include <Windows.h>
#include <iostream>
#include <regex>
#include <string>

void regex_test()
{
    try
    {
        auto str = std::string(1000, 'a');
        auto match = std::smatch{};
        std::regex_match(str, match, std::regex("a+"));     // OK
        std::regex_match(str, match, std::regex("(?:a)+")); // stack error
    }
    catch (std::exception& ex)
    {
        std::cout << ex.what() << '\n';
    }
}

int main()
{
    bool stack_overflow_observed = false;
    __try
    {
        regex_test();
    }
    __except (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
    {
        stack_overflow_observed = true;
    }
    if (stack_overflow_observed)
    {
        _resetstkoflw();
        std::cout << "Stack overflow was observed\n";
    }
    else
    {
        std::cout << "Stack overflow was NOT observed\n";
    }
    return 0;
}

d:\Temp2>cl /EHa /RTC1 /MDd /std:c++17 .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29009.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.27.29009.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

d:\Temp2>.\repro.exe
Stack overflow was observed

Expected behavior
Successful parsing or C++ exception, but not stack overflow

STL version

Microsoft Visual Studio Professional 2019 Preview
Version 16.7.0 Preview 3.1

Additional context
This item is also tracked on Developer Community as DevCom-885115 and by Microsoft-internal VSO-1054746 / AB#1054746.

See also #405

vNext note: Resolving this issue will require breaking binary compatibility. We won't be able to accept pull requests for this issue until the vNext branch is available. See #169 for more information.

@AlexGuteniev
Copy link
Contributor Author

Boost.Regex actually can run into stack overflow and recover from it by itself. See BOOST_REGEX_RECURSIVE in its documentation. This strategy is better for performance of normal cases than avoiding stack allocation or trying to limit them to some amount.

There are actually few ways to accomplish this, and catching EXCEPTION_STACK_OVERFLOW is just one of them. Another is to call GetCurrentThreadStackLimits/SetThreadStackGuarantee and stand away from dangerously low stack.

@StephanTLavavej
Copy link
Member

We talked about this at the weekly maintainer meeting (see #1488). We concluded that reducing the recursion limit would likely cause more problems than it "solves", so we believe that we can't make changes here until vNext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vNext Breaks binary compatibility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants