Skip to content

Commit

Permalink
Correctly throw SerializationException instead of IOOBE for some case…
Browse files Browse the repository at this point in the history
…s with EOF in streams (#1677)

Fixes #1675
  • Loading branch information
sandwwraith committed Sep 23, 2021
1 parent 7285ed3 commit 4276aa1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ internal abstract class AbstractJsonLexer {

protected fun unexpectedToken(expected: Char) {
--currentPosition // To properly handle null
if (expected == STRING && consumeStringLenient() == NULL) {
if (currentPosition >= 0 && expected == STRING && consumeStringLenient() == NULL) {
fail("Expected string literal but 'null' literal was found.\n$coerceInputValuesHint", currentPosition - 4)
}
fail(charToTokenClass(expected))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.json.internal

import java.io.*
Expand Down Expand Up @@ -93,7 +97,7 @@ internal class ReaderJsonLexer(
if (position < source.length) return position
currentPosition = position
ensureHaveChars()
if (currentPosition != 0) return -1 // if something was loaded, then it would be zero.
if (currentPosition != 0 || source.isEmpty()) return -1 // if something was loaded, then it would be zero.
return 0
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.features

import kotlinx.serialization.StringData
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.internal.BATCH_SIZE
import kotlinx.serialization.test.decodeViaStream
import kotlinx.serialization.test.encodeViaStream
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class JsonJvmStreamsTest {
private val strLen = BATCH_SIZE * 2 + 42
Expand Down Expand Up @@ -47,4 +52,17 @@ class JsonJvmStreamsTest {
assertEquals(str, json.decodeViaStream(StringData.serializer(), input).data)
assertEquals(str, json.decodeViaStream(String.serializer(), str))
}

@Test
fun testThrowsCorrectExceptionOnEof() {
assertFailsWith<SerializationException> {
Json.decodeViaStream(StringData.serializer(), """{"data":""")
}
assertFailsWith<SerializationException> {
Json.decodeViaStream(StringData.serializer(), "")
}
assertFailsWith<SerializationException> {
Json.decodeViaStream(String.serializer(), "\"")
}
}
}

0 comments on commit 4276aa1

Please sign in to comment.