CERT's Secure Coding Standard includes an item (API05-C) which encourages the use of conformant array parameters, which is a recommendation I've implemented in a lot of my code (hidden behind a macro for compilers which don't support them).
For those who don't know, a conformant array parameter is something like:
void foo(int length, char data[length]);
API05-C provides more information.
Lots of compilers don't like variable-length arrays (for good reason). C11 demotes them from required (as they were in C99) to optional (compilers should define __STDC_NO_VLA__
if they're not implemented). MSVC flat out doesn't support them. IAR hides them behind a switch (--vla
). GCC and clang will warn you about them if you ask (with -Wvla
, or -Werror=vla
if you want an error).
Conformant array parameters don't suffer from the same problems as "normal" variable-length arrays; they don't result in variable stack usage. They just tell the compiler how big an existing array, which could be on the stack or heap, is.
My issue is that every compiler I'm aware of treats conformant array parameters as VLAs. This isn't such a big deal for compilers like MSVC since I can just define my macro to nothing, but for compilers like GCC and clang I want to use conformant array parameters but don't want to trigger -Wvla
diagnostics.
According to API05-C (emphasis added):
Consequently, an array declaration that serves as a function argument may have an index that is a variable or an expression. The array argument is demoted to a pointer and is consequently not a variable length array (VLA). Conformant array parameters can be used by developers to indicate the expected bounds of the array. This information may be used by compilers, or it may be ignored. However, such declarations are useful to developers because they serve to document relationships between array sizes and pointers. This information can also be used by static analysis tools to diagnose potential defects.
I desperately want this to be true, but I can't seem to find the relevant parts of the C99 or C11 standards.
So, based strictly on the C99/C11 standards, are conformant array parameters VLAs? Or, to put it another way, does passing an array as an argument really demote it to a pointer?
Obviously, please cite the relevant parts of the specification(s).
type funcname (int a, int array[a]) { ... }
, then yes,array
is a VLA (it can't be anything else by definition,'a'
is not a constant). Also, you may prefer C11 Standard (n1570 draft) over the .pdf form. – David C. Rankindoes passing an array as an argument really demote it to a pointer?
Yes. It would be pretty heavy not to do so. – Alceste_C
in the declarationvoid fvla(int m, int C[m][m]);
a "VLA". The (normative) language in 6.7.6.2 indicates that yes, these array parameters are considered variable length arrays. – Cornstalks