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:
+4
-1
@@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "TerminalRenderer.h"
|
||||
#include "RegexGuard.h"
|
||||
#include "Buffer.h"
|
||||
#include "Editor.h"
|
||||
#include "Highlight.h"
|
||||
@@ -70,7 +71,9 @@ TerminalRenderer::Draw(Editor &ed)
|
||||
if (ed.PromptActive() && (
|
||||
ed.CurrentPromptKind() == Editor::PromptKind::RegexSearch || ed.
|
||||
CurrentPromptKind() == Editor::PromptKind::RegexReplaceFind)) {
|
||||
try {
|
||||
// Long lines are not highlighted: std::regex recursion
|
||||
// could overflow the stack (RegexGuard.h).
|
||||
if (sline.size() <= kte::kRegexRenderLineLimit) try {
|
||||
std::regex rx(ed.SearchQuery());
|
||||
for (auto it = std::sregex_iterator(sline.begin(), sline.end(), rx);
|
||||
it != std::sregex_iterator(); ++it) {
|
||||
|
||||
Reference in New Issue
Block a user