Skip to content

Commit 4276aa1

Browse files
authored
Correctly throw SerializationException instead of IOOBE for some cases with EOF in streams (#1677)
Fixes #1675
1 parent 7285ed3 commit 4276aa1

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

formats/json/commonMain/src/kotlinx/serialization/json/internal/lexer/AbstractJsonLexer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal abstract class AbstractJsonLexer {
196196

197197
protected fun unexpectedToken(expected: Char) {
198198
--currentPosition // To properly handle null
199-
if (expected == STRING && consumeStringLenient() == NULL) {
199+
if (currentPosition >= 0 && expected == STRING && consumeStringLenient() == NULL) {
200200
fail("Expected string literal but 'null' literal was found.\n$coerceInputValuesHint", currentPosition - 4)
201201
}
202202
fail(charToTokenClass(expected))

formats/json/jvmMain/src/kotlinx/serialization/json/internal/JsonLexerJvm.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
15
package kotlinx.serialization.json.internal
26

37
import java.io.*
@@ -93,7 +97,7 @@ internal class ReaderJsonLexer(
9397
if (position < source.length) return position
9498
currentPosition = position
9599
ensureHaveChars()
96-
if (currentPosition != 0) return -1 // if something was loaded, then it would be zero.
100+
if (currentPosition != 0 || source.isEmpty()) return -1 // if something was loaded, then it would be zero.
97101
return 0
98102
}
99103

formats/json/jvmTest/src/kotlinx/serialization/features/JsonJvmStreamsTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
/*
2+
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
15
package kotlinx.serialization.features
26

3-
import kotlinx.serialization.StringData
7+
import kotlinx.serialization.*
48
import kotlinx.serialization.builtins.serializer
59
import kotlinx.serialization.json.Json
610
import kotlinx.serialization.json.internal.BATCH_SIZE
711
import kotlinx.serialization.test.decodeViaStream
812
import kotlinx.serialization.test.encodeViaStream
913
import org.junit.Test
1014
import kotlin.test.assertEquals
15+
import kotlin.test.assertFailsWith
1116

1217
class JsonJvmStreamsTest {
1318
private val strLen = BATCH_SIZE * 2 + 42
@@ -47,4 +52,17 @@ class JsonJvmStreamsTest {
4752
assertEquals(str, json.decodeViaStream(StringData.serializer(), input).data)
4853
assertEquals(str, json.decodeViaStream(String.serializer(), str))
4954
}
55+
56+
@Test
57+
fun testThrowsCorrectExceptionOnEof() {
58+
assertFailsWith<SerializationException> {
59+
Json.decodeViaStream(StringData.serializer(), """{"data":""")
60+
}
61+
assertFailsWith<SerializationException> {
62+
Json.decodeViaStream(StringData.serializer(), "")
63+
}
64+
assertFailsWith<SerializationException> {
65+
Json.decodeViaStream(String.serializer(), "\"")
66+
}
67+
}
5068
}

0 commit comments

Comments
 (0)