Skip to content

Crash in Kotlin/Native when parsing deeply nested structure #1332

Open
@Skeptick

Description

@Skeptick

Describe the bug
When parsing a json structure with a lot of nesting, an application crashes without any stacktrace. I caught the problem on iOS but I checked it on macOS as well, and there it is also relevant.

If the parsing is started in a coroutine on the Default dispatcher (I use native-mt version), then 10 times less depth is enough. If you use arrays, you can reduce even more. In my real case, the crash occurs when nesting is more than 42 (😮).

To Reproduce

@Serializable
data class Sample(
    @SerialName("value") var value: Sample? = null
)

fun main() {
    val root = Sample()
    var last = root

    repeat(4000) {
        last.value = Sample()
        last = last.value!!
    }

    val string = Json.Default.encodeToString(Sample.serializer(), root)
    val result = Json.Default.decodeFromString(Sample.serializer(), string)
}

Environment

  • Kotlin version: 1.4.30
  • Library version: 1.0.1/1.1.0-RC
  • Kotlin platforms: Native
  • Gradle version: 6.7

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions