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

* [Bug tree-optimization/96007] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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 ` jakub at gcc dot gnu.org
  2020-06-30 15:34 ` marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-06-30 15:17 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Perhaps related/dup to PR95189?

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

* [Bug tree-optimization/96007] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-06-30 15:34 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |marxin at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |marxin at gcc dot gnu.org
   Last reconfirmed|                            |2020-06-30

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
This one started with my r10-1545-g6aa2e42cb4be16ee, so let me take a look.

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

* [Bug tree-optimization/96007] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-06-30 16:14 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
I suspect it's the same thing -- see bug 95189 comment #7.  I've got a patch in
testing.

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

* [Bug tree-optimization/96007] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-07-01  6:28 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|marxin at gcc dot gnu.org          |msebor at gcc dot gnu.org

--- Comment #4 from Martin Liška <marxin at gcc dot gnu.org> ---
All right, leaving to you Martin.

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

* [Bug tree-optimization/96007] [9/10/11 Regression] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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
                   ` (3 preceding siblings ...)
  2020-07-01  6:28 ` marxin at gcc dot gnu.org
@ 2020-07-01  6:49 ` rguenth at gcc dot gnu.org
  2020-07-01 14:46 ` [Bug middle-end/96007] " msebor at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-07-01  6:49 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Priority|P3                          |P2
            Summary|-O2 miscompiles memcmp an   |[9/10/11 Regression] -O2
                   |object with a string        |miscompiles memcmp an
                   |literal containing '\0'     |object with a string
                   |                            |literal containing '\0'
   Target Milestone|---                         |9.4

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

* [Bug middle-end/96007] [9/10/11 Regression] -O2 miscompiles memcmp an object with a string literal containing '\0'
  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
                   ` (4 preceding siblings ...)
  2020-07-01  6:49 ` [Bug tree-optimization/96007] [9/10/11 Regression] " rguenth at gcc dot gnu.org
@ 2020-07-01 14:46 ` msebor at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-07-01 14:46 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|tree-optimization           |middle-end
         Resolution|---                         |DUPLICATE
             Status|ASSIGNED                    |RESOLVED

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
The patch for pr95189 fixes this as well.  Resolving as a duplicate.

*** This bug has been marked as a duplicate of bug 95189 ***

^ 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).