Skip to content

Commit

Permalink
Fix null pointer passed to non-null argument in CHIPMemString.h
Browse files Browse the repository at this point in the history
when PI= is in mDNS TXT record (its value is empty) , and Dnssd::Internal::GetPairingInstruction calls CopyString, source is an empty bytespan and source.data() will return a null pointer, that will be passed to memcpy
Fix: avoid memcpy in that case.
  • Loading branch information
Alami-Amine committed Sep 15, 2024
1 parent 2fdc72b commit 5ac7d19
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 @@ -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

0 comments on commit 5ac7d19

Please sign in to comment.