Skip to content

Commit

Permalink
Fix for unsigned integer overflow:
Browse files Browse the repository at this point in the history
Error Message: BufferWriter.cpp:76:16: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'size_t' (aka 'unsigned long')

when size becomes 0 and we are in while (size-- > 0), then size will become less than 0, which should be avoided since it is an unsigned (although technically it wraps around, UBSAN complains about it).
Fix: stop size from decrement past 0, by moving size-- inside the loop
  • Loading branch information
Alami-Amine committed Sep 15, 2024
1 parent 7a54490 commit 88031e6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lib/support/BufferWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,19 @@ LittleEndian::BufferWriter & LittleEndian::BufferWriter::EndianPutSigned(int64_t

BigEndian::BufferWriter & BigEndian::BufferWriter::EndianPut(uint64_t x, size_t size)
{
while (size-- > 0)
while (size > 0)
{
size--;
Put(static_cast<uint8_t>((x >> (size * 8)) & 0xff));
}
return *this;
}

BigEndian::BufferWriter & BigEndian::BufferWriter::EndianPutSigned(int64_t x, size_t size)
{
while (size-- > 0)
while (size > 0)
{
size--;
Put(static_cast<uint8_t>((x >> (size * 8)) & 0xff));
}
return *this;
Expand Down

0 comments on commit 88031e6

Please sign in to comment.