Skip to content

Commit

Permalink
Throw JsonDecodingException on empty string literal at the end of the…
Browse files Browse the repository at this point in the history
… input (#1011)

This PR fixes a bug where an unfinished string literal at the end of the input throws a StringIndexOutOfBoundsException instead of a JsonDecodingException.

Similar to #704, this should be a generic decoding exception instead.
  • Loading branch information
ErikP0 committed Aug 31, 2020
1 parent eacd19d commit cebdafc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ internal class JsonReader(private val source: String) {
private fun nextString(source: String, startPosition: Int) {
tokenPosition = startPosition
length = 0 // in buffer
var currentPosition = startPosition + 1
var currentPosition = startPosition + 1 // skip starting "
// except if the input ends
if (currentPosition >= source.length) {
fail("EOF", currentPosition)
}
var lastPosition = currentPosition
while (source[currentPosition] != STRING) {
if (source[currentPosition] == STRING_ESC) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ class JsonParserTest : JsonTestBase() {
val msg = e.message!!
assertTrue(msg.contains("Expected end of the object"))
}

@Test
fun testUnclosedStringLiteral() {
assertFailsWith<JsonDecodingException> {
parse("\"")
}

assertFailsWith<JsonDecodingException> {
parse("""{"id":"""")
}
}
}

0 comments on commit cebdafc

Please sign in to comment.