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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |msebor at gcc dot gnu.org
         Resolution|---                         |WONTFIX

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
When reporting bugs please provide all the information requested here:
  https://www.gnu.org/software/gcc/bugs/#need

The warning was introduced as a result of r262893.  It's caused by a confluence
of two factors:
1) the upper bound of the loop having a limited range (i.e., a subrange of
ptrdiff_t/size_t -- compiling with -m32 suppresses it), and
2) the optimizer's necessary assumption that operator new may modify this->N.

The avoid the warning either use N_ as the upper bound of the loop or change
the type of S::N to size_t.

The only way for GCC to avoid the warning would be to avoid assuming (for the
purposes of the warning only) that operator new and other similar functions(*)
clobber memory.  That might be possible but I'm not sure how intrusive a change
it would be or whether it's a good idea.  So for now, I'm going to resolve this
as WONTFIX.

[*] Another function that GCC assumes clobbers memory is printf, even though in
most cases it doesn't.  It assumes that because the function can, in fact,
clobber memory via the %n format directive and GCC doesn't scan the format
string to determine whether or not it contains the directive.  (The function
can also clobber memory indirectly by calling a registered printf hook but
that's outside the scope of what GCC needs to consider).

$ cat pr91168.c && gcc -O2 -S -Wall -Wextra -Wpedantic pr91168.c
int f (int *n)
{
  const int a[] = { 1, 2 };
  int x = 0;
  *n = 2;

  __builtin_printf ("n = %p\n", (void*)n);

  for (int i = 1; i < *n - 1; i++)
    x = a[i + 1];

  return x;
}
pr91168.c: In function ‘f’:
pr91168.c:10:10: warning: array subscript 2 is above array bounds of ‘const
int[2]’ [-Warray-bounds]
   10 |     x = a[i + 1];
      |         ~^~~~~~~
pr91168.c:3:13: note: while referencing ‘a’
    3 |   const int a[] = { 1, 2 };
      |             ^

Reply via email to