Skip to content

Commit

Permalink
Merge pull request #330 from CodeIntelligenceTesting/master
Browse files Browse the repository at this point in the history
Fix unexpected exceptions
  • Loading branch information
cbeust authored Mar 9, 2021
2 parents ad871f5 + 1450174 commit 9d8e18f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
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))
}
}

0 comments on commit 9d8e18f

Please sign in to comment.