Skip to content

Commit 191803b

Browse files
committed
[JSC] RegExp legacy static properties should not crash when the lazy re-match fails
https://bugs.webkit.org/show_bug.cgi?id=328417 Reviewed by Yusuke Suzuki. `RegExp.prototype.test` only records the position of the match, and `RegExp.lastMatch`, `RegExp.$1` etc. run the match again to get the captures. This second run can fail even though the first one succeeded: the capturing YarrJIT code uses more stack than the match-only code, so it can fall back to the interpreter, which reports "no match" when it runs out of memory. We then dereferenced a null JSArray. This patch throws an out of memory error in that case. Test: JSTests/stress/regexp-legacy-static-properties-rematch-failure.js * JSTests/stress/regexp-legacy-static-properties-rematch-failure.js: Added. (shouldBe): (shouldThrowRangeError): (regExp.test): * Source/JavaScriptCore/runtime/RegExpCachedResult.cpp: (JSC::RegExpCachedResult::lastResult): Canonical link: https://commits.webkit.org/324533@main
1 parent 8b06b6a commit 191803b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@ requireOptions("--maxRegExpStackSize=65536")
2+
3+
function shouldBe(actual, expected) {
4+
if (actual !== expected)
5+
throw new Error(`expected ${expected} but got ${actual}`);
6+
}
7+
8+
function shouldThrowRangeError(func) {
9+
let error;
10+
try {
11+
func();
12+
} catch (e) {
13+
error = e;
14+
}
15+
if (!(error instanceof RangeError))
16+
throw new Error(`expected RangeError but got ${error}`);
17+
}
18+
19+
const string = "a".repeat(2000) + "c";
20+
const regExp = new RegExp("(?:(?!(?:a|b)*d)a)*c" + "(x)?".repeat(1600));
21+
22+
if (regExp.test(string)) {
23+
for (const name of ["lastMatch", "lastParen", "$1", "leftContext", "rightContext"])
24+
shouldThrowRangeError(() => RegExp[name]);
25+
shouldThrowRangeError(() => { RegExp.input = "x"; });
26+
shouldBe(RegExp.input, string);
27+
}
28+
29+
shouldBe(/b(c)d/.test("abcde"), true);
30+
shouldBe(RegExp.lastMatch, "bcd");
31+
shouldBe(RegExp.lastParen, "c");
32+
shouldBe(RegExp.$1, "c");
33+
shouldBe(RegExp.leftContext, "a");
34+
shouldBe(RegExp.rightContext, "e");
35+
shouldBe(RegExp.input, "abcde");

‎Source/JavaScriptCore/runtime/RegExpCachedResult.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ JSArray* RegExpCachedResult::lastResult(JSGlobalObject* globalObject, JSObject*
7979
MatchResult ignoreMatched;
8080
result = createRegExpMatchesArray(vm, globalObject, string, input, m_lastRegExp.get(), m_result.start, ignoreMatched);
8181
RETURN_IF_EXCEPTION(scope, nullptr);
82+
if (!result) [[unlikely]] {
83+
throwOutOfMemoryError(globalObject, scope);
84+
return nullptr;
85+
}
8286
} else {
8387
result = createEmptyRegExpMatchesArray(globalObject, m_lastInput.get(), m_lastRegExp.get());
8488
RETURN_IF_EXCEPTION(scope, nullptr);

0 commit comments

Comments
 (0)