http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49330
Summary: Integer arithmetic on addresses optimised with pointer
arithmetic rules
Product: gcc
Version: 4.6.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
#include <stdint.h>
int x, y;
int main(void) {
uintptr_t px = (uintptr_t) &x;
uintptr_t py = (uintptr_t) &y;
volatile uintptr_t d = px - py;
uintptr_t p = py + d;
x = 1;
*(int *) p = 2;
return x;
}
gcc 4.6(20110603) returns 1 at -O1 or higher. configure options:
--build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --prefix=/usr
--sysconfdir=/etc --program-suffix=-4.6 --enable-languages=c,c++
--enable-checking --enable-build-with-cxx
As far as I can see, this program is perfectly valid and is required to return
2. gcc seems to be optimising on the assumption that an addition to &y will not
result in a pointer to a distinct object (and so stores 2 in y), but that
assumption is only correct for a pointer addition, which the above is not.