@@ -67,6 +67,9 @@ class PackedDecoder(
6767 return if (inStructure) {
6868 decodeBooleanElement(currentDescriptor, currentIndex)
6969 } else {
70+ require(position < input.size) {
71+ " Unexpected EOF while decoding Boolean"
72+ }
7073 input[position++ ].toInt() != 0
7174 }
7275 }
@@ -75,6 +78,9 @@ class PackedDecoder(
7578 return if (inStructure) {
7679 decodeByteElement(currentDescriptor, currentIndex)
7780 } else {
81+ require(position < input.size) {
82+ " Unexpected EOF while decoding Byte"
83+ }
7884 input[position++ ]
7985 }
8086 }
@@ -133,6 +139,9 @@ class PackedDecoder(
133139 } else {
134140 val (len, bytesRead) = PackedUtils .decodeVarInt(input, position)
135141 position + = bytesRead
142+ require(len >= 0 && position + len <= input.size) {
143+ " Unexpected EOF while decoding String payload: need $len bytes from position=$position , size=${input.size} "
144+ }
136145 val bytes = input.copyOfRange(position, position + len)
137146 position + = len
138147 bytes.toString(Charsets .UTF_8 )
@@ -248,6 +257,9 @@ class PackedDecoder(
248257 descriptor : SerialDescriptor ,
249258 index : Int
250259 ): Byte {
260+ require(position < input.size) {
261+ " Unexpected EOF while decoding Byte element at index $index "
262+ }
251263 return input[position++ ]
252264 }
253265
@@ -285,6 +297,9 @@ class PackedDecoder(
285297 ): String {
286298 val (len, bytesRead) = PackedUtils .decodeVarInt(input, position)
287299 position + = bytesRead
300+ require(len >= 0 && position + len <= input.size) {
301+ " Unexpected EOF while decoding String element at index $index : need $len bytes from position=$position , size=${input.size} "
302+ }
288303 val bytes = input.copyOfRange(position, position + len)
289304 position + = len
290305 return bytes.toString(Charsets .UTF_8 )
@@ -369,10 +384,9 @@ class PackedDecoder(
369384 }
370385 }
371386
372-
373387 private fun readUtf8Char (): Char {
374- if (position >= input.size) {
375- error( " Unexpected EOF while decoding UTF-8 char" )
388+ require (position < input.size) {
389+ " Unexpected EOF while decoding UTF-8 char"
376390 }
377391
378392 val b0 = input[position].toInt() and 0xFF
@@ -385,10 +399,12 @@ class PackedDecoder(
385399
386400 // 2-byte: 110xxxxx 10xxxxxx
387401 (b0 and 0b1110_0000 ) == 0b1100_0000 -> {
388- if (position + 2 > input.size) error(" Unexpected EOF in 2-byte UTF-8 char" )
402+ require(position + 2 <= input.size) {
403+ " Unexpected EOF in 2-byte UTF-8 char"
404+ }
389405 val b1 = input[position + 1 ].toInt() and 0xFF
390- if ((b1 and 0b1100_0000 ) ! = 0b1000_0000 ) {
391- error( " Invalid UTF-8 continuation byte: 0x${b1.toString(16 )} " )
406+ require ((b1 and 0b1100_0000 ) = = 0b1000_0000 ) {
407+ " Invalid UTF-8 continuation byte: 0x${b1.toString(16 )} "
392408 }
393409 val codePoint =
394410 ((b0 and 0b0001_1111 ) shl 6 ) or
@@ -398,13 +414,14 @@ class PackedDecoder(
398414
399415 // 3-byte: 1110xxxx 10xxxxxx 10xxxxxx
400416 (b0 and 0b1111_0000 ) == 0b1110_0000 -> {
401- if (position + 3 > input.size) error(" Unexpected EOF in 3-byte UTF-8 char" )
417+ require(position + 3 <= input.size) {
418+ " Unexpected EOF in 3-byte UTF-8 char"
419+ }
402420 val b1 = input[position + 1 ].toInt() and 0xFF
403421 val b2 = input[position + 2 ].toInt() and 0xFF
404- if ((b1 and 0b1100_0000 ) != 0b1000_0000 ||
405- (b2 and 0b1100_0000 ) != 0b1000_0000
406- ) {
407- error(" Invalid UTF-8 continuation byte in 3-byte char" )
422+ require((b1 and 0b1100_0000 ) == 0b1000_0000 &&
423+ (b2 and 0b1100_0000 ) == 0b1000_0000 ) {
424+ " Invalid UTF-8 continuation byte in 3-byte char"
408425 }
409426 val codePoint =
410427 ((b0 and 0b0000_1111 ) shl 12 ) or
@@ -413,7 +430,9 @@ class PackedDecoder(
413430 3 to codePoint
414431 }
415432
416- else -> error(" UTF-8 sequence too long for Char (leading byte: 0x${b0.toString(16 )} )" )
433+ else -> throw IllegalArgumentException (
434+ " UTF-8 sequence too long for Char (leading byte: 0x${b0.toString(16 )} )"
435+ )
417436 }
418437
419438 position + = len
@@ -422,5 +441,4 @@ class PackedDecoder(
422441 // since Char is a UTF-16 code unit.
423442 return cp.toChar()
424443 }
425-
426444}
0 commit comments