Bug 109395 - -Wvla-larger-than has no effect when compiling without optimizations
Summary: -Wvla-larger-than has no effect when compiling without optimizations
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 12.2.1
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2023-04-03 21:05 UTC by Jakub Łukasiewicz
Modified: 2023-05-01 21:42 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Łukasiewicz 2023-04-03 21:05:34 UTC
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.
Comment 1 Andrew Pinski 2023-04-03 21:07:52 UTC
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).
Comment 2 Jakub Łukasiewicz 2023-04-03 21:13:39 UTC
(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
Comment 3 Andrew Pinski 2023-04-03 21:15:03 UTC
(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.
Comment 4 Jakub Łukasiewicz 2023-04-03 21:17:49 UTC
(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"?
Comment 5 Andrew Pinski 2023-04-03 21:22:41 UTC
(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
Comment 6 Jakub Łukasiewicz 2023-04-03 21:26:43 UTC
(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];
          |     ^~~
Comment 7 Andrew Pinski 2023-04-03 21:29:25 UTC
Yes and that is still a VLA in terms of C99 definition ...
Comment 8 Andrew Pinski 2023-04-03 21:31:01 UTC
Note GCC's C++ front-end even rejects VLA definitions like that for parameters so I don't know how useful they are really.
Comment 9 Jakub Łukasiewicz 2023-04-03 21:41:41 UTC
(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);