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

1.3.0 randomly crashs in decodeFromStream mode #1702

Closed
slavonnet opened this issue Sep 25, 2021 · 1 comment
Closed

1.3.0 randomly crashs in decodeFromStream mode #1702

slavonnet opened this issue Sep 25, 2021 · 1 comment
Assignees

Comments

@slavonnet
Copy link

slavonnet commented Sep 25, 2021

2021-09-25 09:43:15.063 9938-9938/com.cisystems.ssmp.arm_vmb2 E/coroutineEH: Unexpected JSON token at offset 16249: Unexpected EOF during unicode escape
    JSON input: .....0432 \u041c\u041e","address":"\u0440-\u043d \u041a\u0440\u04.....
    kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 16249: Unexpected EOF during unicode escape
    JSON input: .....0432 \u041c\u041e","address":"\u0440-\u043d \u041a\u0440\u04.....

if i move to decodeFromString then all is ok. My implement


  override fun responseBodyConverter(
        type: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit,
    ): Converter<ResponseBody, *> =
        Converter { value: ResponseBody  ->
            J.decodeFromStream(J.serializersModule.serializer(type).nullable, value.byteStream() )
//            J.decodeFromString(J.serializersModule.serializer(type).nullable, value.bytes().decodeToString() )
        }


@slavonnet slavonnet changed the title 1.3.9 randomly crashs 1.309 randomly crashs in decodeFromStream mode Sep 25, 2021
@slavonnet slavonnet changed the title 1.309 randomly crashs in decodeFromStream mode 1.3.- randomly crashs in decodeFromStream mode Sep 25, 2021
@slavonnet slavonnet changed the title 1.3.- randomly crashs in decodeFromStream mode 1.3.-0 randomly crashs in decodeFromStream mode Sep 25, 2021
@slavonnet slavonnet changed the title 1.3.-0 randomly crashs in decodeFromStream mode 1.3.0 randomly crashs in decodeFromStream mode Sep 25, 2021
@qwwdfsad
Copy link
Contributor

qwwdfsad commented Sep 27, 2021

Reproducing test:

 @Test
    fun testRandomEscapeSequences()  { 
        repeat(10_000) {
            val s = generateRandomString()
            try {
                assertSerializedAndRestored(s, String.serializer())
            } catch (e: Throwable) {
                // Not assertion error to preserve cause
                throw IllegalStateException("Unexpectedly failed test, cause string: $s", e)
            }
        }
    }

    fun <T : Any> assertSerializedAndRestored(
        original: T,
        serializer: KSerializer<T>,
        format: Json = Json,
        printResult: Boolean = false
    ) {
        if (printResult) println("[Input] $original")
        val b = ByteArrayOutputStream()
        format.encodeToStream(serializer, original, b)
        val restored = format.decodeFromStream(serializer, ByteArrayInputStream(b.toByteArray()))
        if (printResult) println("[Restored form] $restored")
//        assertEquals(original, restored)
    }

    private fun generateRandomString(): String {
        val size = Random.nextInt(1, 32 * 1024)
        return buildString(size) {
            repeat(size) {
                val pickEscape = Random.nextBoolean()
                if (pickEscape) {
                    // Definitely escape symbol
                    // null can be appended as well, completely okay
                    append(ESCAPE_STRINGS.random())
                } else {
                    // Any symbol, including escaping one
                    append(Char(Random.nextInt(Char.MIN_VALUE.code..Char.MAX_VALUE.code)))
                }
            }
        }
    }

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

No branches or pull requests

3 participants