Skip to content

Commit fa0c0b2

Browse files
TreeHunter9Artyom Ivanov
authored andcommitted
Merge commit from fork
Co-authored-by: Artyom Ivanov <artyom.ivanov@red-soft.ru>
1 parent 6d8a64d commit fa0c0b2

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/common/classes/ClumpletReader.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ FB_SIZE_T ClumpletReader::getClumpletSize(bool wTag, bool wLength, bool wData) c
589589
return 0;
590590
}
591591

592+
const FB_UINT64 maxTotalLength = buffer_end - clumplet;
593+
592594
FB_SIZE_T rc = wTag ? 1 : 0;
593595
FB_SIZE_T lengthSize = 0;
594596
FB_SIZE_T dataSize = 0;
@@ -664,15 +666,16 @@ FB_SIZE_T ClumpletReader::getClumpletSize(bool wTag, bool wLength, bool wData) c
664666
invalid_structure("unknown clumplet type", t);
665667
}
666668

667-
const FB_SIZE_T total = 1 + lengthSize + dataSize;
668-
if (clumplet + total > buffer_end)
669+
// Avoid possible overflow
670+
FB_UINT64 totalLength = 1 + lengthSize + static_cast<FB_UINT64>(dataSize);
671+
if (totalLength > maxTotalLength)
669672
{
670-
invalid_structure("buffer end before end of clumplet - clumplet too long", total);
671-
FB_SIZE_T delta = total - (buffer_end - clumplet);
673+
invalid_structure("buffer end before end of clumplet - clumplet too long", totalLength);
674+
FB_UINT64 delta = totalLength - maxTotalLength;
672675
if (delta > dataSize)
673676
dataSize = 0;
674677
else
675-
dataSize -= delta;
678+
dataSize -= static_cast<FB_SIZE_T>(delta);
676679
}
677680

678681
if (wLength) {

0 commit comments

Comments
 (0)