Skip to content

Commit 1450174

Browse files
committed
Prevent NumberFormatException on ill-formed unicode escape
1 parent 5d31f97 commit 1450174

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

klaxon/src/main/kotlin/com/beust/klaxon/Lexer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ class Lexer(passedReader: Reader, val lenient: Boolean = false): Iterator<Token>
126126
throw KlaxonException("EOF reached in unicode char after: u$unicodeChar")
127127
}
128128

129-
val intValue = java.lang.Integer.parseInt(unicodeChar.toString(), 16)
129+
val intValue = try {
130+
java.lang.Integer.parseInt(unicodeChar.toString(), 16)
131+
} catch(e: NumberFormatException) {
132+
throw KlaxonException("Failed to parse unicode char: u$unicodeChar")
133+
}
130134
currentValue.append(intValue.toChar())
131135
}
132136
else -> currentValue.append(c)

klaxon/src/test/kotlin/com/beust/klaxon/JazzerTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ class JazzerTest {
3232
val json = "\"\\u"
3333
Parser.default().parse(StringBuilder(json))
3434
}
35+
36+
@Test(expectedExceptions = [KlaxonException::class])
37+
fun nonNumericUnicodeEscape() {
38+
val json = "\"\\u\\\\{["
39+
Parser.default().parse(StringBuilder(json))
40+
}
3541
}

0 commit comments

Comments
 (0)