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

fix the error message in case the quantifier maximum (second value) is smaller than the minimum (first value) #1260

Merged
merged 2 commits into from
Oct 8, 2022
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
11 changes: 7 additions & 4 deletions src/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ private static boolean parseTerm(CompilerState state) {
int max = -1;
int leftCurl = state.cp;

/* For Perl etc. compatibility, if quntifier does not match
/* For Perl etc. compatibility, if quantifier does not match
* \{\d+(,\d*)?\} exactly back off from it
* being a quantifier, and chew it up as a literal
* atom next time instead.
Expand All @@ -1091,9 +1091,12 @@ private static boolean parseTerm(CompilerState state) {
max = getDecimalValue(c, state, 0xFFFF, "msg.overlarge.max");
c = src[state.cp];
if (min > max) {
reportError(
"msg.max.lt.min", String.valueOf(src[state.cp]));
return false;
String msg =
ScriptRuntime.getMessageById(
"msg.max.lt.min",
Integer.valueOf(max),
Integer.valueOf(min));
throw ScriptRuntime.constructError("SyntaxError", msg);
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/resources/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ msg.zero.quant =\
Zero quantifier {0}

msg.max.lt.min =\
Maximum {0} less than minimum
Invalid regular expression: The quantifier maximum ''{0}'' is less than the minimum ''{1}''.

msg.unterm.quant =\
Unterminated quantifier {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ msg.zero.quant =\
Le quantificateur {0} est nul

msg.max.lt.min =\
Le maximum {0} est inf\u00E9rieur au minimum
Expression r\u00e9guli\u00e8re invalide : Le quantificateur maximum ''{0}'' est inf\u00e9rieur au minimum ''{1}''.

msg.unterm.quant =\
Le quantificateur {0} n''a pas de limite
Expand Down
17 changes: 17 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeRegExpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,21 @@ public void search() {
assertEquals(-1, cx.evaluateString(scope, source, "test", 0, null));
}
}

@Test
public void regExWrongQuantifier() {
try (Context cx = Context.enter()) {
ScriptableObject scope = cx.initStandardObjects();

String source = "'abc'.search(/b{2,1}/);";
try {
cx.evaluateString(scope, source, "test", 0, null);
fail("Shoud throw");
} catch (Exception e) {
assertEquals(
"SyntaxError: Invalid regular expression: The quantifier maximum '1' is less than the minimum '2'.",
e.getMessage());
}
}
}
}