@@ -437,7 +437,14 @@ static int decodePackedVarint32List(
437437 byte [] data , int position , ProtobufList <?> list , Registers registers ) throws IOException {
438438 final IntArrayList output = (IntArrayList ) list ;
439439 position = decodeVarint32 (data , position , registers );
440- final int fieldLimit = position + registers .int1 ;
440+ final int packedDataByteSize = registers .int1 ;
441+ if (packedDataByteSize < 0 ) {
442+ throw InvalidProtocolBufferException .negativeSize ();
443+ }
444+ if (packedDataByteSize > data .length - position ) {
445+ throw InvalidProtocolBufferException .truncatedMessage ();
446+ }
447+ final int fieldLimit = position + packedDataByteSize ;
441448 while (position < fieldLimit ) {
442449 position = decodeVarint32 (data , position , registers );
443450 output .addInt (registers .int1 );
@@ -453,7 +460,14 @@ static int decodePackedVarint64List(
453460 byte [] data , int position , ProtobufList <?> list , Registers registers ) throws IOException {
454461 final LongArrayList output = (LongArrayList ) list ;
455462 position = decodeVarint32 (data , position , registers );
456- final int fieldLimit = position + registers .int1 ;
463+ final int packedDataByteSize = registers .int1 ;
464+ if (packedDataByteSize < 0 ) {
465+ throw InvalidProtocolBufferException .negativeSize ();
466+ }
467+ if (packedDataByteSize > data .length - position ) {
468+ throw InvalidProtocolBufferException .truncatedMessage ();
469+ }
470+ final int fieldLimit = position + packedDataByteSize ;
457471 while (position < fieldLimit ) {
458472 position = decodeVarint64 (data , position , registers );
459473 output .addLong (registers .long1 );
@@ -471,10 +485,13 @@ static int decodePackedFixed32List(
471485 final IntArrayList output = (IntArrayList ) list ;
472486 position = decodeVarint32 (data , position , registers );
473487 final int packedDataByteSize = registers .int1 ;
474- final int fieldLimit = position + packedDataByteSize ;
475- if (fieldLimit > data .length ) {
488+ if (packedDataByteSize < 0 ) {
489+ throw InvalidProtocolBufferException .negativeSize ();
490+ }
491+ if (packedDataByteSize > data .length - position ) {
476492 throw InvalidProtocolBufferException .truncatedMessage ();
477493 }
494+ final int fieldLimit = position + packedDataByteSize ;
478495 output .ensureCapacity (output .size () + packedDataByteSize / 4 );
479496 while (position < fieldLimit ) {
480497 output .addInt (decodeFixed32 (data , position ));
@@ -493,10 +510,13 @@ static int decodePackedFixed64List(
493510 final LongArrayList output = (LongArrayList ) list ;
494511 position = decodeVarint32 (data , position , registers );
495512 final int packedDataByteSize = registers .int1 ;
496- final int fieldLimit = position + packedDataByteSize ;
497- if (fieldLimit > data .length ) {
513+ if (packedDataByteSize < 0 ) {
514+ throw InvalidProtocolBufferException .negativeSize ();
515+ }
516+ if (packedDataByteSize > data .length - position ) {
498517 throw InvalidProtocolBufferException .truncatedMessage ();
499518 }
519+ final int fieldLimit = position + packedDataByteSize ;
500520 output .ensureCapacity (output .size () + packedDataByteSize / 8 );
501521 while (position < fieldLimit ) {
502522 output .addLong (decodeFixed64 (data , position ));
@@ -515,10 +535,13 @@ static int decodePackedFloatList(
515535 final FloatArrayList output = (FloatArrayList ) list ;
516536 position = decodeVarint32 (data , position , registers );
517537 final int packedDataByteSize = registers .int1 ;
518- final int fieldLimit = position + packedDataByteSize ;
519- if (fieldLimit > data .length ) {
538+ if (packedDataByteSize < 0 ) {
539+ throw InvalidProtocolBufferException .negativeSize ();
540+ }
541+ if (packedDataByteSize > data .length - position ) {
520542 throw InvalidProtocolBufferException .truncatedMessage ();
521543 }
544+ final int fieldLimit = position + packedDataByteSize ;
522545 output .ensureCapacity (output .size () + packedDataByteSize / 4 );
523546 while (position < fieldLimit ) {
524547 output .addFloat (decodeFloat (data , position ));
@@ -537,10 +560,13 @@ static int decodePackedDoubleList(
537560 final DoubleArrayList output = (DoubleArrayList ) list ;
538561 position = decodeVarint32 (data , position , registers );
539562 final int packedDataByteSize = registers .int1 ;
540- final int fieldLimit = position + packedDataByteSize ;
541- if (fieldLimit > data .length ) {
563+ if (packedDataByteSize < 0 ) {
564+ throw InvalidProtocolBufferException .negativeSize ();
565+ }
566+ if (packedDataByteSize > data .length - position ) {
542567 throw InvalidProtocolBufferException .truncatedMessage ();
543568 }
569+ final int fieldLimit = position + packedDataByteSize ;
544570 output .ensureCapacity (output .size () + packedDataByteSize / 8 );
545571 while (position < fieldLimit ) {
546572 output .addDouble (decodeDouble (data , position ));
@@ -558,7 +584,14 @@ static int decodePackedBoolList(
558584 throws InvalidProtocolBufferException {
559585 final BooleanArrayList output = (BooleanArrayList ) list ;
560586 position = decodeVarint32 (data , position , registers );
561- final int fieldLimit = position + registers .int1 ;
587+ final int packedDataByteSize = registers .int1 ;
588+ if (packedDataByteSize < 0 ) {
589+ throw InvalidProtocolBufferException .negativeSize ();
590+ }
591+ if (packedDataByteSize > data .length - position ) {
592+ throw InvalidProtocolBufferException .truncatedMessage ();
593+ }
594+ final int fieldLimit = position + packedDataByteSize ;
562595 while (position < fieldLimit ) {
563596 position = decodeVarint64 (data , position , registers );
564597 output .addBoolean (registers .long1 != 0 );
@@ -575,7 +608,14 @@ static int decodePackedSInt32List(
575608 throws InvalidProtocolBufferException {
576609 final IntArrayList output = (IntArrayList ) list ;
577610 position = decodeVarint32 (data , position , registers );
578- final int fieldLimit = position + registers .int1 ;
611+ final int packedDataByteSize = registers .int1 ;
612+ if (packedDataByteSize < 0 ) {
613+ throw InvalidProtocolBufferException .negativeSize ();
614+ }
615+ if (packedDataByteSize > data .length - position ) {
616+ throw InvalidProtocolBufferException .truncatedMessage ();
617+ }
618+ final int fieldLimit = position + packedDataByteSize ;
579619 while (position < fieldLimit ) {
580620 position = decodeVarint32 (data , position , registers );
581621 output .addInt (CodedInputStream .decodeZigZag32 (registers .int1 ));
@@ -592,7 +632,14 @@ static int decodePackedSInt64List(
592632 throws InvalidProtocolBufferException {
593633 final LongArrayList output = (LongArrayList ) list ;
594634 position = decodeVarint32 (data , position , registers );
595- final int fieldLimit = position + registers .int1 ;
635+ final int packedDataByteSize = registers .int1 ;
636+ if (packedDataByteSize < 0 ) {
637+ throw InvalidProtocolBufferException .negativeSize ();
638+ }
639+ if (packedDataByteSize > data .length - position ) {
640+ throw InvalidProtocolBufferException .truncatedMessage ();
641+ }
642+ final int fieldLimit = position + packedDataByteSize ;
596643 while (position < fieldLimit ) {
597644 position = decodeVarint64 (data , position , registers );
598645 output .addLong (CodedInputStream .decodeZigZag64 (registers .long1 ));
0 commit comments