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

            Bug ID: 122764
           Summary: -Warray-bounds false negative
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

alx@devuan:~/tmp$ cat bo.c 
void f(int n, int a[n])
{
        a[n + 100] = 42;
}
void g(int n, int (*a)[n])
{
        (*a)[n + 100] = 42;
}
void h(int a[10])
{
        a[10 + 100] = 42;
}
void i(int (*a)[10])
{
        (*a)[10 + 100] = 42;
}
alx@devuan:~/tmp$ gcc -Wall -Wextra -O3 -fanalyzer -S bo.c 
bo.c: In function ‘h’:
bo.c:11:10: warning: array subscript 110 is outside array bounds of ‘int[10]’
[-Warray-bounds=]
   11 |         a[10 + 100] = 42;
      |         ~^~~~~~~~~~
bo.c:9:12: note: at offset 440 into object ‘a’ of size [0, 40]
    9 | void h(int a[10])
      |        ~~~~^~~~~
bo.c: In function ‘i’:
bo.c:15:13: warning: array subscript 110 is above array bounds of ‘int[10]’
[-Warray-bounds=]
   15 |         (*a)[10 + 100] = 42;
      |         ~~~~^~~~~~~~~~
bo.c:13:14: note: while referencing ‘a’
   13 | void i(int (*a)[10])
      |        ~~~~~~^~~~~~


Would it be possible to diagnose this?  The compiler doesn't know the value of
n, but it should know that n+100 is not within the bounds of the array.

Reply via email to