https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93052
--- Comment #1 from Alexander Cherepanov <ch3root at openwall dot com> ---
Example with a past-the-end pointer (vrp1, similar to but 93051, comment 0 but
this time with PHI):
----------------------------------------------------------------------
#include <stdio.h>
__attribute__((noipa,optnone)) // imagine it in a separate TU
static void *opaque(void *p) { return p; }
static int been_there = 0;
static int *f(int *p, int *q)
{
if (p == q) {
been_there = 1;
return p;
} else {
been_there = 0;
return q;
}
}
int main()
{
int x[5];
int y[1];
int *p = x;
int *q = y + 1;
opaque(q);
int *p1 = opaque(p); // prevents early optimization of x==y+1
int *r = f(p1, q);
if (been_there) {
*p = 1;
*r = 2;
printf("result: %d\n", *p);
}
}
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra -Wno-attributes test.c && ./a.out
result: 2
$ gcc -std=c11 -pedantic -Wall -Wextra -Wno-attributes -O3 test.c && ./a.out
test.c: In function ‘main’:
test.c:33:9: warning: array subscript 1 is outside array bounds of ‘int[1]’
[-Warray-bounds]
33 | *r = 2;
| ^~
test.c:22:9: note: while referencing ‘y’
22 | int y[1];
| ^
result: 1
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.0 20191223 (experimental)
----------------------------------------------------------------------