On 27/02/2026 19:57, Martin Uecker via Gcc wrote:
Am Freitag, dem 27.02.2026 um 19:40 +0100 schrieb David Brown:
On 27/02/2026 18:51, Martin Uecker via Gcc wrote:

<snip>

No, it does not trigger a warning with those values.  (It will if
someone changes MAX_POINTS to 250000, but that's the point of the
-Wvla-larger-than=512000.)


You are right, thank you. I was somehow confusing this with
something else.


You showed that code in your other post:

        void foo(int n)
        {
          if (n > 100) return;
          char buf[n];
          foo(n);
        }

I would say that definitely deserves a warning. Either make the parameter unsigned, or check if for negative values. (Maybe the potential infinite recursion should have a warning too, but I'm guessing that's an unintentional blip!)



<https://godbolt.org/z/xPo7ocWfr>


One thing I have noticed about all this is the weird error messages.
With the code "foo" above, but the -Wvla warning enabled, the error
message is :

"""
warning: ISO C90 forbids variable length array 'v' [-Wla]
"""

That is despite using -std=c23.

Even stranger, it changes when using "const int n = MAX_POINTS;" (in
code like this, "n" should be declared "const") :

"""
warning: ISO C90 forbids array 'v' whose size cannot be evaluated [-Wla]
"""

The same warning is given if "const int n = MAX_POINTS;" is moved to
file-scope - even though that requires that the initialiser for "n" can
not only be evaluated, but must be a constant expression and the
compiler knows the value of "n" can never be changed (without UB).



So there is scope for improvement on the wording of the warnings here!

I agree.  Do you have a suggestion?

Martin


How about just :

        warning: Variable length array 'v' [-Wvla]

You will only see the warning if you specifically enable it - you are getting the warning because /you/ choose to enable the warning, not because ISO C90 forbids it. (And ISO C90 does not "forbid" VLAs - it just doesn't have the concept.)

The same message could be given if "n" is "const" - it does not seem to me that there should be a difference here.

Alternatively, you could have two levels of the warning - -Wvla=1 which warns on VLAs with a size not known until runtime, and -Wvla=2 which also warns on arrays with sizes known at compile-time but which are still VLAs in C. (Perhaps following the C++ rules, to give an optimisation-independent definition.) For consistency, -Wvla alone would default to -Wvla=2.

Then you would have two warnings :

warning: Dynamically sized variable length array [-Wvla=1]

warning: Const sized array is variable length array in C [-Wvla=2]




Reply via email to