Skip to content

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

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!regexmeow is a substring of homeowner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions