Variable length arrays (VLAs) are a feature of C99 and we treat them as a conforming language extension in C++. The feature reuses typical array declaration syntax, as in:
void func(int n) {
int array[n]; // This is a VLA
}
void other(int n, int array[n]); // This is a variably modified type,
// also a kind of VLA but without stack allocation.
Use of a VLA will perform a stack allocation to create an array whose size is determined by the argument passed to the function. However, the reuse of constant array declaration syntax coupled with the potential for a user-controlled stack allocation, make use of VLAs a security concern. For example:
https://nvd.nist.gov/vuln/detail/CVE-2015-5147
https://nvd.nist.gov/vuln/detail/CVE-2020-11203
https://nvd.nist.gov/vuln/detail/CVE-2021-3527
Given the recent advice from certain government agencies to avoid C and C++ due to poor security practices and a lack of coverage with tooling (Consumer Reports, National Security Agency) and our recent efforts to improve security related diagnostics, I am proposing we diagnose use of this extension in C++ more aggressively than we have previously. Specifically, I would like to warn about the extension by default in -std=c++NN
language modes and add the warning to -Wall
for -std=gnu++NN
language modes. We currently issue diagnostics for this under -Wvla-extensions
; I’m not proposing changing the behavior of what code gets diagnosed, just whether -Wvla-extensions
is enabled by default or not.
I have put up a patch for the changes at https://reviews.llvm.org/D156565 and there has been some positive feedback, but I wanted to see if there were wider concerns from the community before moving forward. Note, I have filed an issue with GCC to consider making the same changes. That issue is not resolved, but discussion has died down over this past month and does seem to have some support as well but it is not guaranteed they’ll make the same changes. The primary concern with enabling the diagnostic by default is that it’s a conforming language extension, so you can enable warnings for it with -pedantic
, and we don’t typically issue a diagnostic by default for non-problematic use of an extension (aka, a congratulatory diagnostic). While I agree with this logic in general, I think use of VLAs in C++ should still be discouraged by default in favor of more idiomatic features (like std::vector
, etc) given the ease of accidental usage coupled with the security concerns.
If the community doesn’t have concerns with moving forward, I would like to land these changes early in the 18.x cycle so that we have a long bake time in case there’s surprising fallout from enabling the diagnostic.