https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83172

--- Comment #4 from Hao Hou <hao.hou at utah dot edu> ---
(In reply to Eric Botcazou from comment #3)
> > The same result:
> > 
> > $ gcc-7 -Wvla-larger-than=128 -Wstack-usage=102400 -O0 -c t.c 
> > t.c: In function ‘stack_usage_only’:
> > t.c:23:5: warning: stack usage might be unbounded [-Wstack-usage=]
> >  int stack_usage_only(unsigned x)
> >      ^~~~~~~~~~~~~~~~
> > t.c: In function ‘alloca_fails_even_with_const’:
> > t.c:32:5: warning: stack usage might be unbounded [-Wstack-usage=]
> >  int alloca_fails_even_with_const()
> >      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > -O1 results the same.
> 
> Try -Wvla-larger-than=100 though.
> 
> In any case, note that for:
> 
> int vla_size_only(unsigned x)
> {
>  if(x > 128) __builtin_unreachable();
>  char buf[x];
>  do_something(buf);
>  return 0;
> }
> 
> the warning is expected since the code may allocate more than 128 bytes.
> 
> -Wstack-usage is designed to be *conservatively* correct and to yield the
> same result at all optimization levels, i.e. it will never say that the
> stack usage is bounded if there is a path where it may not be.  So it's very
> different from 
> -Wvla-larger-than or -Walloca-larger-than which say nothing at -O0 or -O1
> and are not conservatively correct.

Thanks Eric, that's a good point. I understand that eventhough the behavior of
the code when x > 128 is undefined, but it's up to the compiler if take this
case into consideration. 

I tried to modify the code a little bit: 

int stack_usage_only(unsigned x)
{
        if(x <= 128)
        {
                if(x > 128) __builtin_unreachable();
                char buf[x];
                do_something(buf);
        }
        return 0;
}


The warning is stil there. I totally understand that the compiler infers the
size conservatively. But this case makes -Wstack-size= somehow equivalent to
-Wvla.

My idea on this warning is that it actually make much more sense when the VLA
or alloca has been used in the code, since it preventing VLA or alloca
allocating unbounded size of memory. That is why I was expecting it actually
infers the range of the x, thus it's an useful indicator of using VLA
correctly.

Reply via email to