I wrote:
> Paul Eggert wrote:
> > +#if (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ \
> > +     && !defined __STDC_NO_VLA__)
> > +# define __ARG_NELTS(n) n
> > +#else
> > +# define __ARG_NELTS(n)
> > +#endif
> 
> I would suggest to add a condition __GNUC__ >= 11 in the #if.
> 
> Rationale: GCC version 10 provides reasonable diagnostics with
> the __access__ attribute, but not with the VLA.

Oops, my suggestion was not right. Instead, what needs to be done
in order to preserve good diagnostics with GCC 10 is to keep the
__access__ attribute, even with the VLA.

Test case:

============================ foo.c ============================

int foo1 (int n, int a[static n]);

int foo1 (int n, int a[static n])
{
  return a[0] + a[n-1];
}

int foo2 (int n, int a[static n])
  __attribute__ ((__access__ (__read_only__, 2, 1)));

int foo2 (int n, int a[static n])
{
  return a[0] + a[n-1];
}

void bar (int x)
{
  int arr[4] = { 1, 2, 3, 4 };
  foo1 (4, arr);
  foo1 (5, arr);
  foo2 (4, arr);
  foo2 (5, arr);
}
===============================================================

$ gcc-version 10.3.0 -Wall -S foo.c
foo.c: In function 'bar':
foo.c:23:3: warning: 'foo2' reading 20 bytes from a region of size 16 
[-Wstringop-overflow=]
   23 |   foo2 (5, arr);
      |   ^~~~~~~~~~~~~
foo.c:12:5: note: in a call to function 'foo2' declared with attribute 
'read_only (2, 1)'
   12 | int foo2 (int n, int a[static n])
      |     ^~~~

So, better use the syntax of foo2 than the one of foo1.

Bruno




Reply via email to