public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99162] New: problem when compile with O2 or O3
@ 2021-02-19 12:19 haibin-zhang at 263 dot net
  2021-02-19 13:28 ` [Bug c++/99162] " pinskia at gcc dot gnu.org
  2021-02-19 14:00 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: haibin-zhang at 263 dot net @ 2021-02-19 12:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99162
           Summary: problem when compile with O2 or O3
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haibin-zhang at 263 dot net
  Target Milestone: ---

$ g++ --version
g++ (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat a.C
#include <stdlib.h>
#include <stdio.h>
void f(int n) {
   unsigned int u = abs(n);
   while(true) {
       if(u < 10) {
          printf("u = %u\n", u);
          break;
       }
       printf("u = %u\n", u);
       u /= 10;
   }
}
main() {
   f(0x80000000);
}

//////////////////////// 02 or 03 can not work ////////////////////////

$ g++ a.C -O2
$ ./a.out
u = 2147483648

//////////////////////// O1 no problem ////////////////////////////////
$ g++ a.C -O1
$ ./a.out
u = 2147483648
u = 214748364
u = 21474836
u = 2147483
u = 214748
u = 21474
u = 2147
u = 214
u = 21
u = 2

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

* [Bug c++/99162] problem when compile with O2 or O3
  2021-02-19 12:19 [Bug c++/99162] New: problem when compile with O2 or O3 haibin-zhang at 263 dot net
@ 2021-02-19 13:28 ` pinskia at gcc dot gnu.org
  2021-02-19 14:00 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-02-19 13:28 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Signed integer overflow is undefined behavior.  Abs of most negative signed
integer is undefined because there is no way to represent the positive value so
it causes an overflow.

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

* [Bug c++/99162] problem when compile with O2 or O3
  2021-02-19 12:19 [Bug c++/99162] New: problem when compile with O2 or O3 haibin-zhang at 263 dot net
  2021-02-19 13:28 ` [Bug c++/99162] " pinskia at gcc dot gnu.org
@ 2021-02-19 14:00 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2021-02-19 14:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Zhang Haibin from comment #0)
> $ g++ --version
> g++ (GCC) 4.9.2

This compiler is ancient and we're not interested in bug reports for it.

If you use a modern compiler you can use -fsanitize=undefined to find bugs like
this integer overflow problem.

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

end of thread, other threads:[~2021-02-19 14:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-19 12:19 [Bug c++/99162] New: problem when compile with O2 or O3 haibin-zhang at 263 dot net
2021-02-19 13:28 ` [Bug c++/99162] " pinskia at gcc dot gnu.org
2021-02-19 14:00 ` redi 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).