From 046100bb9aacb120111208564fc5a6b2e6a1b282 Mon Sep 17 00:00:00 2001 From: Leonard Zgrablic <32855262+lzgrablic02@users.noreply.github.com> Date: Tue, 15 Mar 2022 17:49:58 -0400 Subject: [PATCH] Ensure null-termination in Darwin DNS SD (#16043) * 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 * 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 --- src/platform/Darwin/DnssdImpl.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/platform/Darwin/DnssdImpl.h b/src/platform/Darwin/DnssdImpl.h index 0c002ba9de9dd6..a23470c232c638 100644 --- a/src/platform/Darwin/DnssdImpl.h +++ b/src/platform/Darwin/DnssdImpl.h @@ -19,6 +19,9 @@ #include #include +#include + +#include #include #include @@ -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); } @@ -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); } }; @@ -105,7 +110,8 @@ struct GetAddrInfoContext : public GenericContext callback = cb; interfaceId = cbInterfaceId; port = cbContextPort; - strncpy(name, cbContextName, sizeof(name)); + + Platform::CopyString(name, cbContextName); } };