You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
classTest(valmsg:String)
funserializeTest() {
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
The text was updated successfully, but these errors were encountered:
rnentjes
added a commit
to rnentjes/kotlinx.serialization
that referenced
this issue
May 10, 2021
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:
To Reproduce
Attach a code snippet or test data if possible.
This is the resulting stack trace:
Expected behavior
The string should be serialized and no exception should be thrown.
Environment
The text was updated successfully, but these errors were encountered: