Skip to content

<regex>: regex_match gets caught in recursive loop until stack overflow occurs #1528

@freezerdev

Description

@freezerdev

Describe the bug
I have found a case where calling regex_match can result in a recursive loop until a stack overflow occurs. Problem occurs when run as x64 debug. When run as x64 release an exception occurs. And when run as x86 debug or release, program works as expected.

Command-line test case

#include <iostream>
#include <regex>

int main(void)
{
	auto flags = std::regex_constants::ECMAScript | std::regex::icase;

	try
	{
		std::wregex rgx(L"^http[s]?\\:\\/\\/([^.]+\\.)*github\\.com\\/.*$", flags);
		bool bResult = std::regex_match(L"https://www.google.com/search?client=firefox-b-1-d&q=tea%20box&tbs=lf:1,lf_ui:10&tbm=lcl&sxsrf=ALeKk03GLkevbpot_6JmlWw6_IJKeKBKbw:1607540768824&rflfq=1&num=10&rldimm=11474290690396232776&lqi=Cgd0ZWEgYm94Gd9r-yQGeFTPSKvez_DmgICACFoWCgd0ZWEgYm94EAAQASIHdGVhIGJveIABAQ&phdesc=-P7Y2CJJHVU&ved=2ahUKEwjA9PKDzMHtAhVTMH0KHfnkAUIQvS4wBHoECAQQOQ&rlst=f#rlfi%3Dhd%3A%3Bsi%3A11474290690396232776%2Cl%2CCgd0ZWEgYm94Gd9r-yQGeFTPSKvez_DmgICACFoWCgd0ZWEgYm94EAAQASIHdGVhIGJveIABAQ%2Cy%2C-P7Y2CJJHVU%3Bmv%3A%5B%5B35.638039299999996%2C-117.9412485%5D%2C%5B34.634577799999995%2C-120.8545755%5D%5D%3Btbs%3Alrf%3A%211m4%211u3%212m2%213m1%211e1%211m4%211u2%212m2%212m1%211e1%212m1%211e2%212m1%211e3%213sIAE%2Clf%3A1%2Clf_ui%3A10", rgx);

		if(bResult)
			std::cout << "Match" << std::endl;
		else
			std::cout << "Does not match" << std::endl;
	}
	catch(const std::regex_error &e)
	{
		std::cout << "Exception" << std::endl;
	}

	return 0;
}

Expected behavior
The expression does not match, so the expected result is "Does not match." As stated, the result varies depending on how the code is compiled. x64 debug results in stack overflow. x64 release results in an exception. And x86 debug/release yields the correct result of "Does not match."

STL version

Microsoft Visual Studio Professional 2019
Version 16.8.3

Additional context
Tested on Linux using gcc and clang, in both cases the STL correct returns that it does not match. Only the Windows implementation of the STL results in a stack overflow.

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