Skip to content

Fix resize in JsonPath #2995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down