Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

node: avoid quadratic rescans on unclosed regex character classes

RRFZfe89e1fb4148
diff --git a/src/node.rs b/src/node.rs
index e13f5fa..10ed383 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -990,11 +990,15 @@ for (const [line0, name] of [[0, "f1"], [1, "f2"], [2, "f3"], [3, "f4"], [4, "f5
assert.deepEqual(info.blockers, [], `${name} had unexpected blockers: ${info.blockers}`);
}
-// Adversarial input with many slashes stays linear and preserves length
-const worst = "return " + "/ ".repeat(20000) + "\nconst ok = 1;";
-const res = codeOnly(worst);
-assert.equal(res.length, worst.length);
-assert.ok(res.endsWith("\nconst ok = 1;"));
+// Adversarial inputs with unterminated slashes (plain and unclosed character class) stay linear and preserve later code
+for (const worst of [
+ "x = /a\n".repeat(5000) + "const ok = 1;",
+ "x = /[".repeat(10000) + "\nconst ok = 1;",
+]) {
+ const res = codeOnly(worst);
+ assert.equal(res.length, worst.length);
+ assert.ok(res.endsWith("\nconst ok = 1;"));
+}
"#,
)
.unwrap();
diff --git a/src/node/runner.mjs b/src/node/runner.mjs
index 6486bf6..6fd45d4 100644
--- a/src/node/runner.mjs
+++ b/src/node/runner.mjs
@@ -87,7 +87,7 @@ function codeOnly(src) {
afterDot = false;
} else {
// Unterminated on this line: fall back to treating `/` as division and don't rescan the same tail.
- if (!inClass) noRegexBefore = j;
+ noRegexBefore = j;
i++;
regexAllowed = true;
afterDot = false;