question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.ArrayIndexOutOfBoundsException in JsonStringBuilder 1.2.0

See original GitHub issue

Describe the bug java.lang.ArrayIndexOutOfBoundsException when using Json.encodeToString() with some input, idk what exactly is causing it Stacktrace:

java.lang.ArrayIndexOutOfBoundsException: Index 473 out of bounds for length 473
	at kotlinx.serialization.json.internal.JsonStringBuilder.appendStringSlowPath(JsonStringBuilder.kt:84)
	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.internal.StringSerializer.serialize(Primitives.kt:139)
	at kotlinx.serialization.internal.StringSerializer.serialize(Primitives.kt:136)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:211)
	at kotlinx.serialization.json.Json.encodeToString(Json.kt:80)
	at io.ktor.client.features.json.serializer.KotlinxSerializer.writeContent$ktor_client_serialization(KotlinxSerializer.kt:30)
	at io.ktor.client.features.json.serializer.KotlinxSerializer.write(KotlinxSerializer.kt:26)
	at io.ktor.client.features.json.JsonFeature$Feature$install$1.invokeSuspend(JsonFeature.kt:149)
	at io.ktor.client.features.json.JsonFeature$Feature$install$1.invoke(JsonFeature.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:246)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:126)
	at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:113)
	at io.ktor.client.features.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:246)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
	at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
	at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invoke(HttpRequestLifecycle.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:246)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
	at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:136)
	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:79)
	at io.ktor.client.HttpClient.execute(HttpClient.kt:191)
	at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:43)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:58)
	at me.melijn.melijnbot.internals.services.twitter.TwitterService.postNewTweets(TwitterService.kt:396)
	at me.melijn.melijnbot.internals.services.twitter.TwitterService.access$postNewTweets(TwitterService.kt:26)
	at me.melijn.melijnbot.internals.services.twitter.TwitterService$service$1.invokeSuspend(TwitterService.kt:45)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:46)
	at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:113)
	at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
	at io.ktor.client.HttpClient.execute(HttpClient.kt:191)
	at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:43)
	at me.melijn.melijnbot.internals.services.twitter.TwitterService.postNewTweets(TwitterService.kt:396)
	at me.melijn.melijnbot.internals.services.twitter.TwitterService$service$1.invokeSuspend(TwitterService.kt:45)
	at me.melijn.melijnbot.internals.threading.RunnableTask$run$1.invokeSuspend(Task.kt:140)
	at me.melijn.melijnbot.internals.threading.TaskManager$async$1$1.invokeSuspend(TaskManager.kt:29)
	at me.melijn.melijnbot.internals.threading.Task.run(Task.kt:12)
	at me.melijn.melijnbot.internals.threading.TaskManager$async$1.invokeSuspend(TaskManager.kt:30)

To Reproduce

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

Json.encodeToString("""{"avatar_url":"https://cdn.discordapp.com/avatars/384333349063491584/8adca1bddf8c5c46c7deed3edbd80d60.png","embeds":[{"color":1741274,"author":{"icon_url":"https://pbs.twimg.com/profile_images/1381321181719109633/4bpPMaer_normal.jpg","name":"Merlijn replied:","url":"https://twitter.com/@PixelHamster/status/1390719238155952129"},"description":"[@shroomizu](https://twitter.com/shroomizu) time for a pro controller","type":"rich","timestamp":"2021-05-07T17:24:39Z"}],"username":"Merijn"}""")

Expected behavior Returns the String that was passed to the encodeToString() fucntion

Environment

  • Kotlin version: 1.5.0
  • Library version: 1.2.0
  • Kotlin platforms: JVM
  • Gradle version: 7.0
  • Other relevant context JRE 15.0.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sandwwraithcommented, May 10, 2021

I’ll close this as a duplicate. We’ll fix this bug and release 1.2.1 soon

0reactions
IRuscommented, May 10, 2021

Reproducer:

@Serializable
data class MessageDTO(
    val id: String,
    val text: String,
    val created: Long,
    val tgId: String,
)
    @Test
    fun srl() {
        val messageDTO = MessageDTO(
            id = "mg-28",
            text = """Поздравляем с наступающим 2019 годом — и объявляем о возможности присоединиться к нашей команде.

2018 стал удачным годом для Telegram. Несмотря на попытки блокировки в авторитарных странах, за это время аудитория Telegram увеличилась на треть, финансовые ресурсы — более чем в 10 раз. 

В новом 2019 году Telegram расширяет звездную команду разработчиков. Заявки от кандидатов принимаем на @jobs_bot

Желаем счастья и свободы в новом году!""",
            created = 0,
            tgId = "123"
        )
        
        Json.encodeToString(MessageDTO.serializer(), messageDTO)
    }
Index 463 out of bounds for length 463
java.lang.ArrayIndexOutOfBoundsException: Index 463 out of bounds for length 463
	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)
	at io.heapy.tgto.dao.MessageDTO$$serializer.serialize(XdMessageDao.kt:12)
	at io.heapy.tgto.dao.MessageDTO$$serializer.serialize(XdMessageDao.kt:12)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:211)
	at kotlinx.serialization.json.Json.encodeToString(Json.kt:80)
	at io.heapy.tgto.dao.MapDbMessageDaoTest.srl(MapDbMessageDaoTest.kt:100)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	...
	at java.base/java.lang.Thread.run(Thread.java:829)
Read more comments on GitHub >

github_iconTop Results From Across the Web

What causes a java.lang.ArrayIndexOutOfBoundsException ...
The array goes out of bounds when the index you try to manipulate is more than the length of the array. For correctness,...
Read more >
java.lang.ArrayIndexOutOfBoundsException: 0 in 1.2.124
Using databases created with 1.2.124 in a desktop app, I'm occasionally getting error reports from users with this exception: org.h2.jdbc.
Read more >
The game crashed whilst initializing game Error: java.lang ...
I heard that a java update might help, tried that then the game won't ... ArrayIndexOutOfBoundsException: 0 Exit-Code: -1 ... at java.lang.
Read more >
java.lang.ArrayIndexOutOfBoundsException, when executing ...
ArrayIndexOutOfBoundsException: Array index out of range", occurs when using IBM® Rational® ClearQuest® TestManager (CQTM) for test execution.
Read more >
java.lang.ArrayIndexOutOfBoundsException when closing a ...
We recently experienced a flurry of SQLExceptions caused by an ArrayIndexOutOfBounds exception within the JDBC Driver in one of our web ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found