Skip to content

Commit 557bdf9

Browse files
committed
Fix null pointer passed to non-null argument in CHIPMemString.h
Error Message: CHIPMemString.h:88:22: runtime error: null pointer passed as argument 2, which is declared to never be null 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.
1 parent 88031e6 commit 557bdf9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/lib/support/CHIPMemString.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@ inline void CopyString(char (&dest)[N], const char * source)
8282
*/
8383
inline void CopyString(char * dest, size_t destLength, ByteSpan source)
8484
{
85-
if (dest && destLength)
85+
if ((dest == nullptr) || (destLength == 0))
8686
{
87-
size_t maxChars = std::min(destLength - 1, source.size());
88-
memcpy(dest, source.data(), maxChars);
89-
dest[maxChars] = '\0';
87+
return; // no space to copy anything, not even a null terminator
9088
}
89+
90+
if (source.empty())
91+
{
92+
*dest = '\0'; // just a null terminator, we are copying empty data
93+
return;
94+
}
95+
96+
size_t maxChars = std::min(destLength - 1, source.size());
97+
memcpy(dest, source.data(), maxChars);
98+
dest[maxChars] = '\0';
9199
}
92100

93101
/**

0 commit comments

Comments
 (0)