diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/JsonPathTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/JsonPathTest.kt index 8d31ba227..8205fa4d9 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/JsonPathTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/JsonPathTest.kt @@ -156,6 +156,27 @@ class JsonPathTest : JsonTestBase() { expectPath(expectedPath) { json.decodeFromString(Sealed.serializer(), malformed) } } + @Serializable + data class SimpleNested(val n: SimpleNested? = null, val t: DataObject? = null) + + @Serializable + data object DataObject + + @Test + fun testMalformedDataObjectInDeeplyNestedStructure() = jvmOnly { + var outer = SimpleNested(t = DataObject) + repeat(20) { + outer = SimpleNested(n = outer) + } + val str = Json.encodeToString(SimpleNested.serializer(), outer) + // throw-away data + Json.decodeFromString(SimpleNested.serializer(), str) + + val malformed = str.replace("{}", "42") + val expectedPath = "$" + ".n".repeat(20) + ".t\n" + expectPath(expectedPath) { Json.decodeFromString(SimpleNested.serializer(), malformed) } + } + private inline fun expectPath(path: String, block: () -> Unit) { val message = runCatching { block() } .exceptionOrNull()!!.message!! diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt index 14e70a425..d6e3de151 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt @@ -134,7 +134,9 @@ internal class JsonPath { private fun resize() { val newSize = currentDepth * 2 currentObjectPath = currentObjectPath.copyOf(newSize) - indicies = indicies.copyOf(newSize) + val newIndices = IntArray(newSize) { -1 } + indicies.copyInto(newIndices) + indicies = newIndices } override fun toString(): String = getPath()