Skip to content

Commit 82c9e7d

Browse files
committed
Fix CBOR after dev rebase
- Align byte parser/skip logic with dev (length checks, truncation, indefinite chunks, error messages)\n- Keep structured CBOR helpers warning-free under -Werror\n- Restore tree reader EOF semantics for CborElement decoding
1 parent c2de9c0 commit 82c9e7d

5 files changed

Lines changed: 242 additions & 137 deletions

File tree

formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/CborElementSerializers.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ internal fun Decoder.asCborDecoder(): CborDecoder = this as? CborDecoder
278278
)
279279

280280
/*need to expose writer to access encodeTag()*/
281+
@IgnorableReturnValue
281282
internal fun Encoder.asCborEncoder() = this as? CborEncoder
282283
?: throw IllegalStateException(
283284
"This serializer can be used only with Cbor format. " +

formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/CborParserInterface.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ internal sealed interface CborParserInterface {
3939
// Tag verification
4040
fun verifyTagsAndThrow(expected: ULongArray, actual: ULongArray?)
4141

42+
@IgnorableReturnValue
4243
fun processTags(tags: ULongArray?): ULongArray?
43-
}
44+
}

formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/CborTreeReader.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,24 @@ internal class CborTreeReader(
5555
7 -> { // Major type 7: simple/float/break
5656
when (parser.curByte) {
5757
0xF4 -> {
58-
parser.readByte() // Advance parser position
59-
CborBoolean(false, tags = tags)
58+
CborBoolean(parser.nextBoolean(null), tags = tags)
6059
}
6160

6261
0xF5 -> {
63-
parser.readByte() // Advance parser position
64-
CborBoolean(true, tags = tags)
62+
CborBoolean(parser.nextBoolean(null), tags = tags)
6563
}
6664

6765
0xF6 -> {
68-
parser.nextNull()
66+
parser.nextNull(null)
6967
CborNull(tags = tags)
7068
}
7169

7270
0xF7 -> {
73-
parser.readByte() // Advance parser position
71+
parser.skipElement(null)
7472
CborUndefined(tags = tags)
7573
}
7674
// Half/Float32/Float64
77-
NEXT_HALF, NEXT_FLOAT, NEXT_DOUBLE -> CborFloat(parser.nextDouble(), tags = tags)
75+
NEXT_HALF, NEXT_FLOAT, NEXT_DOUBLE -> CborFloat(parser.nextDouble(null), tags = tags)
7876
else -> throw CborDecodingException(
7977
"Invalid simple value or float type: ${parser.curByte.toString(16).uppercase()}"
8078
)
@@ -94,22 +92,18 @@ internal class CborTreeReader(
9492
* Reads any tags preceding the current value.
9593
* @return An array of tags, possibly empty
9694
*/
97-
@OptIn(ExperimentalUnsignedTypes::class)
9895
private fun readTags(): ULongArray {
99-
val tags = mutableListOf<ULong>()
96+
if ((parser.curByte shr 5) != 6) return EMPTY_TAGS
10097

101-
// Read tags (major type 6) until we encounter a non-tag
98+
val tags = mutableListOf<ULong>()
10299
while ((parser.curByte shr 5) == 6) { // Major type 6: tag
103-
val tag = parser.nextTag()
104-
tags.add(tag)
100+
tags.add(parser.nextTag())
105101
}
106-
107102
return tags.toULongArray()
108103
}
109104

110-
111105
private fun readArray(tags: ULongArray): CborArray {
112-
val size = parser.startArray()
106+
val size = parser.startArray(null)
113107
val elements = mutableListOf<CborElement>()
114108

115109
if (size >= 0) {
@@ -129,7 +123,7 @@ internal class CborTreeReader(
129123
}
130124

131125
private fun readMap(tags: ULongArray): CborMap {
132-
val size = parser.startMap()
126+
val size = parser.startMap(null)
133127
val elements = mutableMapOf<CborElement, CborElement>()
134128

135129
if (size >= 0) {

0 commit comments

Comments
 (0)