Skip to content

Commit

Permalink
Turn on -Wconversion in some tests, shell, jsontlv. (#25326)
Browse files Browse the repository at this point in the history
* Turn on -Wconversion in some tests, shell, jsontlv.

* Address review comments.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Feb 21, 2024
1 parent 15a408d commit 2fd707a
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/inet/BasicPacketFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,26 +158,26 @@ 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;
}

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 };
};

Expand Down
2 changes: 2 additions & 0 deletions src/inet/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ chip_test_suite("tests") {
# TODO: This test does not seem executed
sources += [ "TestLwIPDNS.cpp" ]
}

cflags = [ "-Wconversion" ]
}
17 changes: 10 additions & 7 deletions src/inet/tests/TestInetAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(ULA_PREFIX | (lCurrent->global & ULA_UP_24_BIT_MASK) >> 16)));
NL_TEST_ASSERT(inSuite,
test_addr.Addr[1] ==
htonl(static_cast<uint32_t>((lCurrent->global & ULA_LO_16_BIT_MASK) << 16 | lCurrent->subnet)));
NL_TEST_ASSERT(inSuite, test_addr.Addr[2] == htonl(static_cast<uint32_t>(lCurrent->interface >> 32)));
NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(static_cast<uint32_t>(lCurrent->interface)));

++lCurrent;
}
Expand All @@ -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<uint32_t>(lCurrent->interface >> 32)));
NL_TEST_ASSERT(inSuite, test_addr.Addr[3] == htonl(static_cast<uint32_t>(lCurrent->interface)));

++lCurrent;
}
Expand Down Expand Up @@ -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<uint8_t>(128 - (i++ % 128));
ipprefix_2 = ipprefix_1;

NL_TEST_ASSERT(inSuite, !ipprefix_1.IsZero());
Expand Down
2 changes: 2 additions & 0 deletions src/lib/asn1/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ chip_test_suite("tests") {
"${chip_root}/src/platform",
"${nlunit_test_root}:nlunit-test",
]

cflags = [ "-Wconversion" ]
}
15 changes: 9 additions & 6 deletions src/lib/asn1/tests/TestASN1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(strlen(kTestVal_21_PrintableString)));
ASN1_ENCODE_STRING(kASN1UniversalTag_UTF8String, kTestVal_22_UTFString,
static_cast<uint16_t>(strlen(kTestVal_22_UTFString)));
ASN1_START_OCTET_STRING_ENCAPSULATED
{
ASN1_START_SEQUENCE
Expand All @@ -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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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<const uint8_t *>(kTestVal_21_PrintableString), strlen(kTestVal_21_PrintableString));
reinterpret_cast<const uint8_t *>(kTestVal_21_PrintableString),
static_cast<uint16_t>(strlen(kTestVal_21_PrintableString)));
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
}
ASN1_END_SEQUENCE;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/shell/commands/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ source_set("commands") {
if (chip_device_platform != "none") {
public_deps += [ "${chip_root}/src/app/server" ]
}

cflags = [ "-Wconversion" ]
}
16 changes: 12 additions & 4 deletions src/lib/shell/commands/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(argLen), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(BASE64_MAX_DECODED_LEN(argLen) <= sizeof(binary), CHIP_ERROR_INVALID_ARGUMENT);

binarySize = Base64Decode(argv[0], static_cast<uint16_t>(argLen), binary);
VerifyOrReturnError(binarySize != UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT);
streamer_print_hex(sout, binary, binarySize);
streamer_print_hex(sout, binary, static_cast<int>(binarySize));
streamer_printf(sout, "\r\n");
return CHIP_NO_ERROR;
}
Expand All @@ -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<uint32_t>(argLen), CHIP_ERROR_INVALID_ARGUMENT);

ArgParser::ParseHexString(argv[0], static_cast<uint32_t>(argLen), binary, sizeof(binary), binarySize);
base64Size = Base64Encode(binary, binarySize, base64);
if (!CanCastTo<uint16_t>(binarySize))
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
base64Size = Base64Encode(binary, static_cast<uint16_t>(binarySize), base64);
streamer_printf(sout, "%.*s\r\n", base64Size, base64);
return CHIP_NO_ERROR;
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/shell/commands/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/CommissionableDataProvider.h>
#include <platform/DeviceInstanceInfoProvider.h>
Expand Down Expand Up @@ -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<uint16_t>(arg))
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
uint16_t setupDiscriminator = static_cast<uint16_t>(arg);

VerifyOrReturnError(setupDiscriminator != 0 && setupDiscriminator < chip::kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT);

Expand Down
2 changes: 2 additions & 0 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ static_library("jsontlv") {
"${chip_root}/src/lib/core",
jsoncpp_root,
]

cflags = [ "-Wconversion" ]
}
4 changes: 2 additions & 2 deletions src/lib/support/jsontlv/TlvJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(span.size()), byteString.Get() + kBase64HeaderLen);
if (encodedLen)
{
memcpy(byteString.Get(), kBase64Header, kBase64HeaderLen);
encodedLen += kBase64HeaderLen;
encodedLen = static_cast<uint16_t>(encodedLen + kBase64HeaderLen);
}
byteString.Get()[encodedLen] = '\0';

Expand Down

0 comments on commit 2fd707a

Please sign in to comment.