Skip to content

Commit

Permalink
Enable -Wconversion in lib/dnssd. (#25328)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 23, 2023
1 parent fb6f25e commit 1062079
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/minimal-mdns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -58,7 +58,7 @@ class TargetVideoPlayerInfo
CHIP_ERROR Initialize(chip::NodeId nodeId, chip::FabricIndex fabricIndex,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> 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<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure);
Expand Down Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CASEClientPool<CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS> gCASEClientPool;
CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIndex,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> 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),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/Advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BaseAdvertisingParams
mPort = port;
return *reinterpret_cast<Derived *>(this);
}
uint64_t GetPort() const { return mPort; }
uint16_t GetPort() const { return mPort; }

Derived & SetInterfaceId(Inet::InterfaceId interfaceId)
{
Expand Down
16 changes: 9 additions & 7 deletions src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(snprintf(storage.sleepyIdleIntervalBuf, sizeof(storage.sleepyIdleIntervalBuf),
"SII=%" PRIu32, mrp.mIdleRetransTimeout.count()));
VerifyOrReturnError((writtenCharactersNumber > 0) &&
(writtenCharactersNumber < sizeof(storage.sleepyIdleIntervalBuf)),
CHIP_ERROR_INVALID_STRING_LENGTH);
Expand All @@ -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<size_t>(snprintf(storage.sleepyActiveIntervalBuf, sizeof(storage.sleepyActiveIntervalBuf),
"SAI=%" PRIu32, mrp.mActiveRetransTimeout.count()));
VerifyOrReturnError((writtenCharactersNumber > 0) &&
(writtenCharactersNumber < sizeof(storage.sleepyActiveIntervalBuf)),
CHIP_ERROR_INVALID_STRING_LENGTH);
Expand All @@ -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<size_t>(
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;
Expand All @@ -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] = {
"=",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/dnssd/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ static_library("dnssd") {
} else {
assert(false, "Unknown Dnssd advertiser implementation.")
}

cflags = [ "-Wconversion" ]
}
15 changes: 7 additions & 8 deletions src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down Expand Up @@ -190,7 +189,7 @@ struct CommissionNodeData
}
if (deviceType > 0)
{
ChipLogDetail(Discovery, "\tDevice Type: %u", deviceType);
ChipLogDetail(Discovery, "\tDevice Type: %" PRIu32, deviceType);
}
if (longDiscriminator > 0)
{
Expand Down
12 changes: 6 additions & 6 deletions src/lib/dnssd/ServiceNaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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:
Expand Down Expand Up @@ -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<size_t>(requiredSize),
Encoding::HexFlags::kUppercaseAndNullTerminate);
break;
case DiscoveryFilterType::kInstanceName:
Expand All @@ -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<size_t>(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)
Expand Down Expand Up @@ -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<size_t>(requiredSize) <= (bufferLen - 1)) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY;
}

} // namespace Dnssd
Expand Down
9 changes: 6 additions & 3 deletions src/lib/dnssd/TxtFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>

#include <lib/core/CHIPSafeCasts.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/BytesToHex.h>
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<int>(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<char>(value.data()[i]) == '+')
{
Expand Down
4 changes: 4 additions & 0 deletions src/lib/dnssd/minimal_mdns/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -156,4 +158,6 @@ static_library("minimal_mdns") {
}

public_configs = [ ":config" ]

cflags = [ "-Wconversion" ]
}
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool QueryData::Append(HeaderRef & hdr, RecordWriter & out) const

out.WriteQName(GetName())
.Put16(static_cast<uint16_t>(mType))
.Put16(static_cast<uint16_t>(mClass) | (mAnswerViaUnicast ? kQClassUnicastAnswerFlag : 0));
.Put16(static_cast<uint16_t>(static_cast<uint16_t>(mClass) | (mAnswerViaUnicast ? kQClassUnicastAnswerFlag : 0)));

if (!out.Fit())
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/ResponseSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/dnssd/minimal_mdns/ResponseSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }

Expand All @@ -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;
};
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/core/QName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool SerializedQNameIterator::Next(bool followIndirectPointers)
return false;
}

size_t offset = ((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1);
size_t offset = static_cast<size_t>(((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1));
if (offset > mLookBehindMax)
{
// Potential infinite recursion.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/core/RecordWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void RecordWriter::RememberWrittenQnameOffset(size_t offset)
{
if (previousName == kInvalidOffset)
{
previousName = offset;
previousName = static_cast<unsigned short>(offset);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/core/RecordWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class RecordWriter
{
if (previous == name)
{
return chip::Optional<uint16_t>::Value(previous.OffsetInCurrentValidData());
return chip::MakeOptional(static_cast<uint16_t>(previous.OffsetInCurrentValidData()));
}

if (!previous.Next())
Expand Down

0 comments on commit 1062079

Please sign in to comment.