Skip to content

Commit

Permalink
Sanitize input for substring$ (JabRef#11322)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored May 22, 2024
1 parent 7dce65a commit aa80a72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/jabref/logic/bst/BstFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,9 @@ void bstSubstring(BstVMVisitor visitor, ParserRuleContext ctx) {
length = Integer.MAX_VALUE / 2;
}

if (start > (Integer.MAX_VALUE / 2)) {
start = Integer.MAX_VALUE / 2;
}

if (start < (Integer.MIN_VALUE / 2)) {
start = -Integer.MIN_VALUE / 2;
if ((start > string.length()) || (start < -string.length())) {
stack.push("");
return;
}

if (start < 0) {
Expand All @@ -726,7 +723,12 @@ void bstSubstring(BstVMVisitor visitor, ParserRuleContext ctx) {
}

int zeroBasedStart = start - 1;
String result = string.substring(zeroBasedStart, Math.min(zeroBasedStart + length, string.length()));
int zeroBasedEnd = Math.min(zeroBasedStart + length, string.length());

// Sanitize too large start values
zeroBasedStart = Math.min(zeroBasedStart, zeroBasedEnd);

String result = string.substring(zeroBasedStart, zeroBasedEnd);

LOGGER.trace("substring$(s, start, len): ({}, {}, {})={}", string, start, length, result);
stack.push(result);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jabref/logic/bst/BstFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ public void substring() throws RecognitionException {
"abc, abcd, -2, 2147483647",
"b, abcd, -3, 1",
"a, abcd, -4, 1",
"'', abcd, -5, 1" // invalid number -5
"'', abcd, -5, 1", // invalid number -5
"'', abcd, -2147483647, 2147483647", // invalid number
})
void substringPlain(String expected, String full, Integer start, Integer length) {
BstVMContext bstVMContext = new BstVMContext(List.of(), new BibDatabase(), Path.of("404.bst"));
Expand Down

0 comments on commit aa80a72

Please sign in to comment.