Regex: don't overflow the stack on long lines
libstdc++'s std::regex matcher recurses about once per matched character, so '.*', 'a+' or '(a|b)*' on a line of a few tens of thousands of characters (minified JS/JSON) overflowed the 8 MiB stack and crashed the editor from the regex search prompt or regex replace. Regex search and regex replace now run their matching on a worker thread with a 1 GiB reserved stack (RunWithLargeStack; the caller waits, so nothing is shared concurrently). Renderers skip regex match highlighting on lines over 10,000 bytes. Catastrophic backtracking (e.g. (a*)*b) is still possible: std::regex has no time limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
@@ -695,3 +695,24 @@ TEST(Audit_UArg_DoesNotOverflow)
|
||||
ed.UArgStart();
|
||||
ASSERT_EQ(ed.UArgGet(), 1000000);
|
||||
}
|
||||
|
||||
|
||||
// std::regex recurses per matched character. Regex search and replace on a
|
||||
// very long line (minified JS/JSON) must not overflow the stack.
|
||||
TEST(Audit_Regex_LongLine_NoStackOverflow)
|
||||
{
|
||||
TestHarness h;
|
||||
Editor &ed = h.EditorRef();
|
||||
Buffer &b = h.Buf();
|
||||
b.insert_text(0, 0, std::string(200000, 'a') + "\nshort\n");
|
||||
b.SetCursor(0, 1);
|
||||
|
||||
ASSERT_TRUE(h.Exec(CommandId::RegexFindStart));
|
||||
ASSERT_TRUE(h.Exec(CommandId::InsertText, "(a|b)*"));
|
||||
ASSERT_TRUE(h.Exec(CommandId::Refresh));
|
||||
ASSERT_TRUE(!ed.PromptActive());
|
||||
|
||||
regex_replace_all(h, "a+", "b");
|
||||
ASSERT_EQ(h.Line(0), std::string("b"));
|
||||
ASSERT_EQ(h.Line(1), std::string("short"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user