Skip to content

Commit

Permalink
Enable -Wconversion in lib/dnssd.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Feb 26, 2023
1 parent 3b80154 commit 701847a
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 25 deletions.
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
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" ]
}
13 changes: 6 additions & 7 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
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 @@ -67,6 +67,8 @@ config("config") {
assert(chip_minmdns_default_policy == "none",
"minmdns default policy should be a supported value")
}

cflags = [ "-Wconversion" ]
}

source_set("address_policy") {
Expand All @@ -92,6 +94,8 @@ static_library("default_policy") {
":address_policy",
"${chip_root}/src/inet",
]

cflags = [ "-Wconversion" ]
}

if (current_os == "linux" && chip_minmdns_default_policy == "libnl") {
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 701847a

Please sign in to comment.