Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make regexp execution loop interruptible #1189 #1440

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,8 @@ private static int simpleMatch(
return -1;
}

private static boolean executeREBytecode(REGlobalData gData, String input, int end) {
private static boolean executeREBytecode(
Context cx, REGlobalData gData, String input, int end) {
int pc = 0;
byte[] program = gData.regexp.program;
int continuationOp = REOP_END;
Expand Down Expand Up @@ -1877,6 +1878,7 @@ private static boolean executeREBytecode(REGlobalData gData, String input, int e
}

for (; ; ) {
ScriptRuntime.addInstructionCount(cx, 5);

if (reopIsSimple(op)) {
int match = simpleMatch(gData, input, op, program, pc, end, true);
Expand Down Expand Up @@ -2306,6 +2308,7 @@ && simpleMatch(gData, input, op, program, pc, end, false) < 0) {
}

private static boolean matchRegExp(
Context cx,
REGlobalData gData,
RECompiled re,
String input,
Expand Down Expand Up @@ -2359,7 +2362,7 @@ && upcase(matchCh) == upcase((char) anchorCh))) {
for (int j = 0; j < re.parenCount; j++) {
gData.parens[j] = -1L;
}
boolean result = executeREBytecode(gData, input, end);
boolean result = executeREBytecode(cx, gData, input, end);

gData.backTrackStackTop = null;
gData.stateStackTop = null;
Expand Down Expand Up @@ -2393,7 +2396,7 @@ Object executeRegExp(
//
// Call the recursive matcher to do the real work.
//
boolean matches = matchRegExp(gData, re, str, start, end, res.multiline);
boolean matches = matchRegExp(cx, gData, re, str, start, end, res.multiline);
if (!matches) {
if (matchType != PREFIX) return null;
return Undefined.instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ public void forever() {
String source = "for(;;);";
baseCase(source);
}

@Test
public void longRunningRegExp() {
String source =
"/(.*){1,32000}[bc]/.test(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\");";
baseCase(source);
}
}