This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/83357] New: [Gcc-optimization] wrong code for elements in a union pointed by two different types of global pointers


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

            Bug ID: 83357
           Summary: [Gcc-optimization] wrong code for elements in a union
                    pointed by two different types of global pointers
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yangyibiao at nju dot edu.cn
  Target Milestone: ---

$ cat small.c
int printf(const char *, ...);

union a {
  int b;
  long c;
} e[] = {{0}, {8}};

int d = 0;
int *f = &e[1].b;
long *g = &e[1].c;

void h() {
  for (; d < 2; d++) {
    *g = 0;
    if (*f)
      return;
  }
  return;
}

int main() {
  h();
  printf("%d\n", d);
}

$ gcc -O0 small.c; ./a.out
2
$ gcc -O3 small.c; ./a.out
0
$ clang -fsanitize=alignment small.c; ./a.out
2

I am not very sure whether there exist any undefined behavior in this code.
When I am using sanitize option in clang, clang can not detect any undefined
behavior in this code.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]