Fix numeric overflow in JsonLexer (#1367)
diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonLexer.kt b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonLexer.kt
index a1a92e8..3b6bd39 100644
--- a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonLexer.kt
+++ b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonLexer.kt
@@ -521,7 +521,11 @@
++current
}
currentPosition = current
- return if (isNegative) accumulator else -accumulator
+ return when {
+ isNegative -> accumulator
+ accumulator != Long.MIN_VALUE -> -accumulator
+ else -> fail("Numeric value overflow")
+ }
}
diff --git a/formats/json/commonTest/src/kotlinx/serialization/json/JsonParserFailureModesTest.kt b/formats/json/commonTest/src/kotlinx/serialization/json/JsonParserFailureModesTest.kt
index 06e78b8..a36b90a 100644
--- a/formats/json/commonTest/src/kotlinx/serialization/json/JsonParserFailureModesTest.kt
+++ b/formats/json/commonTest/src/kotlinx/serialization/json/JsonParserFailureModesTest.kt
@@ -62,6 +62,14 @@
it
)
}
+ // -9223372036854775808 is Long.MIN_VALUE
+ assertFailsWith<JsonDecodingException> {
+ default.decodeFromString(
+ Holder.serializer(),
+ """{"id":9223372036854775808}""",
+ it
+ )
+ }
assertFailsWith<JsonDecodingException> { default.decodeFromString(Holder.serializer(), """{"id"}""", it) }
assertFailsWith<JsonDecodingException> { default.decodeFromString(Holder.serializer(), """{"id}""", it) }
assertFailsWith<JsonDecodingException> { default.decodeFromString(Holder.serializer(), """{"i}""", it) }