@@ -182,6 +182,17 @@ func (state *decoderState) decodeInt() int64 {
182
182
return int64 (x >> 1 )
183
183
}
184
184
185
+ // getLength decodes the next uint and makes sure it is a possible
186
+ // size for a data item that follows, which means it must fit in a
187
+ // non-negative int and fit in the buffer.
188
+ func (state * decoderState ) getLength () (int , bool ) {
189
+ n := int (state .decodeUint ())
190
+ if n < 0 || state .b .Len () < n || tooBig <= n {
191
+ return 0 , false
192
+ }
193
+ return n , true
194
+ }
195
+
185
196
// decOp is the signature of a decoding operator for a given type.
186
197
type decOp func (i * decInstr , state * decoderState , v reflect.Value )
187
198
@@ -363,16 +374,9 @@ func decComplex128(i *decInstr, state *decoderState, value reflect.Value) {
363
374
// describing the data.
364
375
// uint8 slices are encoded as an unsigned count followed by the raw bytes.
365
376
func decUint8Slice (i * decInstr , state * decoderState , value reflect.Value ) {
366
- u := state .decodeUint ()
367
- n := int (u )
368
- if n < 0 || uint64 (n ) != u {
369
- errorf ("length of %s exceeds input size (%d bytes)" , value .Type (), u )
370
- }
371
- if n > state .b .Len () {
372
- errorf ("%s data too long for buffer: %d" , value .Type (), n )
373
- }
374
- if n > tooBig {
375
- errorf ("byte slice too big: %d" , n )
377
+ n , ok := state .getLength ()
378
+ if ! ok {
379
+ errorf ("bad %s slice length: %d" , value .Type (), n )
376
380
}
377
381
if value .Cap () < n {
378
382
value .Set (reflect .MakeSlice (value .Type (), n , n ))
@@ -388,13 +392,9 @@ func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
388
392
// describing the data.
389
393
// Strings are encoded as an unsigned count followed by the raw bytes.
390
394
func decString (i * decInstr , state * decoderState , value reflect.Value ) {
391
- u := state .decodeUint ()
392
- n := int (u )
393
- if n < 0 || uint64 (n ) != u || n > state .b .Len () {
394
- errorf ("length of %s exceeds input size (%d bytes)" , value .Type (), u )
395
- }
396
- if n > state .b .Len () {
397
- errorf ("%s data too long for buffer: %d" , value .Type (), n )
395
+ n , ok := state .getLength ()
396
+ if ! ok {
397
+ errorf ("bad %s slice length: %d" , value .Type (), n )
398
398
}
399
399
// Read the data.
400
400
data := make ([]byte , n )
@@ -406,7 +406,11 @@ func decString(i *decInstr, state *decoderState, value reflect.Value) {
406
406
407
407
// ignoreUint8Array skips over the data for a byte slice value with no destination.
408
408
func ignoreUint8Array (i * decInstr , state * decoderState , value reflect.Value ) {
409
- b := make ([]byte , state .decodeUint ())
409
+ n , ok := state .getLength ()
410
+ if ! ok {
411
+ errorf ("slice length too large" )
412
+ }
413
+ b := make ([]byte , n )
410
414
state .b .Read (b )
411
415
}
412
416
@@ -688,8 +692,8 @@ func (dec *Decoder) ignoreInterface(state *decoderState) {
688
692
error_ (dec .err )
689
693
}
690
694
// At this point, the decoder buffer contains a delimited value. Just toss it.
691
- n := int ( state .decodeUint () )
692
- if n < 0 || state . b . Len () < n {
695
+ n , ok := state .getLength ( )
696
+ if ! ok {
693
697
errorf ("bad interface encoding: length too large for buffer" )
694
698
}
695
699
state .b .Drop (n )
0 commit comments