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

JsonStringBuilder.appendStringSlowPath gives ArrayIndexOutOfBoundsException #1464

Closed
rnentjes opened this issue May 10, 2021 · 2 comments
Closed

Comments

@rnentjes
Copy link

Describe the bug

String serialization is broken if there are to many characters to escape in the string

In the JsonStringBuilder.appendStringSlowPath there seems to be a call missing to ensureTotalCapacity in the else block:

                when (val marker = ESCAPE_MARKERS[ch]) {
                    0.toByte() -> {
                        array[sz++] = ch.toChar()
                    }
                    1.toByte() -> {
                        val escapedString = ESCAPE_STRINGS[ch]!!
                        ensureTotalCapacity(sz + escapedString.length)
                        escapedString.toCharArray(array, sz, 0, escapedString.length)
                        sz += escapedString.length
                    }
                    else -> {
                        // I THINK THERE SHOULD BE A CALL TO ensureTotalCapacity HERE
                        array[sz] = '\\'
                        array[sz + 1] = marker.toChar()
                        sz += 2
                    }
                }

To Reproduce
Attach a code snippet or test data if possible.

@Serializable
class Test(val msg: String)

fun serializeTest() {
  val test = Test("""
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
    \n\n\n\n\n\n\n\n\n\n
  """.trimIndent())
  val result = JsonConfig.json.encodeToString(test)
  println(result)
}

This is the resulting stack trace:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 180 out of bounds for length 180
	at kotlinx.serialization.json.internal.JsonStringBuilder.appendStringSlowPath(JsonStringBuilder.kt:99)
	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)
	<serialize call here>

Expected behavior

The string should be serialized and no exception should be thrown.

Environment

  • Kotlin version: 1.5.0
  • Library version: 1.2.0
  • Kotlin platforms: JVM
  • Gradle version: 7.0
  • IDE version (if bug is related to the IDE): na (but 2021.1)
  • Other relevant context: Linux
rnentjes added a commit to rnentjes/kotlinx.serialization that referenced this issue May 10, 2021
rnentjes added a commit to rnentjes/kotlinx.serialization that referenced this issue May 10, 2021
@rnentjes
Copy link
Author

PR #1465 fixes this problem.

@JBeet
Copy link

JBeet commented May 11, 2021

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

3 participants