Skip to content

Commit

Permalink
Disallow VLAs in Matter code by adding -Wvla to our strict warnings c…
Browse files Browse the repository at this point in the history
…onfig.

We suppress the warning in two places, both tracked by existing issues.
  • Loading branch information
bzbarsky-apple committed Sep 3, 2021
1 parent d716f27 commit 5d60f28
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ config("strict_warnings") {
"-Wextra",
"-Wshadow",
"-Wunreachable-code",
"-Wvla",
]

cflags_cc = [ "-Wnon-virtual-dtor" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ void {{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}ListAttribu

CHECK_MESSAGE_LENGTH_VOID(2);
uint16_t count = Encoding::LittleEndian::Read16(message);
{{! TODO: This VLA is not OK. See https://github.com/project-chip/connectedhomeip/issues/8558 }}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvla"
{{chipType}} data[count];
#pragma GCC diagnostic pop
for (size_t i = 0; i < count; i++)
{
{{#if isStruct}}
Expand Down
18 changes: 12 additions & 6 deletions src/crypto/tests/CHIPCryptoPALTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,11 @@ static void TestAES_CCM_128DecryptInvalidCipherText(nlTestSuite * inSuite, void
if (vector->pt_len > 0)
{
numOfTestsRan++;
uint8_t out_pt[vector->pt_len];
Platform::ScopedMemoryBuffer<uint8_t> out_pt;
out_pt.Alloc(vector->pt_len);
NL_TEST_ASSERT(inSuite, out_pt);
CHIP_ERROR err = AES_CCM_decrypt(vector->ct, 0, vector->aad, vector->aad_len, vector->tag, vector->tag_len, vector->key,
vector->key_len, vector->iv, vector->iv_len, out_pt);
vector->key_len, vector->iv, vector->iv_len, out_pt.Get());
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
break;
}
Expand All @@ -575,9 +577,11 @@ static void TestAES_CCM_128DecryptInvalidKey(nlTestSuite * inSuite, void * inCon
if (vector->pt_len > 0)
{
numOfTestsRan++;
uint8_t out_pt[vector->pt_len];
Platform::ScopedMemoryBuffer<uint8_t> out_pt;
out_pt.Alloc(vector->pt_len);
NL_TEST_ASSERT(inSuite, out_pt);
CHIP_ERROR err = AES_CCM_decrypt(vector->ct, vector->ct_len, vector->aad, vector->aad_len, vector->tag, vector->tag_len,
nullptr, 0, vector->iv, vector->iv_len, out_pt);
nullptr, 0, vector->iv, vector->iv_len, out_pt.Get());
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
break;
}
Expand All @@ -595,9 +599,11 @@ static void TestAES_CCM_128DecryptInvalidIVLen(nlTestSuite * inSuite, void * inC
if (vector->pt_len > 0)
{
numOfTestsRan++;
uint8_t out_pt[vector->pt_len];
Platform::ScopedMemoryBuffer<uint8_t> out_pt;
out_pt.Alloc(vector->pt_len);
NL_TEST_ASSERT(inSuite, out_pt);
CHIP_ERROR err = AES_CCM_decrypt(vector->ct, vector->ct_len, vector->aad, vector->aad_len, vector->tag, vector->tag_len,
vector->key, vector->key_len, vector->iv, 0, out_pt);
vector->key, vector->key_len, vector->iv, 0, out_pt.Get());
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ static BOOL isRunningTests(void)
return CHIP_ERROR_INVALID_ARGUMENT;
}

uint16_t idStringLen = 16;
char issuerIdString[idStringLen];
char issuerIdString[16];
uint16_t idStringLen = sizeof(issuerIdString);
if (CHIP_NO_ERROR != storage->SyncGetKeyValue(CHIP_COMMISSIONER_CA_ISSUER_ID, issuerIdString, idStringLen)) {
mIssuerId = arc4random();
CHIP_LOG_ERROR("Assigned %d certificate issuer ID to the commissioner", mIssuerId);
Expand Down
11 changes: 5 additions & 6 deletions src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public:
CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallbackBridge * callback = reinterpret_cast<CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallbackBridge *>(context);
if (callback && callback->mQueue)
{
id values[count];
id array = [NSMutableArray arrayWithCapacity:count];
for (uint16_t i = 0; i < count; i++)
{
{{#if isStruct}}
values[i] = [[NSDictionary alloc] initWithObjectsAndKeys:
array[i] = [[NSDictionary alloc] initWithObjectsAndKeys:
{{#chip_attribute_list_entryTypes}}
{{#if (isString type)}}
{{#if (isOctetString type)}}
Expand All @@ -109,17 +109,16 @@ public:
{{else}}
{{#if (isString type)}}
{{#if (isOctetString type)}}
values[i] = [NSData dataWithBytes:entries[i].data() length:entries[i].size()];
array[i] = [NSData dataWithBytes:entries[i].data() length:entries[i].size()];
{{else}}
values[i] = [[NSString alloc] initWithBytes:entries[i]+{{asReadTypeLength type}} length:emberAf{{asReadType type}}Length(entries[i]) encoding:NSUTF8StringEncoding];
array[i] = [[NSString alloc] initWithBytes:entries[i]+{{asReadTypeLength type}} length:emberAf{{asReadType type}}Length(entries[i]) encoding:NSUTF8StringEncoding];
{{/if}}
{{else}}
values[i] = [NSNumber numberWith{{asObjectiveCNumberType label type false}}:entries[i]];
array[i] = [NSNumber numberWith{{asObjectiveCNumberType label type false}}:entries[i]];
{{/if}}
{{/if}}
}

id array = [NSArray arrayWithObjects:values count:count];
dispatch_async(callback->mQueue, ^{
callback->mHandler(nil, @{ @"value": array });
callback->Cancel();
Expand Down
5 changes: 2 additions & 3 deletions src/setup_payload/ManualSetupPayloadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ static uint32_t chunk3PayloadRepresentation(const SetupPayload & payload)
}

// TODO: issue #3663 - Unbounded stack in src/setup_payload
#if !defined(__clang__)
#pragma GCC diagnostic push
#if !defined(__clang__)
#pragma GCC diagnostic ignored "-Wstack-usage="
#endif
#pragma GCC diagnostic ignored "-Wvla"

static std::string decimalStringWithPadding(uint32_t number, int minLength)
{
Expand All @@ -100,9 +101,7 @@ static std::string decimalStringWithPadding(uint32_t number, int minLength)
return std::string(buf);
}

#if !defined(__clang__)
#pragma GCC diagnostic pop
#endif

CHIP_ERROR ManualSetupPayloadGenerator::payloadDecimalStringRepresentation(std::string & outDecimalString)
{
Expand Down

0 comments on commit 5d60f28

Please sign in to comment.