Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing UBSan issues that showed up in all-clusters app #35580

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 12 additions & 4 deletions src/lib/support/CHIPMemString.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@ inline void CopyString(char (&dest)[N], const char * source)
*/
inline void CopyString(char * dest, size_t destLength, ByteSpan source)
{
if (dest && destLength)
if ((dest == nullptr) || (destLength == 0))
{
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
return; // no space to copy anything, not even a null terminator
}

if (source.empty())
{
*dest = '\0'; // just a null terminator, we are copying empty data
return;
}

size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
}

/**
Expand Down
Loading