Skip to content

Commit

Permalink
Throw SerializationException instead of IllegalStateException in Enum… (
Browse files Browse the repository at this point in the history
#1325)

* Throw SerializationException instead of IllegalStateException in EnumSerializer

* Fix (almost) infinite loop in readVarintSlowPath on JS

     Because of shift+=7 addition and strict condition shift != 64, the loop never ends. This resulted in test timeout fail on JS. On JVM and Native, due to shift overflow, the test eventually came out of the loop (but that took quite a big amount of time, about million overflows).

Fixes #1303

Co-authored-by: Leonid Startsev <[email protected]>
  • Loading branch information
qwwdfsad and sandwwraith authored Feb 16, 2021
1 parent 358dc0b commit 7a0f671
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
14 changes: 10 additions & 4 deletions core/commonMain/src/kotlinx/serialization/internal/Enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ internal class EnumSerializer<T : Enum<T>>(

override fun serialize(encoder: Encoder, value: T) {
val index = values.indexOf(value)
check(index != -1) {
"$value is not a valid enum ${descriptor.serialName}, must be one of ${values.contentToString()}"
if (index == -1) {
throw SerializationException(
"$value is not a valid enum ${descriptor.serialName}, " +
"must be one of ${values.contentToString()}"
)
}
encoder.encodeEnum(descriptor, index)
}

override fun deserialize(decoder: Decoder): T {
val index = decoder.decodeEnum(descriptor)
check(index in values.indices) {
"$index is not among valid $${descriptor.serialName} enum values, values size is ${values.size}"
if (index !in values.indices) {
throw SerializationException(
"$index is not among valid ${descriptor.serialName} enum values, " +
"values size is ${values.size}"
)
}
return values[index]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,29 @@ internal class ByteArrayInput(private var array: ByteArray, private val endIndex
private fun readVarint64SlowPath(): Long {
var result = 0L
var shift = 0
while (shift != 64) {
while (shift < 64) {
val byte = read()
result = result or ((byte and 0x7F).toLong() shl shift)
if (byte and 0x80 == 0) {
return result
}
shift += 7
}
throw SerializationException("Varint too long: exceeded 64 bits")
throw SerializationException("Input stream is malformed: Varint too long (exceeded 64 bits)")
}

private fun readVarint32SlowPath(): Int {
var result = 0
var shift = 0
while (shift != 32) {
while (shift < 32) {
val byte = read()
result = result or ((byte and 0x7F) shl shift)
if (byte and 0x80 == 0) {
return result
}
shift += 7
}
throw SerializationException("Varint too long: exceeded 32 bits")
throw SerializationException("Input stream is malformed: Varint too long (exceeded 32 bits)")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.protobuf

import kotlinx.serialization.*
import kotlin.test.*

class ProtobufEnumTest {

enum class SomeEnum { ALPHA, BETA, GAMMA }

@Serializable
data class EnumWithUnion(@ProtoNumber(5) val s: String,
@ProtoNumber(6) val e: SomeEnum = SomeEnum.ALPHA,
@ProtoNumber(7) val i: Int = 42)

@Test
fun testEnumWithUnion() {
val data = EnumWithUnion("foo", SomeEnum.BETA)
val hex = ProtoBuf.encodeToHexString(EnumWithUnion.serializer(), data)
val restored = ProtoBuf.decodeFromHexString(EnumWithUnion.serializer(), hex)
assertEquals(data, restored)
}

@Serializable
class EnumHolder(val e: SomeEnum)

@Test
fun testUnknownValue() {
val bytes = ProtoBuf.encodeToByteArray(EnumHolder(SomeEnum.ALPHA))
bytes[1] = 3
assertFailsWith<SerializationException> { ProtoBuf.decodeFromByteArray<EnumHolder>(bytes) }
bytes[1] = -1
assertFailsWith<SerializationException> { ProtoBuf.decodeFromByteArray<EnumHolder>(bytes) }

}
}

This file was deleted.

0 comments on commit 7a0f671

Please sign in to comment.