Skip to content

Commit

Permalink
Fix null pointer issue in CHIPMemString (#35840)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoB13-Matter authored Sep 30, 2024
1 parent 58e7da1 commit d1bb9df
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 d1bb9df

Please sign in to comment.