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 27, 2023
1 parent 3b80154 commit 2c2ac9b
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 66 deletions.
6 changes: 6 additions & 0 deletions examples/common/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ source_set("trace_handlers") {
deps = [ "${chip_root}/src/lib" ]

public_configs = [ ":default_config" ]

cflags = [ "-Wconversion" ]
}

source_set("trace_handlers_decoder") {
Expand All @@ -47,6 +49,8 @@ source_set("trace_handlers_decoder") {

deps = [ "${chip_root}/src/lib" ]
public_deps = [ "${chip_root}/third_party/jsoncpp" ]

cflags = [ "-Wconversion" ]
}

executable("chip-trace-decoder") {
Expand All @@ -73,4 +77,6 @@ executable("chip-trace-decoder") {
"${chip_root}/src/lib",
"${chip_root}/third_party/jsoncpp",
]

cflags = [ "-Wconversion" ]
}
21 changes: 12 additions & 9 deletions examples/common/tracing/TraceDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ CHIP_ERROR TraceDecoder::ReadString(const char * str)
CHIP_ERROR TraceDecoder::LogJSON(Json::Value & json)
{
auto protocol = json[kProtocolIdKey].asLargestUInt();
uint16_t vendorId = protocol >> 16;
uint16_t vendorId = static_cast<uint16_t>(protocol >> 16);
uint16_t protocolId = protocol & 0xFFFF;
if (!mOptions.IsProtocolEnabled(chip::Protocols::Id(chip::VendorId(vendorId), protocolId)))
{
Expand Down Expand Up @@ -213,7 +213,7 @@ CHIP_ERROR TraceDecoder::LogAndConsumeProtocol(Json::Value & json)
auto id = json[kProtocolIdKey].asLargestUInt();
auto opcode = static_cast<uint8_t>(json[kProtocolCodeKey].asLargestUInt());

uint16_t vendorId = (id >> 16);
uint16_t vendorId = static_cast<uint16_t>(id >> 16);
uint16_t protocolId = (id & 0xFFFF);

chip::StringBuilderBase builder(protocolInfo, sizeof(protocolInfo));
Expand All @@ -228,15 +228,15 @@ CHIP_ERROR TraceDecoder::LogAndConsumeProtocol(Json::Value & json)
builder.Add(protocolDetail);

builder.Add(" [");
builder.Add(ToProtocolName(id));
builder.Add(ToProtocolName(vendorId, protocolId));

builder.Add(" ");
memset(protocolDetail, '\0', sizeof(protocolDetail));
snprintf(protocolDetail, sizeof(protocolDetail), "(%u)", protocolId);
builder.Add(protocolDetail);

builder.Add(" / ");
builder.Add(ToProtocolMessageTypeName(id, opcode));
builder.Add(ToProtocolMessageTypeName(vendorId, protocolId, opcode));

builder.Add(" ");
memset(protocolDetail, '\0', sizeof(protocolDetail));
Expand Down Expand Up @@ -278,11 +278,14 @@ CHIP_ERROR TraceDecoder::MaybeLogAndConsumePayload(Json::Value & json, bool isRe
Log("data", payload.c_str());
}

bool shouldDecode = !isResponse || mOptions.mEnableProtocolInteractionModelResponse;
auto payload = json[kPayloadDataKey].asString();
auto protocolId = json[kProtocolIdKey].asLargestUInt();
auto protocolCode = json[kProtocolCodeKey].asLargestUInt();
ReturnErrorOnFailure(LogAsProtocolMessage(protocolId, protocolCode, payload.c_str(), payload.size(), shouldDecode));
bool shouldDecode = !isResponse || mOptions.mEnableProtocolInteractionModelResponse;
auto payload = json[kPayloadDataKey].asString();
auto id = json[kProtocolIdKey].asLargestUInt();
uint16_t vendorId = static_cast<uint16_t>(id >> 16);
uint16_t protocolId = (id & 0xFFFF);
auto protocolCode = static_cast<uint8_t>(json[kProtocolCodeKey].asLargestUInt());
ReturnErrorOnFailure(
LogAsProtocolMessage(vendorId, protocolId, protocolCode, payload.c_str(), payload.size(), shouldDecode));
Log(" ");
}

Expand Down
12 changes: 6 additions & 6 deletions examples/common/tracing/decoder/TraceDecoderProtocols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ void ENFORCE_FORMAT(1, 2) TLVPrettyPrinter(const char * aFormat, ...)
namespace chip {
namespace trace {

const char * ToProtocolName(uint16_t protocolId)
const char * ToProtocolName(uint16_t vendorId, uint16_t protocolId)
{
const char * name = nullptr;

auto protocol = Protocols::Id(VendorId::Common, protocolId);
auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId);
if (protocol == Protocols::SecureChannel::Id)
{
name = secure_channel::ToProtocolName();
Expand Down Expand Up @@ -77,11 +77,11 @@ const char * ToProtocolName(uint16_t protocolId)
return name;
}

const char * ToProtocolMessageTypeName(uint16_t protocolId, uint8_t protocolCode)
const char * ToProtocolMessageTypeName(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode)
{
const char * name = nullptr;

auto protocol = Protocols::Id(VendorId::Common, protocolId);
auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId);
if (protocol == Protocols::SecureChannel::Id)
{
name = secure_channel::ToProtocolMessageTypeName(protocolCode);
Expand Down Expand Up @@ -110,7 +110,7 @@ const char * ToProtocolMessageTypeName(uint16_t protocolId, uint8_t protocolCode
return name;
}

CHIP_ERROR LogAsProtocolMessage(uint16_t protocolId, uint8_t protocolCode, const char * payload, size_t len,
CHIP_ERROR LogAsProtocolMessage(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode, const char * payload, size_t len,
bool interactionModelResponse)
{
constexpr uint16_t kMaxPayloadLen = 2048;
Expand All @@ -120,7 +120,7 @@ CHIP_ERROR LogAsProtocolMessage(uint16_t protocolId, uint8_t protocolCode, const

CHIP_ERROR err = CHIP_NO_ERROR;

auto protocol = Protocols::Id(VendorId::Common, protocolId);
auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId);
if (protocol == Protocols::SecureChannel::Id)
{
err = secure_channel::LogAsProtocolMessage(protocolCode, data, dataLen);
Expand Down
6 changes: 3 additions & 3 deletions examples/common/tracing/decoder/TraceDecoderProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
namespace chip {
namespace trace {

const char * ToProtocolName(uint16_t protocolId);
const char * ToProtocolName(uint16_t vendorId, uint16_t protocolId);

const char * ToProtocolMessageTypeName(uint16_t protocolId, uint8_t protocolCode);
const char * ToProtocolMessageTypeName(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode);

CHIP_ERROR LogAsProtocolMessage(uint16_t protocolId, uint8_t protocolCode, const char * payload, size_t len,
CHIP_ERROR LogAsProtocolMessage(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode, const char * payload, size_t len,
bool interactionModelResponse);

} // namespace trace
Expand Down
4 changes: 2 additions & 2 deletions examples/common/tracing/decoder/logging/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ void ENFORCE_FORMAT(1, 2) LogFormatted(const char * format, ...)
{
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE] = {};

uint8_t indentation = gIndentLevel * kSpacePerIndent;
int indentation = gIndentLevel * kSpacePerIndent;
snprintf(buffer, sizeof(buffer), "%*s", indentation, "");

va_list args;
va_start(args, format);
vsnprintf(&buffer[indentation], sizeof(buffer) - indentation, format, args);
vsnprintf(&buffer[indentation], sizeof(buffer) - static_cast<size_t>(indentation), format, args);
va_end(args);

ChipLogDetail(DataManagement, "%s", buffer);
Expand Down
12 changes: 10 additions & 2 deletions examples/common/tracing/decoder/logging/ToCertificateString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ToCertificateString.h"

#include <lib/support/Base64.h>
#include <lib/support/SafeInt.h>
#include <lib/support/ScopedBuffer.h>

namespace {
Expand All @@ -40,6 +41,12 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
return destination.data();
}

if (!chip::CanCastTo<uint16_t>(source.size()))
{
ChipLogError(DataManagement, "The certificate is too large to do base64 conversion on");
return destination.data();
}

size_t base64DataLen = BASE64_ENCODED_LEN(source.size());
size_t bufferLen = base64DataLen + 1; // add one character for null-terminator
if (bufferLen + strlen(header) + strlen(footer) > destination.size())
Expand All @@ -51,7 +58,7 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
chip::Platform::ScopedMemoryBuffer<char> str;
str.Alloc(bufferLen);

auto encodedLen = chip::Base64Encode(source.data(), source.size(), str.Get());
auto encodedLen = chip::Base64Encode(source.data(), static_cast<uint16_t>(source.size()), str.Get());
str.Get()[encodedLen] = '\0';

if (IsChipCertificate(source))
Expand Down Expand Up @@ -79,7 +86,8 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
snprintf(destination.data(), destination.size(), "%s\n", header);
for (; inIndex < base64DataLen; inIndex += 64)
{
outIndex += snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%.64s\n", &str[inIndex]);
auto charsPrinted = snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%.64s\n", &str[inIndex]);
outIndex += static_cast<size_t>(charsPrinted);
}
snprintf(&destination.data()[outIndex], destination.size() - outIndex, "%s", footer);
}
Expand Down
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
6 changes: 3 additions & 3 deletions examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ struct LinuxDeviceOptions
bool mWiFi = false;
bool mThread = false;
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE || CHIP_DEVICE_ENABLE_PORT_PARAMS
uint32_t securedDevicePort = CHIP_PORT;
uint32_t unsecuredCommissionerPort = CHIP_UDC_PORT;
uint16_t securedDevicePort = CHIP_PORT;
uint16_t unsecuredCommissionerPort = CHIP_UDC_PORT;
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
uint32_t securedCommissionerPort = CHIP_PORT + 12; // TODO: why + 12?
uint16_t securedCommissionerPort = CHIP_PORT + 12; // TODO: why + 12?
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
const char * command = nullptr;
const char * PICS = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions examples/shell/shell_common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ static_library("shell_common") {
}

public_configs = [ ":shell_common_config" ]

cflags = [ "-Wconversion" ]
}
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static CHIP_ERROR CmdAppServerClusters(int argc, char ** argv)
{
bool server = true;

for (int i = 0; i < emberAfEndpointCount(); i++)
for (uint16_t i = 0; i < emberAfEndpointCount(); i++)
{
EndpointId endpoint = emberAfEndpointFromIndex(i);

Expand All @@ -190,7 +190,7 @@ static CHIP_ERROR CmdAppServerClusters(int argc, char ** argv)

static CHIP_ERROR CmdAppServerEndpoints(int argc, char ** argv)
{
for (int i = 0; i < emberAfEndpointCount(); i++)
for (uint16_t i = 0; i < emberAfEndpointCount(); i++)
{
EndpointId endpoint = emberAfEndpointFromIndex(i);

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
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
Loading

0 comments on commit 2c2ac9b

Please sign in to comment.