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

Cleanup darwin usage of strtok and strncpy in DNS implementation #11934

Merged
merged 10 commits into from
Nov 18, 2021
21 changes: 12 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,7 @@ 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);
Platform::CopyString(value, reinterpret_cast<const char *>(valuePtr));
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
value[valueLen] = 0;

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