Skip to content

Commit d71fbb1

Browse files
committed
Fix index out of bounds when escaping strings
1 parent 7d5fb53 commit d71fbb1

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/StringUtilsTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void blockCjkUnifiedIdeographUnknownCharacters() throws Exception {
8282

8383
@Test
8484
public void malformedError() throws Exception {
85+
checkSyntaxErrorMessage("'\\N'", "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \\N character escape");
8586
checkSyntaxErrorMessage("'\\N {LATIN CAPITAL LETTER A}'", "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \\N character escape");
8687
checkSyntaxErrorMessage("'\\N LATIN CAPITAL LETTER A}'", "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \\N character escape");
8788
checkSyntaxErrorMessage("'\\N{LATIN CAPITAL LETTER A'", "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-24: malformed \\N character escape");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/StringUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public static String unescapeJavaString(String st) {
206206
*/
207207
@CompilerDirectives.TruffleBoundary
208208
private static int doCharacterName(String text, StringBuilder sb, int offset) {
209+
if (offset >= text.length()) {
210+
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.UnicodeDecodeError, UNICODE_ERROR + MALFORMED_ERROR, offset - 2, offset - 1);
211+
}
209212
char ch = text.charAt(offset);
210213
if (ch != '{') {
211214
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.UnicodeDecodeError, UNICODE_ERROR + MALFORMED_ERROR, offset - 2, offset - 1);

0 commit comments

Comments
 (0)