[Bug other/85778] New: unexpected results with -O2, wrong code?

doko at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 14 18:19:00 GMT 2018


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

            Bug ID: 85778
           Summary: unexpected results with -O2, wrong code?
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: doko at gcc dot gnu.org
  Target Milestone: ---

seen with GCC 5 and all newer version. Is this wrong-code?

$ cat testcase.c 
#include <stdio.h>
#include <sys/stat.h>

#define PRINT \
  if (foo == NULL) \
    printf("foo in func() is %p, NULL (expected)\n", foo); \
  else \
    printf("foo in func() is %p, not NULL (NOT EXPECTED!)\n", foo);

void func(const char *foo) {
  struct stat stat_buf;

  PRINT;
  stat(foo, &stat_buf);
  PRINT;
}

int main() {
  const char *foo = NULL;
  struct stat stat_buf;

  func(NULL);

  PRINT;
  stat(foo, &stat_buf);
  PRINT;
  return 0;
}

$ gcc -O0 testcase.c && ./a.out 
foo in func() is (nil), NULL (expected)
foo in func() is (nil), NULL (expected)
foo in func() is (nil), NULL (expected)
foo in func() is (nil), NULL (expected)
$ gcc -O2 testcase.c && ./a.out 
foo in func() is (nil), NULL (expected)
foo in func() is (nil), not NULL (NOT EXPECTED!)
foo in func() is (nil), NULL (expected)
foo in func() is (nil), NULL (expected)


More information about the Gcc-bugs mailing list