Skip to content

Commit 959e5cf

Browse files
committed
StringIndexOutOfBounds thrown for malformed escape sequences #62
1 parent 5353bcb commit 959e5cf

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/main/java/com/dashjoin/jsonata/Tokenizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ Token next(boolean prefix) {
263263
currentChar = path.charAt(position);
264264
if (currentChar == '\\') { // escape sequence
265265
position++;
266-
currentChar = path.charAt(position);
266+
if (position < path.length()) currentChar = path.charAt(position); else throw new JException("S0103", position, "");
267267
if (escapes.get(""+currentChar)!=null) {
268268
qstr += escapes.get(""+currentChar);
269269
} else if (currentChar == 'u') {
270270
// u should be followed by 4 hex digits
271-
String octets = path.substring(position + 1, (position + 1) + 4);
271+
String octets = position+5 < path.length() ? path.substring(position + 1, (position + 1) + 4) : "";
272272
if (octets.matches("^[0-9a-fA-F]+$")) { // /^[0-9a-fA-F]+$/.test(octets)) {
273273
int codepoint = Integer.parseInt(octets, 16);
274274
qstr += Character.toString((char) codepoint);
@@ -278,7 +278,7 @@ Token next(boolean prefix) {
278278
}
279279
} else {
280280
// illegal escape sequence
281-
throw new JException("S0301", position, currentChar);
281+
throw new JException("S0103", position, currentChar);
282282

283283
}
284284
} else if (currentChar == quoteType) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.dashjoin.jsonata;
2+
3+
import static com.dashjoin.jsonata.Jsonata.jsonata;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class ParserTest {
8+
9+
@Test
10+
public void testUnsupportedEscapeSequence() {
11+
Assertions.assertThrows(JException.class, ()->jsonata("$substring('input', '\\a"));
12+
Assertions.assertThrows(JException.class, ()->jsonata("$substring('input', '\\"));
13+
Assertions.assertEquals("", jsonata("$substring('\\\\', 1)").evaluate(null));
14+
15+
Assertions.assertThrows(JException.class, ()->jsonata("$substring('\\u"));
16+
Assertions.assertEquals("", jsonata("$substring('\\uDDDD', 1)").evaluate(null));
17+
}
18+
}

0 commit comments

Comments
 (0)