Skip to content

Commit a8846fa

Browse files
Fix resize in JsonPath
1 parent 4667a18 commit a8846fa

File tree

2 files changed

+24
-1
lines changed
  • formats

2 files changed

+24
-1
lines changed

formats/json-tests/commonTest/src/kotlinx/serialization/JsonPathTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ class JsonPathTest : JsonTestBase() {
156156
expectPath(expectedPath) { json.decodeFromString(Sealed.serializer(), malformed) }
157157
}
158158

159+
@Serializable
160+
data class SimpleNested(val n: SimpleNested? = null, val t: DataObject? = null)
161+
162+
@Serializable
163+
data object DataObject
164+
165+
@Test
166+
fun testMalformedDataObjectInDeeplyNestedStructure() {
167+
var outer = SimpleNested(t = DataObject)
168+
repeat(20) {
169+
outer = SimpleNested(n = outer)
170+
}
171+
val str = Json.encodeToString(SimpleNested.serializer(), outer)
172+
// throw-away data
173+
Json.decodeFromString(SimpleNested.serializer(), str)
174+
175+
val malformed = str.replace("{}", "42")
176+
val expectedPath = "$" + ".n".repeat(20) + ".t\n"
177+
expectPath(expectedPath) { Json.decodeFromString(SimpleNested.serializer(), malformed) }
178+
}
179+
159180
private inline fun expectPath(path: String, block: () -> Unit) {
160181
val message = runCatching { block() }
161182
.exceptionOrNull()!!.message!!

formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ internal class JsonPath {
134134
private fun resize() {
135135
val newSize = currentDepth * 2
136136
currentObjectPath = currentObjectPath.copyOf(newSize)
137-
indicies = indicies.copyOf(newSize)
137+
val newIndices = IntArray(newSize) { -1 }
138+
indicies.copyInto(newIndices)
139+
indicies = newIndices
138140
}
139141

140142
override fun toString(): String = getPath()

0 commit comments

Comments
 (0)