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

--- Comment #16 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Chung-Kil Hur from comment #15)
> 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. */

Wrong, j == i != 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