public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96007] New: -O2 miscompiles memcmp an object with a string literal containing '\0'
@ 2020-06-30 15:10 jerryfromearth at gmail dot com
  2020-06-30 15:17 ` [Bug tree-optimization/96007] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jerryfromearth at gmail dot com @ 2020-06-30 15:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96007
           Summary: -O2 miscompiles memcmp an object with a string literal
                    containing '\0'
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jerryfromearth at gmail dot com
  Target Milestone: ---

Hi,

While comparing an object to a literal string that contains "\0" (e.g.
memcmp((&u128), "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)), -O2 produces
incorrect result, while -O1 and -O0 produces correct result.

This affects at least gcc 9.2, 9.3 and 10.1.
gcc 9.1 is fine.

```
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct tagMYUINT128
{
  unsigned long u64High;
  unsigned long u64Low;
} MYUINT128, *PMYUINT128;

int main(int argc, char **argv) {
  MYUINT128 u128 = {0};
  char arr[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  printf("All results are expected to be non-zero.\n");

  // Test A, u128={0x0, 0x1234}
  printf("Test A. u128={0x0, 0x1234}\n");
  u128.u64High = 0x0;
  u128.u64Low  = 0x1234;

  // Comparing to a literal string is broken.
  printf("memcmp1 result=%d\n", memcmp((&u128),
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));
  printf("memcmp2 result=%d\n", __builtin_memcmp((&u128),
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));

  // Comparing to a array pointer works.
  printf("memcmp3 result=%d\n", memcmp((&u128), arr, 16));
  printf("memcmp4 result=%d\n", __builtin_memcmp((&u128), arr, 16));

  return 0;
}

$ gcc -O2 -o test test.c && ./test
All results are expected to be non-zero.
Test A. u128={0x0, 0x1234}
memcmp1 result=0
memcmp2 result=0
memcmp3 result=52
memcmp4 result=52

$ gcc -O1 -o test test.c && ./test
All results are expected to be non-zero.
Test A. u128={0x0, 0x1234}
memcmp1 result=52
memcmp2 result=52
memcmp3 result=52
memcmp4 result=52
```

godbolt link: https://godbolt.org/z/9M3L8h (it contains more tests)

I looked into the list of bugs fixed in 9.2 and found this probably relevant
issue: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90892

Cheers,
Jiarui Hong

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-07-01 14:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-30 15:10 [Bug tree-optimization/96007] New: -O2 miscompiles memcmp an object with a string literal containing '\0' jerryfromearth at gmail dot com
2020-06-30 15:17 ` [Bug tree-optimization/96007] " jakub at gcc dot gnu.org
2020-06-30 15:34 ` marxin at gcc dot gnu.org
2020-06-30 16:14 ` msebor at gcc dot gnu.org
2020-07-01  6:28 ` marxin at gcc dot gnu.org
2020-07-01  6:49 ` [Bug tree-optimization/96007] [9/10/11 Regression] " rguenth at gcc dot gnu.org
2020-07-01 14:46 ` [Bug middle-end/96007] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).