public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu
@ 2023-09-07  7:38 shaohua.li at inf dot ethz.ch
  2023-09-07  7:40 ` [Bug c/111319] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-09-07  7:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111319
           Summary: Wrong code at -O2 on x86_64-linux-gnu
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
  Target Milestone: ---

The executable produced by gcc -O2 would crash. Not sure if there is UB in the
code or not as `h` has not been dereferenced after `h--`.

Compiler explorer: https://godbolt.org/z/avssssPze

$ cat a.c
int printf(const char *, ...);
int a, b = 5, c, d;
int e[2] = {0, 0};
int i() {
  int f = 0;
  int g[2] = {1, f};
  int *h = g;
  h--;
  if (!b)
    while (h >= &c == 1)
      h--;
  h++;
  d = *h;
  return d;
}
int main() {
  if (e[i()])
    e[0] = 0;
  printf("%d\n", a);
}
$
$ gcc -O2 a.c && ./a.out
Segmentation fault
$

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

* [Bug c/111319] Wrong code at -O2 on x86_64-linux-gnu
  2023-09-07  7:38 [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu shaohua.li at inf dot ethz.ch
@ 2023-09-07  7:40 ` pinskia at gcc dot gnu.org
  2023-09-07  7:59 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-07  7:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---

  int *h = g;
  h--;

is undefined right away ...

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

* [Bug c/111319] Wrong code at -O2 on x86_64-linux-gnu
  2023-09-07  7:38 [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu shaohua.li at inf dot ethz.ch
  2023-09-07  7:40 ` [Bug c/111319] " pinskia at gcc dot gnu.org
@ 2023-09-07  7:59 ` pinskia at gcc dot gnu.org
  2023-09-07  8:01 ` xry111 at gcc dot gnu.org
  2023-09-07  8:09 ` shaohua.li at inf dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-07  7:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also comparing unrelated addresses with >= is undefined.

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

* [Bug c/111319] Wrong code at -O2 on x86_64-linux-gnu
  2023-09-07  7:38 [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu shaohua.li at inf dot ethz.ch
  2023-09-07  7:40 ` [Bug c/111319] " pinskia at gcc dot gnu.org
  2023-09-07  7:59 ` pinskia at gcc dot gnu.org
@ 2023-09-07  8:01 ` xry111 at gcc dot gnu.org
  2023-09-07  8:09 ` shaohua.li at inf dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-09-07  8:01 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
                 CC|                            |xry111 at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Please try not to create more reports with "not sure if there is UB" esp. this
UB should be obvious, and it's also detectable with -fsanitize=undefined:

t.c:13:7: runtime error: load of address 0x7ffd53183020 with insufficient space
for an object of type 'int'
0x7ffd53183020: note: pointer points here
 2a 56 00 00  01 00 00 00 00 00 00 00  00 14 1a ed ca 50 d4 0e  00 00 00 00 00
00 00 00  98 31 18 53
              ^

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

* [Bug c/111319] Wrong code at -O2 on x86_64-linux-gnu
  2023-09-07  7:38 [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu shaohua.li at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-09-07  8:01 ` xry111 at gcc dot gnu.org
@ 2023-09-07  8:09 ` shaohua.li at inf dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-09-07  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Shaohua Li <shaohua.li at inf dot ethz.ch> ---
(In reply to Xi Ruoyao from comment #3)
> Please try not to create more reports with "not sure if there is UB" esp.
> this UB should be obvious, and it's also detectable with
> -fsanitize=undefined:
> 
> t.c:13:7: runtime error: load of address 0x7ffd53183020 with insufficient
> space for an object of type 'int'
> 0x7ffd53183020: note: pointer points here
>  2a 56 00 00  01 00 00 00 00 00 00 00  00 14 1a ed ca 50 d4 0e  00 00 00 00
> 00 00 00 00  98 31 18 53
>               ^

UBSan only detects it at -O2 level and I used `-O0
-fsanitize=address,undefined` to detect if there is any report.

https://godbolt.org/z/zvcG8znvT

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

end of thread, other threads:[~2023-09-07  8:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-07  7:38 [Bug c/111319] New: Wrong code at -O2 on x86_64-linux-gnu shaohua.li at inf dot ethz.ch
2023-09-07  7:40 ` [Bug c/111319] " pinskia at gcc dot gnu.org
2023-09-07  7:59 ` pinskia at gcc dot gnu.org
2023-09-07  8:01 ` xry111 at gcc dot gnu.org
2023-09-07  8:09 ` shaohua.li at inf dot ethz.ch

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