Skip to content

Commit

Permalink
Fix null pointer issue in CHIPMemString
Browse files Browse the repository at this point in the history
  • Loading branch information
BoB13-Matter committed Sep 30, 2024
1 parent 6bbe1d6 commit c32fc4d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/support/CHIPMemString.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,20 @@ inline void CopyString(char (&dest)[N], ByteSpan source)
*/
inline void CopyString(char * dest, size_t destLength, CharSpan 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

0 comments on commit c32fc4d

Please sign in to comment.