[Bug tree-optimization/61502] == comparison on "one-past" pointer gives wrong result

post+gcc at ralfj dot de via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61502

--- Comment #49 from post+gcc at ralfj dot de ---
While checking whether some LLVM bugs also occur in GCC, I stumbled upon
another variant of this bug:

#include <stdio.h>
#include <stdint.h>

int main()
{
  // also try swapping these
  int x = 7777; 
  int y = 6666;

  int* p = &y+1;
  int* q = &x;

  if (q == p) {
    printf("pointers are equal!\n");
  } else {
    printf("pointers are NOT equal!\n");
  }

  uintptr_t dist = (((uintptr_t)p) - ((uintptr_t)q));
  if (dist == 0) {
    printf("Distance is 0 (i.e., poiners are equal)\n");
  }

  return 0;
}

(https://c.godbolt.org/z/TdGq4q5bK)

This prints

pointers are NOT equal!
Distance is 0 (i.e., poiners are equal)

which is logically inconsistent, i.e., a miscompilation. The pointers may be
equal or not, but they can't be sometimes equal and sometimes not just because
you look at them from a different direction.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.