Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Cleanup darwin usage of strtok and strncpy in DNS implementation #11936

Closed
wants to merge 8 commits into from
29 changes: 20 additions & 9 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,22 @@ void OnBrowseAdd(BrowseContext * context, const char * name, const char * type,

VerifyOrReturn(strcmp(kLocalDot, domain) == 0);

char * tokens = strdup(type);
char * regtype = strtok(tokens, ".");
free(tokens);

DnssdService service = {};
service.mInterface = interfaceId;
service.mProtocol = context->protocol;

strncpy(service.mName, name, sizeof(service.mName));
service.mName[Common::kInstanceNameMaxLength] = 0;
Platform::CopyString(service.mName, name);
Platform::CopyString(service.mType, type);

strncpy(service.mType, regtype, sizeof(service.mType));
service.mType[kDnssdTypeMaxSize] = 0;
// only the first token after '.' should be included in the type
for (char * p = service.mType; *p != '\0'; p++)
{
if (*p == '.')
{
*p = '\0';
break;
}
}

context->services.push_back(service);
}
Expand Down Expand Up @@ -426,7 +429,15 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
err = TXTRecordGetItemAtIndex(txtLen, txtRecord, i, kDnssdKeyMaxSize, key, &valueLen, &valuePtr);
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);

strncpy(value, reinterpret_cast<const char *>(valuePtr), valueLen);
if (valueLen >= sizeof(value))
{
// Truncation, but nothing better we can do
valueLen = sizeof(value) - 1;
}
if (valueLen > 1)
{
memcpy(value, valuePtr, valueLen - 1);
}
value[valueLen] = 0;

sdCtx->textEntries.push_back(TextEntry{ strdup(key), reinterpret_cast<const uint8_t *>(strdup(value)), valueLen });
Expand Down