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

--- Comment #15 from Chung-Kil Hur <gil.hur at sf dot snu.ac.kr> ---
Hi Richard,

Thanks for the explanation.
But, what I wonder was how to justify such an optimization, rather than how it
works.

I have a better example. This might be a real bug of GCC.

#include <stdio.h>

int main() {
  int x = 0;
  uintptr_t pi = (uintptr_t) &x;
  uintptr_t i, j;

  for (i = 0; i < pi; i++) { }
  j = i;
  /* Note that the following "if" statement is never executed because j == pi.
*/
  if (j != pi) {
    j = pi;
  }

  *(int*)((pi+i)-j) = 15;

  printf("%d\n", x);
}

This program prints out "0" instead of "15".
Here, "pi" contains the address of the variable x; and "i" and "j" contain the
same integer.
So, it seems that "(pi+i)-j" should have a proper provenance of "x" and thus
the variable "x" should be updated to 15.
However, GCC seems to think that "(pi+i)-j" has no provenance.

So, as a programmer, I wonder how I should calculate the provenance of an
integer in order to see whether casting it to a pointer is valid or not.

Thanks.

Reply via email to