microsoft / microsoft/STL

<regex>: regex_search() throws regex_error(error_complexity) when a long string is used with a lookahead regex

Open
#6,347 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug regex
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug

We a trying to upgrade the compiler to the latest MSVC version. First time we did hit this issue #5792. After it was fixed and new MSVC version published, we've upgraded again to 14.51 and now we are getting another issue, this time with regex_error(error_complexity) when a lookahead regex is being used with a really long search string.

Note that if I make the path string shorter, everything is working good. While this might be an expected limitation, we didn't have issues with Visual Studio 2022. Also on Linux with GCC 13 this code is working properly.

Command-line test case

#include <iostream>
#include <regex>

int main()
{
	try {
		std::regex pattern(R"((?:.+[\/\\])?(?:.+?)_(.+?)_(.+?)@(.+?)\.(?:exe|app))");
		std::smatch matches;
		std::string requestStringParams(R"(D:\some\really\long\path\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\dir\myapp.exe https://mysite.com/doaction/?action=56)");
		std::regex_search(requestStringParams, matches, pattern);
		std::cout << "no exception\n";
	}
	catch (const std::regex_error& ex) {
		std::cout << "regex_error with code " << ex.code() << "\n";
	}

	return 0;
}

This prints "regex_error with code 11".

Expected behavior

Should print "no exception".

STL version

Microsoft (R) C/C++ Optimizing Compiler Version 19.51.36248 for x64

Additional context

Might be related to issue #5792 but the exception is with different error code.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied C++ command-line reproducer with the long path and regex_search(), using the reported MSVC 19.51 environment. Trace how std::regex_search handles this pattern and the regex_error(error_complexity) result, then verify that the reproducer completes without an exception while preserving the expected match behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.