Skip to content

Commit

Permalink
CastingDiscovery: fix snprintf warning (#29534)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jan 18, 2024
1 parent 6297644 commit 3511618
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ using ConnectCallback = std::function<void(ConnectionError)>;
using DisconnectCallback = std::function<void(void)>;

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] = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 3511618

Please sign in to comment.