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 unexpected exceptions #330

Merged
merged 4 commits into from
Mar 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions klaxon/src/main/kotlin/com/beust/klaxon/KlaxonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ internal class KlaxonParser(
VALUE_TYPE.tokenType, { world: World, token: Token ->
with(world) {
popStatus()
val key = popValue() as String
val key = popValue()
if (key !is String) {
throw KlaxonException("Object keys must be strings, got: $key")
}
parent = getFirstObject()
parent[key] = (token as Value<*>).value
status = peekStatus()
Expand All @@ -148,7 +151,10 @@ internal class KlaxonParser(
LEFT_BRACKET.tokenType, { world: World, _: Token ->
with(world) {
popStatus()
val key = popValue() as String
val key = popValue()
if (key !is String) {
throw KlaxonException("Object keys must be strings, got: $key")
}
parent = getFirstObject()
val newArray = JsonArray<Any>()
parent[key] = newArray
Expand All @@ -160,7 +166,10 @@ internal class KlaxonParser(
LEFT_BRACE.tokenType, { world: World, _: Token ->
with(world) {
popStatus()
val key = popValue() as String
val key = popValue()
if (key !is String) {
throw KlaxonException("Object keys must be strings, got: $key")
}
parent = getFirstObject()
val newObject = JsonObject()
parent[key] = newObject
Expand Down
23 changes: 16 additions & 7 deletions klaxon/src/main/kotlin/com/beust/klaxon/Lexer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,21 @@ class Lexer(passedReader: Reader, val lenient: Boolean = false): Iterator<Token>
't' -> currentValue.append("\t")
'u' -> {
val unicodeChar = StringBuilder(4)
.append(nextChar())
.append(nextChar())
.append(nextChar())
.append(nextChar())

val intValue = java.lang.Integer.parseInt(unicodeChar.toString(), 16)
try {
unicodeChar
.append(nextChar())
.append(nextChar())
.append(nextChar())
.append(nextChar())
} catch(_: IllegalStateException) {
throw KlaxonException("EOF reached in unicode char after: u$unicodeChar")
}

val intValue = try {
java.lang.Integer.parseInt(unicodeChar.toString(), 16)
} catch(e: NumberFormatException) {
throw KlaxonException("Failed to parse unicode char: u$unicodeChar")
}
currentValue.append(intValue.toChar())
}
else -> currentValue.append(c)
Expand Down Expand Up @@ -165,7 +174,7 @@ class Lexer(passedReader: Reader, val lenient: Boolean = false): Iterator<Token>
} else if (!isDone) {
while (isValueLetter(c)) {
currentValue.append(c)
if (! isValueLetter(peekChar())) {
if (isDone || !isValueLetter(peekChar())) {
break
} else {
c = nextChar()
Expand Down
41 changes: 41 additions & 0 deletions klaxon/src/test/kotlin/com/beust/klaxon/JazzerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.beust.klaxon

import org.testng.annotations.Test

class JazzerTest {
@Test(expectedExceptions = [KlaxonException::class])
fun characterInNumericLiteral() {
val json = "0r"
Parser.default().parse(StringBuilder(json))
}

@Test(expectedExceptions = [KlaxonException::class])
fun numericKeyAndObject() {
val json = "{1{"
Parser.default().parse(StringBuilder(json))
}

@Test(expectedExceptions = [KlaxonException::class])
fun numericKeyAndArray() {
val json = "{3["
Parser.default().parse(StringBuilder(json))
}

@Test(expectedExceptions = [KlaxonException::class])
fun numericKeyAndString() {
val json = "{0\"\""
Parser.default().parse(StringBuilder(json))
}

@Test(expectedExceptions = [KlaxonException::class])
fun incompleteUnicodeEscape() {
val json = "\"\\u"
Parser.default().parse(StringBuilder(json))
}

@Test(expectedExceptions = [KlaxonException::class])
fun nonNumericUnicodeEscape() {
val json = "\"\\u\\\\{["
Parser.default().parse(StringBuilder(json))
}
}