Let's take the following for an example: int bar(int n, int[n]); void foo(int n) { int arr[n]; for (int i = 0; i < n; ++i) { arr[i] = i; } bar(n, arr); } When compiled with $ gcc -O -Wvla-larger-than=0 -c foo.c a warning is shown as expected: foo.c: In function ‘foo’: foo.c:5:9: warning: argument to variable-length array may be too large [-Wvla-larger-than=] 5 | int arr[n]; | ^~~ It works with -O, -O1, -O2, -O3, -Os, -Oz, -Ofast On the other hand, when compiling with -O0, -Og or no -O flag, no warning is triggered (https://godbolt.org/z/Penjbqojh). I've tried to find if only a flag implied by optimization is required, but to no avail.
This is by designed and is even documented: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla-larger-than_003d -Wvla-larger-than=‘PTRDIFF_MAX’ is enabled by default but is typically only effective when -ftree-vrp is active (default for -O2 and above).
(In reply to Andrew Pinski from comment #1) > This is by designed and is even documented: > https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index- > Wvla-larger-than_003d > > -Wvla-larger-than=‘PTRDIFF_MAX’ is enabled by default but is typically only > effective when -ftree-vrp is active (default for -O2 and above). Passing `-ftree-vrp` doesn't activate it either
(In reply to Jakub Łukasiewicz from comment #2) > Passing `-ftree-vrp` doesn't activate it either Because of another part of the documentation: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options Most optimizations are completely disabled at -O0 or if an -O level is not set on the command line, even if individual optimization flags are specified. Similarly, -Og suppresses many optimization passes.
(In reply to Andrew Pinski from comment #3) > (In reply to Jakub Łukasiewicz from comment #2) > > Passing `-ftree-vrp` doesn't activate it either > > Because of another part of the documentation: > https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options > > Most optimizations are completely disabled at -O0 or if an -O level is not > set on the command line, even if individual optimization flags are > specified. Similarly, -Og suppresses many optimization passes. So there is not way of detecting automatic VLA in "debug release"?
(In reply to Jakub Łukasiewicz from comment #4) > So there is not way of detecting automatic VLA in "debug release"? -Wvla works. https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla
(In reply to Andrew Pinski from comment #5) > (In reply to Jakub Łukasiewicz from comment #4) > > So there is not way of detecting automatic VLA in "debug release"? > > -Wvla works. > > https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla But also marks desired VLA (like in function parameters) $ gcc -std=c99 -Wvla -c foo.c foo.c:1:1: warning: ISO C90 forbids variable length array [-Wvla] 1 | int bar(int n, int[n]); | ^~~ foo.c: In function ‘foo’: foo.c:5:5: warning: ISO C90 forbids variable length array ‘arr’ [-Wvla] 5 | int arr[n]; | ^~~
Yes and that is still a VLA in terms of C99 definition ...
Note GCC's C++ front-end even rejects VLA definitions like that for parameters so I don't know how useful they are really.
(In reply to Andrew Pinski from comment #7) > Yes and that is still a VLA in terms of C99 definition ... Indeed, but we only want to prevent automatic VLA, all other instances of VM types are even encouraged. (In reply to Andrew Pinski from comment #8) > Note GCC's C++ front-end even rejects VLA definitions like that for > parameters so I don't know how useful they are really. The project is pure C and we really don't plan to convert it to C++ any time. At the same time, (recently added) diagnostics for size checking array parameter (https://godbolt.org/z/fdbheM8G9) could be really beneficial. Also, allocating on heap multidimensional arrays of unknown size is really convenient: int (*arr)[n][m] = malloc(sizeof *arr);