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

            Bug ID: 92765
           Summary: [10 Regression] Wrong code caused by folding of
                    -Wstring-compare since r276773
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
  Target Milestone: ---

I've just isolated following code snippet from tar package:

$ cat buffer.c
struct posix_header {
  char magic[2];
};

union Union {
  char buffer[512];
  struct posix_header header;
};

union Union a;
union Union *ptr;

int main(int argc, char **argv)
{
  a.buffer[0] = 'a';
  a.buffer[1] = 'b';
  a.buffer[2] = '\0';

  if (argc != 123)
    ptr = &a;

  if (__builtin_strcmp(ptr->header.magic, "x") == 0
      || __builtin_strcmp(ptr->buffer + __builtin_offsetof(struct posix_header,
magic), "ab") == 0)
    ;
  else
    __builtin_abort ();

  return 0;
}

$ gcc -Wstring-compare buffer.c -O2 && ./a.out
buffer.c: In function ‘main’:
buffer.c:23:10: warning: ‘__builtin_strcmp’ of a string of length 2 and an
array of size 2 evaluates to nonzero [-Wstring-compare]
   23 |       || __builtin_strcmp(ptr->buffer + __builtin_offsetof(struct
posix_header, magic), "ab") == 0)
      |         
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Aborted (core dumped)

$ gcc -Wstring-compare buffer.c && ./a.out
[exits with 0]

Reply via email to