Skip to content

Commit

Permalink
fixing ubsan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Sep 13, 2024
1 parent 7a54490 commit 512efaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 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
13 changes: 10 additions & 3 deletions src/lib/support/CHIPMemString.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,16 @@ inline void CopyString(char * dest, size_t destLength, ByteSpan source)
{
if (dest && destLength)
{
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
if (!source.empty())
{
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
}
else
{
dest[0] = '\0';
}
}
}

Expand Down

0 comments on commit 512efaf

Please sign in to comment.