[Bug tree-optimization/65752] Too strong optimizations int -> pointer casts

gil.hur at sf dot snu.ac.kr gcc-bugzilla@gcc.gnu.org
Tue May 19 15:29:00 GMT 2015


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

--- Comment #24 from Chung-Kil Hur <gil.hur at sf dot snu.ac.kr> ---
(In reply to schwab from comment #23)
> "gil.hur at sf dot snu.ac.kr" <gcc-bugzilla@gcc.gnu.org> writes:
> 
> > Since "hello" is not printed, I think the if-statement is the same as no-op.
> > Thus, removing the if-statement should not change the behavior of the program
> > according to ISO C11.
> 
> Unless you are invoking undefined behaviour.
> 
> Andreas.

==============================
#include <stdio.h>

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

  for (i = 0; i < xp; i++) { }
  j = i;

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

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

This prints "15".
And I do not think there is any UB.
Please correct me if I am wrong.

Then, I add the if-statement.

==============================
#include <stdio.h>

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

  for (i = 0; i < xp; i++) { }
  j = i;

  /****** begin *******/
  if (j != xp) { 
    printf("hello\n");
    j = xp; 
  }
  /****** end *********/

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

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

This prints "0" without printing "hello".

Thus, this raises no UB unless "j != xp" raises UB.

If you think "j != xp" raises UB, please explain why and give some reference.

Otherwise, I think it is a bug of GCC.



More information about the Gcc-bugs mailing list