Re: Arrays of variable length

From: Al Viro
Date: Sun Mar 05 2017 - 05:02:01 EST


On Sun, Mar 05, 2017 at 11:44:33AM +0200, Tomas Winkler wrote:
> Sparse complains for arrays declared with variable length
>
> 'warning: Variable length array is used'
>
> Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
> with that https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
> And also Linux kernel compilation with W=1 doesn't complain.
>
> Since sparse is used extensively would like to ask what is the correct
> usage of arrays of variable length
> within Linux Kernel.

That depends. For structure members the answer is simply "don't, it's
not a valid C to start with". Note that this is about actual VLA, not
struct foo {
int bar;
struct baz[];
}
- that is valid C99 and sparse is just fine with it. For local variables...
keep in mind that kernel stack is _small_, so any VLA there needs to be
done very carefully. For heap it's more or less usable, but keep in mind
that gcc support of VLA (and variably-modified types in general) has
seriously unpleasant corner cases, especially when combined with the ({...})
thing. IOW, "doesn't have problem" is overoptimistic; use with care.