VLA warning on recent git

Uecker, Martin Martin.Uecker@med.uni-goettingen.de
Fri Oct 23 13:41:03 GMT 2020



I tested a recent GCC from git and noticed a couple
of new warnings for VLA parameters. 

(Martin, I assume this is your work. First, let me
say: thank you! I think this is really important.)

Here is some feedback from running this on an
existing code base. See below for comments and
toy examples. The capital letters refer to 
specific warnings.

Best,
Martin



-Wvla-parameter

A and B: I think the warning is a bit too strict.
These types are compatible, just one of them is
incomplete. 

C: this seems to be bug as the types are
identical.

-Wstringop-overflow / -Wnonnull

D and F: Without 'static' I am not sure why this
is not allowed to be a null pointer (as in E and G).




#include <stdlib.h>

void f1(int n, int x[n]);

void f1(int n, int x[]) { }; 
// A: warning: argument 2 of type ‘int[]’ declared as an ordinary array

void f2(int n, int x[]);

void f2(int n, int x[n]) { }; 
// B: argument 2 of type ‘int[n]’ declared as a variable length array

void f3(int n, int x[n + 1]);

void f3(int n, int x[n + 1]) { }; 
// C: argument 2 of type ‘int[n + 1]’ declared with mismatched bound ‘n
+ 1’

void f4(int n, int x[static n]) { };

void foo(void)
{
  int x[] = { };
  f1(0, x);
  f1(1, x); 
// D: ‘f1’ accessing 4 bytes in a region of size 0

  f4(0, x);
  f4(1, x):
// E: ‘f4’ accessing 4 bytes in a region of size
0

  f1(0, NULL);
  f1(1, NULL);
// F: argument 1 of variable length array ‘int[n]’ is null but the
corresponding bound argument 2 value is 1

  f4(0, NULL);
  f4(1, NULL);
// G: argument 1 of variable length array ‘int[n]’ is null but the
corresponding bound argument 2 value is 1
}



More information about the Gcc mailing list