Skip to content
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

Serialising an object with a String field containing certain JSON crashes with java.lang.ArrayIndexOutOfBoundsException #1456

Closed
timorantalaiho opened this issue May 6, 2021 · 1 comment

Comments

@timorantalaiho
Copy link

timorantalaiho commented May 6, 2021

Describe the bug

Serialising an object with a String field containing certain JSON crashes with java.lang.ArrayIndexOutOfBoundsException:

CrashingJsonSerializationTest > bar() FAILED
    java.lang.ArrayIndexOutOfBoundsException: Index 128 out of bounds for length 128
        at kotlinx.serialization.json.internal.JsonStringBuilder.appendStringSlowPath(JsonStringBuilder.kt:102)
        at kotlinx.serialization.json.internal.JsonStringBuilder.appendQuoted(JsonStringBuilder.kt:61)
        at kotlinx.serialization.json.internal.Composer.printQuoted(Composers.kt:42)
        at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeString(StreamingJsonEncoder.kt:203)
        at kotlinx.serialization.encoding.AbstractEncoder.encodeStringElement(AbstractEncoder.kt:65)
        at CrashingJsonSerializationTest$MyObject$$serializer.serialize(CrashingJsonSerialisation.kt:13)
        at CrashingJsonSerializationTest$MyObject$$serializer.serialize(CrashingJsonSerialisation.kt:13)
        at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:211)
        at kotlinx.serialization.json.Json.encodeToString(Json.kt:80)
        at CrashingJsonSerializationTest.bar(CrashingJsonSerialisation.kt:10)

I tried to create a minimal input to reproduce the problem, based on the input we first experienced it with.

To Reproduce

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test

class CrashingJsonSerializationTest {
    @Test
    fun bar() {
        val failingString = "{\"status\":123,\"message\":\"content\",\"path\":\"/here/beeeeeeeeeeee/dragoons/d63574f-705c-49dd-a6bc-c8d1e524eefd/\"}"
        val o = MyObject(failingString)
        System.err.println(Json.encodeToString(MyObject.serializer(), o))
    }

    @Serializable
    data class MyObject(val text: String)
}

See a complete reproducible project at https://github.com/timorantalaiho/kotlin-json-crash .

Expected behavior

Object should be serialised with the String properly escaped.
Serialisation should not crash, regardless of the contents of the String.

Environment

  • Kotlin version: 1.5.0
  • Library version: 1.2.0
  • Kotlin platforms: OpenJDK 11 (openjdk version "11.0.11" 2021-04-20)
  • Gradle version: 6.8
@timorantalaiho
Copy link
Author

Hey: looks like this is a duplicate of #1441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant