Skip to content

Commit

Permalink
Ensure null-termination in Darwin DNS SD (#16043)
Browse files Browse the repository at this point in the history
* Ensure null-termination in Darwin DNS SD

Null-termination was not ensured after strncpy.

* Rearrange style

* Apply suggestions from code review

Use CopyString instead of strncpy.

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>

* Add missing header for Darwin DnssdImpl

* Use C header for strcmp instead of C++

In src/platform/Darwin/DnssdImpl.h.

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
  • Loading branch information
lzgrablic02 and bzbarsky-apple authored Mar 15, 2022
1 parent b4fd95f commit 046100b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/platform/Darwin/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include <dns_sd.h>
#include <lib/dnssd/platform/Dnssd.h>
#include <lib/support/CHIPMemString.h>

#include <string.h>
#include <string>
#include <vector>

Expand Down Expand Up @@ -47,10 +50,11 @@ struct RegisterContext : public GenericContext

RegisterContext(const char * sType, DnssdPublishCallback cb, void * cbContext)
{
type = ContextType::Register;
strncpy(mType, sType, sizeof(mType));
type = ContextType::Register;
context = cbContext;
callback = cb;

Platform::CopyString(mType, sType);
}

bool matches(const char * sType) { return (strcmp(mType, sType) == 0); }
Expand Down Expand Up @@ -80,11 +84,12 @@ struct ResolveContext : public GenericContext

ResolveContext(void * cbContext, DnssdResolveCallback cb, const char * cbContextName, chip::Inet::IPAddressType cbAddressType)
{
type = ContextType::Resolve;
context = cbContext;
callback = cb;
strncpy(name, cbContextName, sizeof(name));
type = ContextType::Resolve;
context = cbContext;
callback = cb;
addressType = cbAddressType;

Platform::CopyString(name, cbContextName);
}
};

Expand All @@ -105,7 +110,8 @@ struct GetAddrInfoContext : public GenericContext
callback = cb;
interfaceId = cbInterfaceId;
port = cbContextPort;
strncpy(name, cbContextName, sizeof(name));

Platform::CopyString(name, cbContextName);
}
};

Expand Down

0 comments on commit 046100b

Please sign in to comment.