@@ -409,15 +409,27 @@ Int_t TMessage::Uncompress()
409409 if (!fBufComp || !(fWhat & kMESS_ZIP ))
410410 return -1 ;
411411
412+ Int_t nbytesRemain = CompLength ();
413+ if (nbytesRemain < static_cast <Int_t>(3 * sizeof (Int_t))) {
414+ Error (" Uncompress" , " Compressed buffer too short (%d)" , CompLength ());
415+ return -1 ;
416+ }
412417 Int_t buflen;
413418 Int_t hdrlen = 2 *sizeof (UInt_t);
414419 char *bufcur1 = fBufComp + hdrlen;
415420 frombuf (bufcur1, &buflen);
416421 UChar_t *bufcur = (UChar_t*)bufcur1;
422+ nbytesRemain -= 3 * sizeof (Int_t);
423+
424+ if (buflen < hdrlen) {
425+ Error (" Uncompress" , " Uncompressed buffer length too short (%d)" , buflen);
426+ return -1 ;
427+ }
417428
418429 /* early consistency check */
419- Int_t nin, nbuf;
420- if (R__unzip_header (&nin, bufcur, &nbuf)!=0 ) {
430+ Int_t nin = 0 ;
431+ Int_t nbuf = 0 ;
432+ if ((nbytesRemain < ROOT::Internal::kZipHeaderSize ) || R__unzip_header (&nin, bufcur, &nbuf) != 0 ) {
421433 Error (" Uncompress" , " Inconsistency found in header (nin=%d, nbuf=%d)" , nin, nbuf);
422434 return -1 ;
423435 }
@@ -427,21 +439,34 @@ Int_t TMessage::Uncompress()
427439 fBufCur = fBuffer + sizeof (UInt_t) + sizeof (fWhat );
428440 fBufMax = fBuffer + fBufSize ;
429441 char *messbuf = fBuffer + hdrlen;
430-
442+
431443 // Force being owner of the newly created buffer
432444 SetBit (kIsOwner );
433445
434- Int_t nout;
446+ Int_t nout = 0 ;
435447 Int_t noutot = 0 ;
436- while (1 ) {
448+ Int_t objlenRemain = buflen - hdrlen;
449+ while (nbytesRemain >= ROOT::Internal::kZipHeaderSize ) {
437450 Int_t hc = R__unzip_header (&nin, bufcur, &nbuf);
438- if (hc!=0 ) break ;
451+ if ((hc != 0 ) || (nin > nbytesRemain) || (nbuf > objlenRemain))
452+ break ;
439453 R__unzip (&nin, bufcur, &nbuf, (unsigned char *) messbuf, &nout);
440454 if (!nout) break ;
441455 noutot += nout;
442456 if (noutot >= buflen - hdrlen) break ;
443457 bufcur += nin;
444458 messbuf += nout;
459+ nbytesRemain -= nin;
460+ objlenRemain -= nout;
461+ }
462+
463+ if (noutot != buflen - hdrlen) {
464+ Error (" Uncompress" , " buflen = %d, objlenRemain = %d, noutot = %d, nout=%d, nin=%d, nbuf=%d" , buflen, objlenRemain,
465+ noutot, nout, nin, nbuf);
466+ delete[] fBuffer ;
467+ fBuffer = fBufCur = fBufMax = nullptr ;
468+ fBufSize = 0 ;
469+ return -1 ;
445470 }
446471
447472 fWhat &= ~kMESS_ZIP ;
0 commit comments