From 6693bce787d0b1d36bc0eaca029da26ecfa11cfb Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Sun, 26 Feb 2023 10:17:19 -0500 Subject: [PATCH] Turn on -Wconversion in some tests, shell, jsontlv. (#25326) * Turn on -Wconversion in some tests, shell, jsontlv. * Address review comments. --- src/inet/BasicPacketFilters.h | 16 ++++++++-------- src/inet/tests/BUILD.gn | 2 ++ src/inet/tests/TestInetAddress.cpp | 17 ++++++++++------- src/lib/asn1/tests/BUILD.gn | 2 ++ src/lib/asn1/tests/TestASN1.cpp | 15 +++++++++------ src/lib/shell/commands/BUILD.gn | 2 ++ src/lib/shell/commands/Base64.cpp | 16 ++++++++++++---- src/lib/shell/commands/Config.cpp | 10 ++++++++-- src/lib/support/jsontlv/BUILD.gn | 2 ++ src/lib/support/jsontlv/TlvJson.cpp | 4 ++-- 10 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/inet/BasicPacketFilters.h b/src/inet/BasicPacketFilters.h index 364c0c7c11f17d..e6673af695996e 100644 --- a/src/inet/BasicPacketFilters.h +++ b/src/inet/BasicPacketFilters.h @@ -68,7 +68,7 @@ class DropIfTooManyQueuedPacketsFilter : public chip::Inet::EndpointQueueFilter * * @param maxAllowedQueuedPackets - number of packets currently pending allowed. */ - void SetMaxQueuedPacketsLimit(int maxAllowedQueuedPackets) { mMaxAllowedQueuedPackets.store(maxAllowedQueuedPackets); } + void SetMaxQueuedPacketsLimit(size_t maxAllowedQueuedPackets) { mMaxAllowedQueuedPackets.store(maxAllowedQueuedPackets); } /** * @return the total number of packets dropped so far by the filter @@ -158,17 +158,17 @@ class DropIfTooManyQueuedPacketsFilter : public chip::Inet::EndpointQueueFilter return FilterOutcome::kAllowPacket; } + // If we ever go negative, we have mismatch ingress/egress filter via predicate and + // device may eventually starve. + VerifyOrDie(mNumQueuedPackets != 0); + --mNumQueuedPackets; - int numQueuedPackets = mNumQueuedPackets.load(); + size_t numQueuedPackets = mNumQueuedPackets.load(); if (numQueuedPackets == 0) { OnLastMatchDequeued(endpoint, pktInfo, pktPayload); } - // If we ever go negative, we have mismatch ingress/egress filter via predicate and - // device may eventually starve. - VerifyOrDie(numQueuedPackets >= 0); - // We always allow the packet and just do accounting, since all dropping is prior to queue entry. return FilterOutcome::kAllowPacket; } @@ -176,8 +176,8 @@ class DropIfTooManyQueuedPacketsFilter : public chip::Inet::EndpointQueueFilter protected: PacketMatchPredicateFunc mPredicate = nullptr; void * mContext = nullptr; - std::atomic_int mNumQueuedPackets{ 0 }; - std::atomic_int mMaxAllowedQueuedPackets{ 0 }; + std::atomic_size_t mNumQueuedPackets{ 0 }; + std::atomic_size_t mMaxAllowedQueuedPackets{ 0 }; std::atomic_size_t mNumDroppedPackets{ 0u }; }; diff --git a/src/inet/tests/BUILD.gn b/src/inet/tests/BUILD.gn index ce7fd4664096ba..774d6af52d2a54 100644 --- a/src/inet/tests/BUILD.gn +++ b/src/inet/tests/BUILD.gn @@ -92,4 +92,6 @@ chip_test_suite("tests") { # TODO: This test does not seem executed sources += [ "TestLwIPDNS.cpp" ] } + + cflags = [ "-Wconversion" ] } diff --git a/src/inet/tests/TestInetAddress.cpp b/src/inet/tests/TestInetAddress.cpp index b696a89e44da44..4df87b1e752e59 100644 --- a/src/inet/tests/TestInetAddress.cpp +++ b/src/inet/tests/TestInetAddress.cpp @@ -1492,10 +1492,13 @@ void CheckMakeULA(nlTestSuite * inSuite, void * inContext) // Call MakeULA function that we test. test_addr = IPAddress::MakeULA(lCurrent->global, lCurrent->subnet, lCurrent->interface); - NL_TEST_ASSERT(inSuite, test_addr.Addr[0] == htonl(ULA_PREFIX | (lCurrent->global & ULA_UP_24_BIT_MASK) >> 16)); - NL_TEST_ASSERT(inSuite, test_addr.Addr[1] == htonl((lCurrent->global & ULA_LO_16_BIT_MASK) << 16 | lCurrent->subnet)); - NL_TEST_ASSERT(inSuite, test_addr.Addr[2] == htonl(lCurrent->interface >> 32)); - NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(lCurrent->interface)); + NL_TEST_ASSERT( + inSuite, test_addr.Addr[0] == htonl(static_cast(ULA_PREFIX | (lCurrent->global & ULA_UP_24_BIT_MASK) >> 16))); + NL_TEST_ASSERT(inSuite, + test_addr.Addr[1] == + htonl(static_cast((lCurrent->global & ULA_LO_16_BIT_MASK) << 16 | lCurrent->subnet))); + NL_TEST_ASSERT(inSuite, test_addr.Addr[2] == htonl(static_cast(lCurrent->interface >> 32))); + NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(static_cast(lCurrent->interface))); ++lCurrent; } @@ -1519,8 +1522,8 @@ void CheckMakeLLA(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, test_addr.Addr[0] == htonl(LLA_PREFIX)); NL_TEST_ASSERT(inSuite, test_addr.Addr[1] == 0); - NL_TEST_ASSERT(inSuite, test_addr.Addr[2] == htonl(lCurrent->interface >> 32)); - NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(lCurrent->interface)); + NL_TEST_ASSERT(inSuite, test_addr.Addr[2] == htonl(static_cast(lCurrent->interface >> 32))); + NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(static_cast(lCurrent->interface))); ++lCurrent; } @@ -1698,7 +1701,7 @@ void CheckIPPrefix(nlTestSuite * inSuite, void * inContext) SetupIPAddress(test_addr_1, lCurrent); ipprefix_1.IPAddr = test_addr_1; - ipprefix_1.Length = 128 - (i++ % 128); + ipprefix_1.Length = static_cast(128 - (i++ % 128)); ipprefix_2 = ipprefix_1; NL_TEST_ASSERT(inSuite, !ipprefix_1.IsZero()); diff --git a/src/lib/asn1/tests/BUILD.gn b/src/lib/asn1/tests/BUILD.gn index 27767118b5495b..6770bfa39a8552 100644 --- a/src/lib/asn1/tests/BUILD.gn +++ b/src/lib/asn1/tests/BUILD.gn @@ -30,4 +30,6 @@ chip_test_suite("tests") { "${chip_root}/src/platform", "${nlunit_test_root}:nlunit-test", ] + + cflags = [ "-Wconversion" ] } diff --git a/src/lib/asn1/tests/TestASN1.cpp b/src/lib/asn1/tests/TestASN1.cpp index 2560a58850d22d..da719fcc202c04 100644 --- a/src/lib/asn1/tests/TestASN1.cpp +++ b/src/lib/asn1/tests/TestASN1.cpp @@ -134,8 +134,10 @@ static CHIP_ERROR EncodeASN1TestData(ASN1Writer & writer) ASN1_ENCODE_OCTET_STRING(kTestVal_20_OctetString, 1); ASN1_ENCODE_OCTET_STRING(kTestVal_20_OctetString, 0); ASN1_ENCODE_STRING(kASN1UniversalTag_GeneralString, "", 0); - ASN1_ENCODE_STRING(kASN1UniversalTag_PrintableString, kTestVal_21_PrintableString, strlen(kTestVal_21_PrintableString)); - ASN1_ENCODE_STRING(kASN1UniversalTag_UTF8String, kTestVal_22_UTFString, strlen(kTestVal_22_UTFString)); + ASN1_ENCODE_STRING(kASN1UniversalTag_PrintableString, kTestVal_21_PrintableString, + static_cast(strlen(kTestVal_21_PrintableString))); + ASN1_ENCODE_STRING(kASN1UniversalTag_UTF8String, kTestVal_22_UTFString, + static_cast(strlen(kTestVal_22_UTFString))); ASN1_START_OCTET_STRING_ENCAPSULATED { ASN1_START_SEQUENCE @@ -159,7 +161,7 @@ static void TestASN1_Encode(nlTestSuite * inSuite, void * inContext) CHIP_ERROR err; static uint8_t buf[2048]; ASN1Writer writer; - uint16_t encodedLen; + size_t encodedLen; writer.Init(buf); @@ -290,7 +292,7 @@ static void TestASN1_NullWriter(nlTestSuite * inSuite, void * inContext) { CHIP_ERROR err; ASN1Writer writer; - uint16_t encodedLen; + size_t encodedLen; writer.InitNullWriter(); @@ -380,7 +382,7 @@ static void TestASN1_ObjectID(nlTestSuite * inSuite, void * inContext) static uint8_t buf[2048]; ASN1Writer writer; ASN1Reader reader; - uint16_t encodedLen; + size_t encodedLen; writer.Init(buf, sizeof(buf)); @@ -486,7 +488,8 @@ static void TestASN1_FromTLVReader(nlTestSuite * inSuite, void * inContext) ASN1_ENCODE_BIT_STRING(kTestVal_09_BitString); err = writer.PutValue(kASN1TagClass_Universal, kASN1UniversalTag_PrintableString, false, - reinterpret_cast(kTestVal_21_PrintableString), strlen(kTestVal_21_PrintableString)); + reinterpret_cast(kTestVal_21_PrintableString), + static_cast(strlen(kTestVal_21_PrintableString))); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); } ASN1_END_SEQUENCE; diff --git a/src/lib/shell/commands/BUILD.gn b/src/lib/shell/commands/BUILD.gn index 9cccd2f44a9153..192dbfda5410a3 100644 --- a/src/lib/shell/commands/BUILD.gn +++ b/src/lib/shell/commands/BUILD.gn @@ -69,4 +69,6 @@ source_set("commands") { if (chip_device_platform != "none") { public_deps += [ "${chip_root}/src/app/server" ] } + + cflags = [ "-Wconversion" ] } diff --git a/src/lib/shell/commands/Base64.cpp b/src/lib/shell/commands/Base64.cpp index 508017b161196b..0e64bd5959bbdc 100644 --- a/src/lib/shell/commands/Base64.cpp +++ b/src/lib/shell/commands/Base64.cpp @@ -49,9 +49,13 @@ static CHIP_ERROR Base64DecodeHandler(int argc, char ** argv) uint8_t binary[256]; VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); - binarySize = Base64Decode(argv[0], strlen(argv[0]), binary); + auto argLen = strlen(argv[0]); + VerifyOrReturnError(CanCastTo(argLen), CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(BASE64_MAX_DECODED_LEN(argLen) <= sizeof(binary), CHIP_ERROR_INVALID_ARGUMENT); + + binarySize = Base64Decode(argv[0], static_cast(argLen), binary); VerifyOrReturnError(binarySize != UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT); - streamer_print_hex(sout, binary, binarySize); + streamer_print_hex(sout, binary, static_cast(binarySize)); streamer_printf(sout, "\r\n"); return CHIP_NO_ERROR; } @@ -65,11 +69,15 @@ static CHIP_ERROR Base64EncodeHandler(int argc, char ** argv) VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); - size_t argLen = strlen(argv[0]); + auto argLen = strlen(argv[0]); VerifyOrReturnError(CanCastTo(argLen), CHIP_ERROR_INVALID_ARGUMENT); ArgParser::ParseHexString(argv[0], static_cast(argLen), binary, sizeof(binary), binarySize); - base64Size = Base64Encode(binary, binarySize, base64); + if (!CanCastTo(binarySize)) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + base64Size = Base64Encode(binary, static_cast(binarySize), base64); streamer_printf(sout, "%.*s\r\n", base64Size, base64); return CHIP_NO_ERROR; } diff --git a/src/lib/shell/commands/Config.cpp b/src/lib/shell/commands/Config.cpp index e29327b09fe672..ae1274a599bb16 100644 --- a/src/lib/shell/commands/Config.cpp +++ b/src/lib/shell/commands/Config.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -133,8 +134,13 @@ static CHIP_ERROR ConfigGetSetupDiscriminator(bool printHeader) static CHIP_ERROR ConfigSetSetupDiscriminator(char * argv) { CHIP_ERROR error; - streamer_t * sout = streamer_get(); - uint16_t setupDiscriminator = strtoull(argv, nullptr, 10); + streamer_t * sout = streamer_get(); + unsigned long long arg = strtoull(argv, nullptr, 10); + if (!CanCastTo(arg)) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + uint16_t setupDiscriminator = static_cast(arg); VerifyOrReturnError(setupDiscriminator != 0 && setupDiscriminator < chip::kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT); diff --git a/src/lib/support/jsontlv/BUILD.gn b/src/lib/support/jsontlv/BUILD.gn index eda4d7fa19bcf2..1e62202adc29a5 100644 --- a/src/lib/support/jsontlv/BUILD.gn +++ b/src/lib/support/jsontlv/BUILD.gn @@ -27,4 +27,6 @@ static_library("jsontlv") { "${chip_root}/src/lib/core", jsoncpp_root, ] + + cflags = [ "-Wconversion" ] } diff --git a/src/lib/support/jsontlv/TlvJson.cpp b/src/lib/support/jsontlv/TlvJson.cpp index 35bf8686d43bfc..b18331b730c833 100644 --- a/src/lib/support/jsontlv/TlvJson.cpp +++ b/src/lib/support/jsontlv/TlvJson.cpp @@ -156,11 +156,11 @@ CHIP_ERROR TlvToJson(TLV::TLVReader & reader, KeyContext context, Json::Value & byteString.Alloc(kBase64HeaderLen + BASE64_ENCODED_LEN(span.size()) + 1); VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY); - auto encodedLen = Base64Encode(span.data(), span.size(), byteString.Get() + kBase64HeaderLen); + auto encodedLen = Base64Encode(span.data(), static_cast(span.size()), byteString.Get() + kBase64HeaderLen); if (encodedLen) { memcpy(byteString.Get(), kBase64Header, kBase64HeaderLen); - encodedLen += kBase64HeaderLen; + encodedLen = static_cast(encodedLen + kBase64HeaderLen); } byteString.Get()[encodedLen] = '\0';