From 35116188fd770e54f164a7735090df6cfbf27bd1 Mon Sep 17 00:00:00 2001 From: C Freeman Date: Tue, 3 Oct 2023 11:20:11 -0400 Subject: [PATCH] CastingDiscovery: fix snprintf warning (#29534) * CastingDiscovery: fix snprintf warning snprintf appends a null terminator, as does strcat, so both buffers need to be upsized by 1 to accommodate that. * ID buffer size was also incorrect * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../tv-casting-app/tv-casting-common/core/CastingPlayer.h | 5 +++-- .../tv-casting-common/core/CastingPlayerDiscovery.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h index c523fb111b0b48..977642790e60e6 100644 --- a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h +++ b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h @@ -40,12 +40,13 @@ using ConnectCallback = std::function; using DisconnectCallback = std::function; const int kPortMaxLength = 5; // port is uint16_t -const int kIdMaxLength = chip::Dnssd::kHostNameMaxLength + kPortMaxLength; +// +1 for the : between the hostname and the port. +const int kIdMaxLength = chip::Dnssd::kHostNameMaxLength + kPortMaxLength + 1; class CastingPlayerAttributes { public: - char id[kIdMaxLength] = {}; + char id[kIdMaxLength + 1] = {}; char deviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {}; char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {}; char instanceName[chip::Dnssd::kHostNameMaxLength + 1] = {}; diff --git a/examples/tv-casting-app/tv-casting-common/core/CastingPlayerDiscovery.cpp b/examples/tv-casting-app/tv-casting-common/core/CastingPlayerDiscovery.cpp index 65a589932c1a4b..e391116c0ef1f7 100644 --- a/examples/tv-casting-app/tv-casting-common/core/CastingPlayerDiscovery.cpp +++ b/examples/tv-casting-app/tv-casting-common/core/CastingPlayerDiscovery.cpp @@ -81,7 +81,7 @@ void DeviceDiscoveryDelegateImpl::OnDiscoveredDevice(const chip::Dnssd::Discover CastingPlayerAttributes attributes; strcpy(attributes.id, nodeData.resolutionData.hostName); - char port[kPortMaxLength] = {}; + char port[kPortMaxLength + 1] = {}; snprintf(port, sizeof(port), "%u", nodeData.resolutionData.port); strcat(attributes.id, port);