Skip to content

Commit

Permalink
integrating suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Sep 13, 2024
1 parent 512efaf commit c9ccaff
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/lib/support/CHIPMemString.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +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))
{
if (!source.empty())
{
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
}
else
{
dest[0] = '\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 c9ccaff

Please sign in to comment.