Skip to content

Commit

Permalink
[compiler] Add -Wshorten-64-to-32 compiler flag for is_clang (#23111)
Browse files Browse the repository at this point in the history
* [compiler] Add -Wshorten-64-to-32 compiler flag for is_clang

* Update generated code
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 21, 2023
1 parent be9e8ae commit d53bd1b
Show file tree
Hide file tree
Showing 27 changed files with 220 additions and 90 deletions.
1 change: 1 addition & 0 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ config("strict_warnings") {
cflags += [
"-Wimplicit-fallthrough",
"-Wheader-hygiene",
"-Wshorten-64-to-32",
"-Wformat-type-confusion",
]
}
Expand Down
2 changes: 1 addition & 1 deletion examples/common/tracing/TraceDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ CHIP_ERROR TraceDecoder::ReadString(const char * str)

CHIP_ERROR TraceDecoder::LogJSON(Json::Value & json)
{
uint32_t protocol = json[kProtocolIdKey].asLargestUInt();
auto protocol = json[kProtocolIdKey].asLargestUInt();
uint16_t vendorId = protocol >> 16;
uint16_t protocolId = protocol & 0xFFFF;
if (!mOptions.IsProtocolEnabled(chip::Protocols::Id(chip::VendorId(vendorId), protocolId)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ static bool ParseJsonFileAndPopulateCandidates(

auto vendorId = [NSNumber numberWithUnsignedInt:iter.get("vendorId", 1).asUInt()];
auto productId = [NSNumber numberWithUnsignedInt:iter.get("productId", 1).asUInt()];
auto softwareVersion = [NSNumber numberWithUnsignedLong:iter.get("softwareVersion", 10).asUInt64()];
auto softwareVersion = [NSNumber numberWithUnsignedLongLong:iter.get("softwareVersion", 10).asUInt64()];
auto softwareVersionString = [NSString stringWithUTF8String:iter.get("softwareVersionString", "1.0.0").asCString()];
auto cDVersionNumber = [NSNumber numberWithUnsignedInt:iter.get("cDVersionNumber", 0).asUInt()];
auto softwareVersionValid = iter.get("softwareVersionValid", true).asBool() ? YES : NO;
auto minApplicableSoftwareVersion =
[NSNumber numberWithUnsignedLong:iter.get("minApplicableSoftwareVersion", 0).asUInt64()];
[NSNumber numberWithUnsignedLongLong:iter.get("minApplicableSoftwareVersion", 0).asUInt64()];
auto maxApplicableSoftwareVersion =
[NSNumber numberWithUnsignedLong:iter.get("maxApplicableSoftwareVersion", 1000).asUInt64()];
[NSNumber numberWithUnsignedLongLong:iter.get("maxApplicableSoftwareVersion", 1000).asUInt64()];
auto otaURL = [NSString stringWithUTF8String:iter.get("otaURL", "https://test.com").asCString()];

candidate.deviceModelData.vendorId = vendorId;
Expand Down
14 changes: 5 additions & 9 deletions examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <lib/core/CHIPError.h>
#include <lib/support/Base64.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/SafeInt.h>

#include <credentials/examples/DeviceAttestationCredsExample.h>

Expand Down Expand Up @@ -233,16 +234,11 @@ bool Base64ArgToVector(const char * arg, size_t maxSize, std::vector<uint8_t> &
outVector.resize(maxSize);

size_t argLen = strlen(arg);
if (argLen > maxBase64Size)
{
return false;
}
VerifyOrReturnValue(argLen <= maxBase64Size, false);
VerifyOrReturnValue(chip::CanCastTo<uint32_t>(argLen), false);

size_t decodedLen = chip::Base64Decode32(arg, argLen, reinterpret_cast<uint8_t *>(outVector.data()));
if (decodedLen == 0)
{
return false;
}
size_t decodedLen = chip::Base64Decode32(arg, static_cast<uint32_t>(argLen), reinterpret_cast<uint8_t *>(outVector.data()));
VerifyOrReturnValue(decodedLen != 0, false);

outVector.resize(decodedLen);
return true;
Expand Down
10 changes: 7 additions & 3 deletions examples/providers/DeviceInfoProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <lib/support/SafeInt.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <stdlib.h>
Expand Down Expand Up @@ -134,6 +135,8 @@ CHIP_ERROR DeviceInfoProviderImpl::GetUserLabelLength(EndpointId endpoint, size_

CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel)
{
VerifyOrReturnError(CanCastTo<uint32_t>(index), CHIP_ERROR_INVALID_ARGUMENT);

DefaultStorageKeyAllocator keyAlloc;
uint8_t buf[UserLabelTLVMaxSize()];
TLV::TLVWriter writer;
Expand All @@ -145,15 +148,15 @@ CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t in
ReturnErrorOnFailure(writer.PutString(kLabelValueTag, userLabel.value));
ReturnErrorOnFailure(writer.EndContainer(outerType));

return mStorage->SyncSetKeyValue(keyAlloc.UserLabelIndexKey(endpoint, index), buf,
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelIndexKey(endpoint, static_cast<uint32_t>(index)), buf,
static_cast<uint16_t>(writer.GetLengthWritten()));
}

CHIP_ERROR DeviceInfoProviderImpl::DeleteUserLabelAt(EndpointId endpoint, size_t index)
{
DefaultStorageKeyAllocator keyAlloc;

return mStorage->SyncDeleteKeyValue(keyAlloc.UserLabelIndexKey(endpoint, index));
return mStorage->SyncDeleteKeyValue(keyAlloc.UserLabelIndexKey(endpoint, static_cast<uint32_t>(index)));
}

DeviceInfoProvider::UserLabelIterator * DeviceInfoProviderImpl::IterateUserLabel(EndpointId endpoint)
Expand All @@ -176,12 +179,13 @@ bool DeviceInfoProviderImpl::UserLabelIteratorImpl::Next(UserLabelType & output)
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrReturnError(mIndex < mTotal, false);
VerifyOrReturnError(CanCastTo<uint32_t>(mIndex), false);

DefaultStorageKeyAllocator keyAlloc;
uint8_t buf[UserLabelTLVMaxSize()];
uint16_t len = static_cast<uint16_t>(sizeof(buf));

err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, len);
err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, static_cast<uint32_t>(mIndex)), buf, len);
VerifyOrReturnError(err == CHIP_NO_ERROR, false);

TLV::ContiguousBufferTLVReader reader;
Expand Down
4 changes: 2 additions & 2 deletions scripts/idl/generators/java/ChipClustersCpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
auto * listHolder_{{depth}} = new ListHolder<ListMemberType_{{depth}}>({{source}}Size);
listFreer.add(listHolder_{{depth}});

for (size_t i_{{depth}} = 0; i_{{depth}} < static_cast<size_t>({{source}}Size); ++i_{{depth}}) {
for (jint i_{{depth}} = 0; i_{{depth}} < {{source}}Size; ++i_{{depth}}) {
jobject element_{{depth}};
chip::JniReferences::GetInstance().GetListItem({{source}}, i_{{depth}}, element_{{depth}});
{{encode_value(
"listHolder_{}->mList[i_{}]".format(depth, depth),
"listHolder_{}->mList[static_cast<uint32_t>(i_{})]".format(depth, depth),
"element_{}".format(depth),
depth+1, encodable.without_list()
)}}
Expand Down
51 changes: 37 additions & 14 deletions src/controller/java/AndroidOperationalCredentialsIssuer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,25 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::CallbackGenerateNOCChain(const B
return err;
}

VerifyOrReturnError(CanCastTo<uint32_t>(csrElements.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(csrNonce.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(csrElementsSignature.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(attestationChallenge.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(DAC.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(PAI.size()), CHIP_ERROR_INVALID_ARGUMENT);

mOnNOCCompletionCallback = onCompletion;

env->ExceptionClear();

jbyteArray javaCsrElements;
JniReferences::GetInstance().N2J_ByteArray(env, csrElements.data(), csrElements.size(), javaCsrElements);
JniReferences::GetInstance().N2J_ByteArray(env, csrElements.data(), static_cast<uint32_t>(csrElements.size()), javaCsrElements);

jbyteArray javaCsrNonce;
JniReferences::GetInstance().N2J_ByteArray(env, csrNonce.data(), csrNonce.size(), javaCsrNonce);
JniReferences::GetInstance().N2J_ByteArray(env, csrNonce.data(), static_cast<uint32_t>(csrNonce.size()), javaCsrNonce);

jbyteArray javaCsrElementsSignature;
JniReferences::GetInstance().N2J_ByteArray(env, csrElementsSignature.data(), csrElementsSignature.size(),
JniReferences::GetInstance().N2J_ByteArray(env, csrElementsSignature.data(), static_cast<uint32_t>(csrElementsSignature.size()),
javaCsrElementsSignature);

ChipLogProgress(Controller, "Parsing Certificate Signing Request");
Expand All @@ -202,8 +209,10 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::CallbackGenerateNOCChain(const B
ByteSpan csr(reader.GetReadPoint(), reader.GetLength());
reader.ExitContainer(containerType);

VerifyOrReturnError(CanCastTo<uint32_t>(csr.size()), CHIP_ERROR_INVALID_ARGUMENT);

jbyteArray javaCsr;
JniReferences::GetInstance().N2J_ByteArray(env, csr.data(), csr.size(), javaCsr);
JniReferences::GetInstance().N2J_ByteArray(env, csr.data(), static_cast<uint32_t>(csr.size()), javaCsr);

P256PublicKey pubkey;
ReturnErrorOnFailure(VerifyCertificateSigningRequest(csr.data(), csr.size(), pubkey));
Expand All @@ -218,28 +227,36 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::CallbackGenerateNOCChain(const B
}

jbyteArray javaAttestationChallenge;
JniReferences::GetInstance().N2J_ByteArray(env, attestationChallenge.data(), attestationChallenge.size(),
JniReferences::GetInstance().N2J_ByteArray(env, attestationChallenge.data(), static_cast<uint32_t>(attestationChallenge.size()),
javaAttestationChallenge);

const ByteSpan & attestationElements = mAutoCommissioner->GetAttestationElements();
VerifyOrReturnError(CanCastTo<uint32_t>(attestationElements.size()), CHIP_ERROR_INVALID_ARGUMENT);

jbyteArray javaAttestationElements;
JniReferences::GetInstance().N2J_ByteArray(env, attestationElements.data(), attestationElements.size(),
JniReferences::GetInstance().N2J_ByteArray(env, attestationElements.data(), static_cast<uint32_t>(attestationElements.size()),
javaAttestationElements);

const ByteSpan & attestationNonce = mAutoCommissioner->GetAttestationNonce();
VerifyOrReturnError(CanCastTo<uint32_t>(attestationNonce.size()), CHIP_ERROR_INVALID_ARGUMENT);

jbyteArray javaAttestationNonce;
JniReferences::GetInstance().N2J_ByteArray(env, attestationNonce.data(), attestationNonce.size(), javaAttestationNonce);
JniReferences::GetInstance().N2J_ByteArray(env, attestationNonce.data(), static_cast<uint32_t>(attestationNonce.size()),
javaAttestationNonce);

const ByteSpan & attestationElementsSignature = mAutoCommissioner->GetAttestationSignature();
VerifyOrReturnError(CanCastTo<uint32_t>(attestationElementsSignature.size()), CHIP_ERROR_INVALID_ARGUMENT);

jbyteArray javaAttestationElementsSignature;
JniReferences::GetInstance().N2J_ByteArray(env, attestationElementsSignature.data(), attestationElementsSignature.size(),
JniReferences::GetInstance().N2J_ByteArray(env, attestationElementsSignature.data(),
static_cast<uint32_t>(attestationElementsSignature.size()),
javaAttestationElementsSignature);

jbyteArray javaDAC;
JniReferences::GetInstance().N2J_ByteArray(env, DAC.data(), DAC.size(), javaDAC);
JniReferences::GetInstance().N2J_ByteArray(env, DAC.data(), static_cast<uint32_t>(DAC.size()), javaDAC);

jbyteArray javaPAI;
JniReferences::GetInstance().N2J_ByteArray(env, PAI.data(), PAI.size(), javaPAI);
JniReferences::GetInstance().N2J_ByteArray(env, PAI.data(), static_cast<uint32_t>(PAI.size()), javaPAI);

ByteSpan certificationDeclarationSpan;
ByteSpan attestationNonceSpan;
Expand All @@ -255,12 +272,16 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::CallbackGenerateNOCChain(const B
return err;
}

VerifyOrReturnError(CanCastTo<uint32_t>(certificationDeclarationSpan.size()), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(CanCastTo<uint32_t>(firmwareInfoSpan.size()), CHIP_ERROR_INVALID_ARGUMENT);

jbyteArray javaCD;
JniReferences::GetInstance().N2J_ByteArray(env, certificationDeclarationSpan.data(), certificationDeclarationSpan.size(),
javaCD);
JniReferences::GetInstance().N2J_ByteArray(env, certificationDeclarationSpan.data(),
static_cast<uint32_t>(certificationDeclarationSpan.size()), javaCD);

jbyteArray javaFirmwareInfo;
JniReferences::GetInstance().N2J_ByteArray(env, firmwareInfoSpan.data(), firmwareInfoSpan.size(), javaFirmwareInfo);
JniReferences::GetInstance().N2J_ByteArray(env, firmwareInfoSpan.data(), static_cast<uint32_t>(firmwareInfoSpan.size()),
javaFirmwareInfo);

jobject attestationInfo;
err = N2J_AttestationInfo(env, javaAttestationChallenge, javaAttestationNonce, javaAttestationElements,
Expand Down Expand Up @@ -305,6 +326,8 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::LocalGenerateNOCChain(const Byte
return err;
}

VerifyOrReturnError(CanCastTo<uint32_t>(csrElements.size()), CHIP_ERROR_INVALID_ARGUMENT);

NodeId assignedId;
if (mNodeIdRequested)
{
Expand Down Expand Up @@ -374,7 +397,7 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::LocalGenerateNOCChain(const Byte
jbyteArray javaCsr;
JniReferences::GetInstance().GetEnvForCurrentThread()->ExceptionClear();
JniReferences::GetInstance().N2J_ByteArray(JniReferences::GetInstance().GetEnvForCurrentThread(), csrElements.data(),
csrElements.size(), javaCsr);
static_cast<uint32_t>(csrElements.size()), javaCsr);
JniReferences::GetInstance().GetEnvForCurrentThread()->CallVoidMethod(mJavaObjectRef, method, javaCsr);
return CHIP_NO_ERROR;
}
Expand Down
Loading

0 comments on commit d53bd1b

Please sign in to comment.