Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does clang complain about using variable-length arrays with '-std=c99' flag?

When I compile this experiment code:

int main(void)
{
    int foo = 5;
    char bar[foo];
}

with clang and the '-Weverything' or respectively the separate '-Wvla' flag combined with the '-std=c99' flag, I still get the warning:

warning: variable length array used [-Wvla]

Example here

although C99 conform implementations shall, in comparison to later C standards (C11, C18, etc.) - where the VLA-support is optional, support variable length arrays without exception.


  • Why do I still get this warning for using a VLA with the '-std=c99' flag in clang?
  • Is this a bug or is that just for the hint to take care for the compliance to implementations conforming to later C standards (as well as C89/C90)?
like image 429
RobertS supports Monica Cellio Avatar asked May 15 '20 18:05

RobertS supports Monica Cellio


People also ask

Does clang support variable length arrays?

However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs. If you would prefer not to use this extension, you can disable it with -Werror=vla.

Why are variable length arrays bad?

The biggest problem is that one can not even check for failure as they could with the slightly more verbose malloc'd memory. Assumptions in the size of an array could be broken two years after writing perfectly legal C using VLAs, leading to possibly very difficult to find issues in the code.

What makes an array a variable length array?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).

How do you declare a variable length array in C++?

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself: int n = 10; double* a = new double[n]; // Don't forget to delete [] a; when you're done!


3 Answers

You are correct that conforming C99 implementations must support VLAs, whereas implementations can conform to later C standards without supporting VLAs. But I think you're missing the forest for the trees: I take the aforementioned difference between the standards to be exactly the point of the warning. It's not saying that your code, as you are now building it, may break. Rather, it is warning that you are relying on a feature that is not universally forward compatible, so that if you try to build your code somewhere else then it may break.

If Clang thought that VLAs were a non-standard extension to C99, then it ought not to accept your VLA-using code at all in -std=c99 mode.

like image 197
John Bollinger Avatar answered Oct 18 '22 19:10

John Bollinger


C99 conforming implementations must support VLAs, but enabling all warnings with -Weverything means please compiler, tell me about anything that may pose a problem. Among potential problems are many conforming constructs:

  • VLAs may pose portability issues to non fully conforming implementations, and there are quite a few of those. Use -Wno-vla to disable this warning.
  • some combinations of operators may be caused by a programmer error or confusion about relative precedence that is sometimes counter-intuitive. The expression is still conform to the C Standard, yet the compiler will issue a warning and the programmer can add parentheses to prevent the warning.
  • assignments in a test expression are conforming expressions, yet the compiler will issue a warning as they might be simple typos, where == was misspelt as =.
  • unused variables are not errors, but will produce warnings under -Weverything

The list goes on. If you ask for more warnings, which a life saver in most circumstances, you might want to refine your CFLAGS to disable some selected warnings such as -Wno-vla to compile your programs if you indeed use VLAs on purpose.

like image 30
chqrlie Avatar answered Oct 18 '22 17:10

chqrlie


There are various reasons why one might want to avoid using variable length arrays in a program, even when they are guaranteed to be supported in the version of the language you are using. One, as John Bollinger mentioned, is in case you want to maintain compatibility with other standards that don't support them.

Another is that, on typical stack-based implementations, they consume an amount of stack that may not be known at compile time, or that an untrusted user may be able to affect, and such implementations typically don't have a good way to detect or recover from stack overflow. For instance, the Linux kernel developers have decided for this reason that VLAs should not be used in the kernel, and using -Wvla helps them enforce this.

These issues will certainly not apply in every program, and that is why the -Wvla option is an option; you can turn it on if, for whatever reason, you want to know about uses of VLAs in your program, and you can leave it off if you don't. You chose to use -Weverything which turns on all warnings that exist. This flag is not really intended to be useful for programmers, since as you've noticed, it turns on many warnings that are only meant to be useful in certain situations to people that know they want them. It's meant instead for helping to debug the compiler itself.

So in effect, you have told the compiler "issue all the warnings you can, even if they're not relevant in my situation", and now you're asking why you got a warning that wasn't relevant in your situation :-)

like image 3
Nate Eldredge Avatar answered Oct 18 '22 17:10

Nate Eldredge