[Bug rtl-optimization/49330] Integer arithmetic on addresses optimised with pointer arithmetic rules

ch3root at openwall dot com gcc-bugzilla@gcc.gnu.org
Mon Dec 30 12:43:00 GMT 2019


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

--- Comment #28 from Alexander Cherepanov <ch3root at openwall dot com> ---
I see the same even with pure pointers. I guess RTL doesn't care about such
differences but it means the problem could bite a relatively innocent code.

----------------------------------------------------------------------
#include <stdio.h>

__attribute__((noipa)) // imagine it in a separate TU
static int *opaque(int *p) { return p; }

int main()
{
    static int x, y;

    int *r = opaque(&x) + (opaque(&y) - &y);

    x = 1;
    *r = 2;
    printf("x = %d\n", x);
}
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
x = 2
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
x = 1
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.0 20191230 (experimental)
----------------------------------------------------------------------


More information about the Gcc-bugs mailing list