From 1062079881f3ecfa91175138e7db0f3311f3712a Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 27 Feb 2023 13:34:05 -0500 Subject: [PATCH] Enable -Wconversion in lib/dnssd. (#25328) --- examples/minimal-mdns/server.cpp | 2 +- .../tv-casting-common/include/CastingServer.h | 2 +- .../include/TargetVideoPlayerInfo.h | 6 +++--- .../src/TargetVideoPlayerInfo.cpp | 2 +- src/lib/dnssd/Advertiser.h | 2 +- src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp | 16 +++++++++------- src/lib/dnssd/BUILD.gn | 2 ++ src/lib/dnssd/Resolver.h | 15 +++++++-------- src/lib/dnssd/ServiceNaming.cpp | 12 ++++++------ src/lib/dnssd/TxtFields.cpp | 9 ++++++--- src/lib/dnssd/minimal_mdns/BUILD.gn | 4 ++++ src/lib/dnssd/minimal_mdns/Parser.cpp | 2 +- src/lib/dnssd/minimal_mdns/ResponseSender.cpp | 2 +- src/lib/dnssd/minimal_mdns/ResponseSender.h | 8 ++++---- src/lib/dnssd/minimal_mdns/core/QName.cpp | 2 +- src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp | 2 +- src/lib/dnssd/minimal_mdns/core/RecordWriter.h | 2 +- 17 files changed, 50 insertions(+), 40 deletions(-) diff --git a/examples/minimal-mdns/server.cpp b/examples/minimal-mdns/server.cpp index 1bee06f8773b3b..44c03c784c4ab9 100644 --- a/examples/minimal-mdns/server.cpp +++ b/examples/minimal-mdns/server.cpp @@ -165,7 +165,7 @@ class ReplyDelegate : public mdns::Minimal::ServerDelegate, public mdns::Minimal mdns::Minimal::ResponseSender * mResponder; const Inet::IPPacketInfo * mCurrentSource = nullptr; - uint32_t mMessageId = 0; + uint16_t mMessageId = 0; }; mdns::Minimal::Server<10 /* endpoints */> gMdnsServer; diff --git a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h index f67b4d9df93456..60aea4867d18f2 100644 --- a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h +++ b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h @@ -436,7 +436,7 @@ class CastingServer TargetVideoPlayerInfo mCachedTargetVideoPlayerInfo[kMaxCachedVideoPlayers]; uint16_t mTargetVideoPlayerVendorId = 0; uint16_t mTargetVideoPlayerProductId = 0; - uint16_t mTargetVideoPlayerDeviceType = 0; + chip::DeviceTypeId mTargetVideoPlayerDeviceType = 0; char mTargetVideoPlayerDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {}; size_t mTargetVideoPlayerNumIPs = 0; // number of valid IP addresses chip::Inet::IPAddress mTargetVideoPlayerIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses]; diff --git a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h index f98116f3c7ca27..26e939346d9b32 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -37,7 +37,7 @@ class TargetVideoPlayerInfo bool IsInitialized() { return mInitialized; } uint16_t GetVendorId() const { return mVendorId; } uint16_t GetProductId() const { return mProductId; } - uint16_t GetDeviceType() const { return mDeviceType; } + chip::DeviceTypeId GetDeviceType() const { return mDeviceType; } chip::NodeId GetNodeId() const { return mNodeId; } chip::FabricIndex GetFabricIndex() const { return mFabricIndex; } const char * GetDeviceName() const { return mDeviceName; } @@ -58,7 +58,7 @@ class TargetVideoPlayerInfo CHIP_ERROR Initialize(chip::NodeId nodeId, chip::FabricIndex fabricIndex, std::function onConnectionSuccess, std::function onConnectionFailure, uint16_t vendorId = 0, uint16_t productId = 0, - uint16_t deviceType = 0, const char * deviceName = {}, size_t numIPs = 0, + chip::DeviceTypeId deviceType = 0, const char * deviceName = {}, size_t numIPs = 0, chip::Inet::IPAddress * ipAddressList = nullptr); CHIP_ERROR FindOrEstablishCASESession(std::function onConnectionSuccess, std::function onConnectionFailure); @@ -108,7 +108,7 @@ class TargetVideoPlayerInfo chip::OperationalDeviceProxy mDeviceProxy; uint16_t mVendorId = 0; uint16_t mProductId = 0; - uint16_t mDeviceType = 0; + chip::DeviceTypeId mDeviceType = 0; char mDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {}; size_t mNumIPs = 0; // number of valid IP addresses chip::Inet::IPAddress mIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses]; diff --git a/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp b/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp index f21fde3883256a..bff3bb2fa8380b 100644 --- a/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp @@ -26,7 +26,7 @@ CASEClientPool gCASEClientPool; CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIndex, std::function onConnectionSuccess, std::function onConnectionFailure, uint16_t vendorId, - uint16_t productId, uint16_t deviceType, const char * deviceName, size_t numIPs, + uint16_t productId, DeviceTypeId deviceType, const char * deviceName, size_t numIPs, chip::Inet::IPAddress * ipAddress) { ChipLogProgress(NotSpecified, "TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabricIndex=%d", ChipLogValueX64(nodeId), diff --git a/src/lib/dnssd/Advertiser.h b/src/lib/dnssd/Advertiser.h index 710fc64bc4596f..45cd072c1db11c 100644 --- a/src/lib/dnssd/Advertiser.h +++ b/src/lib/dnssd/Advertiser.h @@ -63,7 +63,7 @@ class BaseAdvertisingParams mPort = port; return *reinterpret_cast(this); } - uint64_t GetPort() const { return mPort; } + uint16_t GetPort() const { return mPort; } Derived & SetInterfaceId(Inet::InterfaceId interfaceId) { diff --git a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp index 47ee3b10e7d073..679c8bae7d3e48 100644 --- a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp @@ -236,8 +236,9 @@ class AdvertiserMinMdns : public ServiceAdvertiser, "MRP retry interval idle value exceeds allowed range of 1 hour, using maximum available"); mrp.mIdleRetransTimeout = kMaxRetryInterval; } - size_t writtenCharactersNumber = snprintf(storage.sleepyIdleIntervalBuf, sizeof(storage.sleepyIdleIntervalBuf), - "SII=%" PRIu32, mrp.mIdleRetransTimeout.count()); + size_t writtenCharactersNumber = + static_cast(snprintf(storage.sleepyIdleIntervalBuf, sizeof(storage.sleepyIdleIntervalBuf), + "SII=%" PRIu32, mrp.mIdleRetransTimeout.count())); VerifyOrReturnError((writtenCharactersNumber > 0) && (writtenCharactersNumber < sizeof(storage.sleepyIdleIntervalBuf)), CHIP_ERROR_INVALID_STRING_LENGTH); @@ -252,8 +253,9 @@ class AdvertiserMinMdns : public ServiceAdvertiser, "MRP retry interval active value exceeds allowed range of 1 hour, using maximum available"); mrp.mActiveRetransTimeout = kMaxRetryInterval; } - size_t writtenCharactersNumber = snprintf(storage.sleepyActiveIntervalBuf, sizeof(storage.sleepyActiveIntervalBuf), - "SAI=%" PRIu32, mrp.mActiveRetransTimeout.count()); + size_t writtenCharactersNumber = + static_cast(snprintf(storage.sleepyActiveIntervalBuf, sizeof(storage.sleepyActiveIntervalBuf), + "SAI=%" PRIu32, mrp.mActiveRetransTimeout.count())); VerifyOrReturnError((writtenCharactersNumber > 0) && (writtenCharactersNumber < sizeof(storage.sleepyActiveIntervalBuf)), CHIP_ERROR_INVALID_STRING_LENGTH); @@ -262,8 +264,8 @@ class AdvertiserMinMdns : public ServiceAdvertiser, } if (params.GetTcpSupported().HasValue()) { - size_t writtenCharactersNumber = - snprintf(storage.tcpSupportedBuf, sizeof(storage.tcpSupportedBuf), "T=%d", params.GetTcpSupported().Value()); + size_t writtenCharactersNumber = static_cast( + snprintf(storage.tcpSupportedBuf, sizeof(storage.tcpSupportedBuf), "T=%d", params.GetTcpSupported().Value())); VerifyOrReturnError((writtenCharactersNumber > 0) && (writtenCharactersNumber < sizeof(storage.tcpSupportedBuf)), CHIP_ERROR_INVALID_STRING_LENGTH); txtFields[numTxtFields++] = storage.tcpSupportedBuf; @@ -290,7 +292,7 @@ class AdvertiserMinMdns : public ServiceAdvertiser, // current request handling const chip::Inet::IPPacketInfo * mCurrentSource = nullptr; - uint32_t mMessageId = 0; + uint16_t mMessageId = 0; const char * mEmptyTextEntries[1] = { "=", diff --git a/src/lib/dnssd/BUILD.gn b/src/lib/dnssd/BUILD.gn index e723dd54466600..b33b304f195df6 100644 --- a/src/lib/dnssd/BUILD.gn +++ b/src/lib/dnssd/BUILD.gn @@ -69,4 +69,6 @@ static_library("dnssd") { } else { assert(false, "Unknown Dnssd advertiser implementation.") } + + cflags = [ "-Wconversion" ] } diff --git a/src/lib/dnssd/Resolver.h b/src/lib/dnssd/Resolver.h index 9f4698a40db563..5f48378bd01d4e 100644 --- a/src/lib/dnssd/Resolver.h +++ b/src/lib/dnssd/Resolver.h @@ -149,13 +149,12 @@ struct CommissionNodeData uint16_t vendorId = 0; uint16_t productId = 0; uint8_t commissioningMode = 0; - // TODO: possibly 32-bit - see spec issue #3226 - uint16_t deviceType = 0; - char deviceName[kMaxDeviceNameLen + 1] = {}; - uint8_t rotatingId[kMaxRotatingIdLen] = {}; - size_t rotatingIdLen = 0; - uint16_t pairingHint = 0; - char pairingInstruction[kMaxPairingInstructionLen + 1] = {}; + uint32_t deviceType = 0; + char deviceName[kMaxDeviceNameLen + 1] = {}; + uint8_t rotatingId[kMaxRotatingIdLen] = {}; + size_t rotatingIdLen = 0; + uint16_t pairingHint = 0; + char pairingInstruction[kMaxPairingInstructionLen + 1] = {}; CommissionNodeData() {} @@ -190,7 +189,7 @@ struct CommissionNodeData } if (deviceType > 0) { - ChipLogDetail(Discovery, "\tDevice Type: %u", deviceType); + ChipLogDetail(Discovery, "\tDevice Type: %" PRIu32, deviceType); } if (longDiscriminator > 0) { diff --git a/src/lib/dnssd/ServiceNaming.cpp b/src/lib/dnssd/ServiceNaming.cpp index 16aedd8211707a..25bdfbf2cbf341 100644 --- a/src/lib/dnssd/ServiceNaming.cpp +++ b/src/lib/dnssd/ServiceNaming.cpp @@ -84,7 +84,7 @@ CHIP_ERROR MakeHostName(char * buffer, size_t bufferLen, const chip::ByteSpan & { ReturnErrorCodeIf(bufferLen < macOrEui64.size() * 2 + 1, CHIP_ERROR_BUFFER_TOO_SMALL); - size_t idx = 0; + int idx = 0; for (size_t i = 0; i < macOrEui64.size(); ++i) { idx += snprintf(buffer + idx, 3, "%02X", macOrEui64.data()[i]); @@ -94,7 +94,7 @@ CHIP_ERROR MakeHostName(char * buffer, size_t bufferLen, const chip::ByteSpan & CHIP_ERROR MakeServiceSubtype(char * buffer, size_t bufferLen, DiscoveryFilter subtype) { - size_t requiredSize; + int requiredSize; switch (subtype.type) { case DiscoveryFilterType::kShortDiscriminator: @@ -135,7 +135,7 @@ CHIP_ERROR MakeServiceSubtype(char * buffer, size_t bufferLen, DiscoveryFilter s break; case DiscoveryFilterType::kCompressedFabricId: requiredSize = snprintf(buffer, bufferLen, "_I"); - return Encoding::Uint64ToHex(subtype.code, &buffer[requiredSize], bufferLen - requiredSize, + return Encoding::Uint64ToHex(subtype.code, &buffer[requiredSize], bufferLen - static_cast(requiredSize), Encoding::HexFlags::kUppercaseAndNullTerminate); break; case DiscoveryFilterType::kInstanceName: @@ -146,12 +146,12 @@ CHIP_ERROR MakeServiceSubtype(char * buffer, size_t bufferLen, DiscoveryFilter s buffer[0] = '\0'; break; } - return (requiredSize <= (bufferLen - 1)) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY; + return (static_cast(requiredSize) <= (bufferLen - 1)) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY; } CHIP_ERROR MakeServiceTypeName(char * buffer, size_t bufferLen, DiscoveryFilter nameDesc, DiscoveryType type) { - size_t requiredSize; + int requiredSize; if (nameDesc.type == DiscoveryFilterType::kNone) { if (type == DiscoveryType::kCommissionableNode) @@ -192,7 +192,7 @@ CHIP_ERROR MakeServiceTypeName(char * buffer, size_t bufferLen, DiscoveryFilter } } - return (requiredSize <= (bufferLen - 1)) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY; + return (static_cast(requiredSize) <= (bufferLen - 1)) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY; } } // namespace Dnssd diff --git a/src/lib/dnssd/TxtFields.cpp b/src/lib/dnssd/TxtFields.cpp index 33840b9cd2ced5..a09efaaf5aa4b9 100644 --- a/src/lib/dnssd/TxtFields.cpp +++ b/src/lib/dnssd/TxtFields.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -49,9 +50,11 @@ bool IsKey(const ByteSpan & key, const char * desired) { return false; } + + auto desired_bytes = Uint8::from_const_char(desired); for (size_t i = 0; i < key.size(); ++i) { - if (SafeToLower(key.data()[i]) != SafeToLower(desired[i])) + if (SafeToLower(key.data()[i]) != SafeToLower(desired_bytes[i])) { return false; } @@ -104,8 +107,8 @@ bool MakeBoolFromAsciiDecimal(const ByteSpan & val) size_t GetPlusSignIdx(const ByteSpan & value) { - // Fist value is the vendor id, second (after the +) is the product. - for (int i = 0; i < static_cast(value.size()); ++i) + // First value is the vendor id, second (after the +) is the product. + for (size_t i = 0; i < value.size(); ++i) { if (static_cast(value.data()[i]) == '+') { diff --git a/src/lib/dnssd/minimal_mdns/BUILD.gn b/src/lib/dnssd/minimal_mdns/BUILD.gn index 927b881f2aabfc..bf0ae8abef3ae4 100644 --- a/src/lib/dnssd/minimal_mdns/BUILD.gn +++ b/src/lib/dnssd/minimal_mdns/BUILD.gn @@ -92,6 +92,8 @@ static_library("default_policy") { ":address_policy", "${chip_root}/src/inet", ] + + cflags = [ "-Wconversion" ] } if (current_os == "linux" && chip_minmdns_default_policy == "libnl") { @@ -156,4 +158,6 @@ static_library("minimal_mdns") { } public_configs = [ ":config" ] + + cflags = [ "-Wconversion" ] } diff --git a/src/lib/dnssd/minimal_mdns/Parser.cpp b/src/lib/dnssd/minimal_mdns/Parser.cpp index 8bc3e9e1af3bb4..4cd341da977677 100644 --- a/src/lib/dnssd/minimal_mdns/Parser.cpp +++ b/src/lib/dnssd/minimal_mdns/Parser.cpp @@ -75,7 +75,7 @@ bool QueryData::Append(HeaderRef & hdr, RecordWriter & out) const out.WriteQName(GetName()) .Put16(static_cast(mType)) - .Put16(static_cast(mClass) | (mAnswerViaUnicast ? kQClassUnicastAnswerFlag : 0)); + .Put16(static_cast(static_cast(mClass) | (mAnswerViaUnicast ? kQClassUnicastAnswerFlag : 0))); if (!out.Fit()) { diff --git a/src/lib/dnssd/minimal_mdns/ResponseSender.cpp b/src/lib/dnssd/minimal_mdns/ResponseSender.cpp index ef530355c02ed3..722ca973902fb3 100644 --- a/src/lib/dnssd/minimal_mdns/ResponseSender.cpp +++ b/src/lib/dnssd/minimal_mdns/ResponseSender.cpp @@ -100,7 +100,7 @@ bool ResponseSender::HasQueryResponders() const return false; } -CHIP_ERROR ResponseSender::Respond(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource, +CHIP_ERROR ResponseSender::Respond(uint16_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource, const ResponseConfiguration & configuration) { mSendState.Reset(messageId, query, querySource); diff --git a/src/lib/dnssd/minimal_mdns/ResponseSender.h b/src/lib/dnssd/minimal_mdns/ResponseSender.h index cf1b80f5d97f65..636acaaf7acf14 100644 --- a/src/lib/dnssd/minimal_mdns/ResponseSender.h +++ b/src/lib/dnssd/minimal_mdns/ResponseSender.h @@ -53,7 +53,7 @@ class ResponseSendingState public: ResponseSendingState() {} - void Reset(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * packet) + void Reset(uint16_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * packet) { mMessageId = messageId; mQuery = &query; @@ -72,7 +72,7 @@ class ResponseSendingState } CHIP_ERROR GetError() const { return mSendError; } - uint32_t GetMessageId() const { return mMessageId; } + uint16_t GetMessageId() const { return mMessageId; } const QueryData * GetQuery() const { return mQuery; } @@ -91,7 +91,7 @@ class ResponseSendingState private: const QueryData * mQuery = nullptr; // query being replied to const chip::Inet::IPPacketInfo * mSource = nullptr; // Where to send the reply (if unicast) - uint32_t mMessageId = 0; // message id for the reply + uint16_t mMessageId = 0; // message id for the reply ResourceType mResourceType = ResourceType::kAnswer; // what is being sent right now CHIP_ERROR mSendError = CHIP_NO_ERROR; }; @@ -112,7 +112,7 @@ class ResponseSender : public ResponderDelegate bool HasQueryResponders() const; /// Send back the response to a particular query - CHIP_ERROR Respond(uint32_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource, + CHIP_ERROR Respond(uint16_t messageId, const QueryData & query, const chip::Inet::IPPacketInfo * querySource, const ResponseConfiguration & configuration); // Implementation of ResponderDelegate diff --git a/src/lib/dnssd/minimal_mdns/core/QName.cpp b/src/lib/dnssd/minimal_mdns/core/QName.cpp index 94a15bb4e8bba8..2cc6488cfb1503 100644 --- a/src/lib/dnssd/minimal_mdns/core/QName.cpp +++ b/src/lib/dnssd/minimal_mdns/core/QName.cpp @@ -60,7 +60,7 @@ bool SerializedQNameIterator::Next(bool followIndirectPointers) return false; } - size_t offset = ((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1); + size_t offset = static_cast(((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1)); if (offset > mLookBehindMax) { // Potential infinite recursion. diff --git a/src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp b/src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp index c03bb32990506f..0249d80d6e35ff 100644 --- a/src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp +++ b/src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp @@ -135,7 +135,7 @@ void RecordWriter::RememberWrittenQnameOffset(size_t offset) { if (previousName == kInvalidOffset) { - previousName = offset; + previousName = static_cast(offset); return; } } diff --git a/src/lib/dnssd/minimal_mdns/core/RecordWriter.h b/src/lib/dnssd/minimal_mdns/core/RecordWriter.h index 0356f7e4b2643e..35e563fe870ec2 100644 --- a/src/lib/dnssd/minimal_mdns/core/RecordWriter.h +++ b/src/lib/dnssd/minimal_mdns/core/RecordWriter.h @@ -110,7 +110,7 @@ class RecordWriter { if (previous == name) { - return chip::Optional::Value(previous.OffsetInCurrentValidData()); + return chip::MakeOptional(static_cast(previous.OffsetInCurrentValidData())); } if (!previous.Next())