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:
Claude
2026-09-24 09:16:04 -07:00
committed by Kyle Isom
parent 812cde090f
commit 9f3da72765
8 changed files with 136 additions and 23 deletions
+2
View File
@@ -172,6 +172,7 @@ set(COMMON_SOURCES
ErrorHandler.cc
SyscallWrappers.cc
ErrorRecovery.cc
RegexGuard.cc
TerminalInputHandler.cc
TerminalRenderer.cc
TerminalFrontend.cc
@@ -384,6 +385,7 @@ if (BUILD_TESTS)
ErrorHandler.cc
SyscallWrappers.cc
ErrorRecovery.cc
RegexGuard.cc
KKeymap.cc
SwapRecorder.h
OptimizedSearch.cc