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

Protobuf throws IllegalStateException #870

Closed
PaulWoitaschek opened this issue Jun 22, 2020 · 3 comments
Closed

Protobuf throws IllegalStateException #870

PaulWoitaschek opened this issue Jun 22, 2020 · 3 comments
Assignees
Labels

Comments

@PaulWoitaschek
Copy link
Contributor

Using Kotlin 1.3.72 and serialization 0.20.0, the following code throws an exception:

import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoBuf

@Serializable
class MyMessage(
    val value : Int
)

fun main(){
    ProtoBuf.load(MyMessage.serializer(), byteArrayOf(8))
}
Exception in thread "main" java.lang.IllegalStateException: Unexpected EOF
	at kotlinx.serialization.protobuf.ProtoBuf$Varint.decodeVarint$kotlinx_serialization_protobuf(ProtoBuf.kt:485)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufDecoder.decode32(ProtoBuf.kt:421)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufDecoder.decode32$default(ProtoBuf.kt:420)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufDecoder.nextInt(ProtoBuf.kt:370)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufReader.decodeTaggedInt(ProtoBuf.kt:271)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufReader.decodeTaggedInt(ProtoBuf.kt:236)
	at kotlinx.serialization.internal.TaggedDecoder.decodeIntElement(Tagged.kt:222)
	at MyMessage$$serializer.deserialize(protobug.kt)
	at MyMessage$$serializer.deserialize(protobug.kt:5)
	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufReader.decodeSerializableValue(ProtoBuf.kt:290)
	at kotlinx.serialization.DecodingKt.decode(Decoding.kt:521)
	at kotlinx.serialization.protobuf.ProtoBuf.load(ProtoBuf.kt:548)
	at kotlinx.serialization.protobuf.ProtoBuf$Default.load(ProtoBuf.kt)
	at ProtobugKt.main(protobug.kt:13)
	at ProtobugKt.main(protobug.kt)

This crashes my application because I use protobuf to serialize data to disc and catch SerializationException on parsing.
The correct behavior would be to wrap the IllegalStateException in a SerializationException.

@qwwdfsad qwwdfsad self-assigned this Jun 22, 2020
@EvgeniiKuznetsov
Copy link

EvgeniiKuznetsov commented Jun 23, 2020

Correct version:

@Serializable
data class MyMessage(@ProtoId(1) val value: Int)

fun main(){
    ProtoBuf.load(MyMessage.serializer(), byteArrayOf(8, 1)) //value=1
}

first byte - name
second byte - value (Int)

@PaulWoitaschek
Copy link
Contributor Author

This is not about the case that it's crashing (ofc it's crashing as it's invalid data). This issue is about the type of the exception.

qwwdfsad added a commit that referenced this issue Jul 21, 2020
    * Do not throw exceptions that are not subtype of SerializationException on unexpected inputs
    * Get rid of dead code

Fixes #870
@qwwdfsad qwwdfsad mentioned this issue Jul 21, 2020
@PaulWoitaschek
Copy link
Contributor Author

Wuhuu thanks 🎉

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

No branches or pull requests

3 participants