https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94675
--- Comment #11 from Martin Sebor <msebor at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #3)
> (In reply to Martin Sebor from comment #1)
> > The false positive is not due a shortcoming of the warning but rather due to
> > GCC not having a sufficiently sophisticated analysis of relationships of
> > pointers into the same objects. The same warning (and probably a numbers as
> > well) can be reproduced with a simpler example.
> >
> > $ cat pr94675.c && gcc -O2 -S -Wall -fdump-tree-vrp=/dev/stdout pr94675.c
> > unsigned char c, n;
> >
> > int f (void)
> > {
> > if (n <= 7) return 0;
> >
> > unsigned char *p = &c, *q = p + n;
> >
> > if (q - p <= 7) // not eliminated
> > return 0;
>
> Not sure why you write not eliminated - it is eliminated. I believe
> your testcase is bogus - why would the p[7] access never happen?
> Because p + n is invoking undefined behavior?
I may not have entirely accurately describe what happens in the small test case
but yes, p + n is undefined for values of n > 1, so the test (either of them)
can be assumed to be true and the dereference can be eliminated. The following
might be better:
unsigned char c, n;
int f (void)
{
unsigned char *p = &c, *q = p + n;
if (q - p <= 7) // not eliminated but could be
return 0;
return p[7]; // spurious -Warray-bounds
}