public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98151] New: integer output gives different results with -O2 and -O3
@ 2020-12-05  3:11 bradley_bell at yahoo dot com
  2020-12-05  3:18 ` [Bug c++/98151] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: bradley_bell at yahoo dot com @ 2020-12-05  3:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98151
           Summary: integer output gives different results with -O2 and
                    -O3
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bradley_bell at yahoo dot com
  Target Milestone: ---

Created attachment 49688
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49688&action=edit
bash shell script that creates the souce code, compiles, and runs the test

The attached bash script creates a c++ file called int_output.cpp, then
compiles and runs the program. If you run the script with no arguments you will
get the following usage message:

    usage: ./int_output.sh print optimize
    print:    is either 0 or 1
    optimize: is -O2 or -O3

    If print is 0 and optimize is -O3, the test will fail.
    Otherwise, the test will pass

The program starts with the maximum integer, uses a stringstream to convert it
to a string, and then converts it back to an integer. It then checks that the
result is the maximum integer; i.e., the integer it started with.

If print is 0 (1) the value of the starting and ending integers are printed.
The optimize argument determines the optimization level during the g++
compliation.

The only case that fails is when print is 0 and optimize is -O3.
In addition, if one uses clang++, instead of g++, all the cases give the
correct result.

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

* [Bug c++/98151] integer output gives different results with -O2 and -O3
  2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
@ 2020-12-05  3:18 ` pinskia at gcc dot gnu.org
  2020-12-05  4:07 ` bradley_bell at yahoo dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-12-05  3:18 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
               Host|Linux                       |
                   |5.8.15-301.fc33.x86_64      |
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2020-12-05
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think you have an signed integer overflow happening.
Take a look at:
10 * result + s[index++] - '0'
To avoid the overflow, do instead:
10 * result + (s[index++] - '0')

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

* [Bug c++/98151] integer output gives different results with -O2 and -O3
  2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
  2020-12-05  3:18 ` [Bug c++/98151] " pinskia at gcc dot gnu.org
@ 2020-12-05  4:07 ` bradley_bell at yahoo dot com
  2020-12-05  9:21 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: bradley_bell at yahoo dot com @ 2020-12-05  4:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Brad Bell <bradley_bell at yahoo dot com> ---
That fixed my test result.
Sorry I missed that.

Thanks.

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

* [Bug c++/98151] integer output gives different results with -O2 and -O3
  2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
  2020-12-05  3:18 ` [Bug c++/98151] " pinskia at gcc dot gnu.org
  2020-12-05  4:07 ` bradley_bell at yahoo dot com
@ 2020-12-05  9:21 ` jakub at gcc dot gnu.org
  2020-12-05 13:56 ` redi at gcc dot gnu.org
  2020-12-05 14:50 ` bradley_bell at yahoo dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-05  9:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
There is another UB in it, if you try to parse the INT_MIN value there is
another signed integer overflow, because 0x80000000 is not representable in
int, while -0x7fffffff-1 is.
Better to compute the result in unsigned type and only at the end cast to int.

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

* [Bug c++/98151] integer output gives different results with -O2 and -O3
  2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
                   ` (2 preceding siblings ...)
  2020-12-05  9:21 ` jakub at gcc dot gnu.org
@ 2020-12-05 13:56 ` redi at gcc dot gnu.org
  2020-12-05 14:50 ` bradley_bell at yahoo dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-12-05 13:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Compiling with -fsanitize=undefined (as suggested by the bug reporting
guidelines) would have shown the undefined behaviour.

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

* [Bug c++/98151] integer output gives different results with -O2 and -O3
  2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
                   ` (3 preceding siblings ...)
  2020-12-05 13:56 ` redi at gcc dot gnu.org
@ 2020-12-05 14:50 ` bradley_bell at yahoo dot com
  4 siblings, 0 replies; 6+ messages in thread
From: bradley_bell at yahoo dot com @ 2020-12-05 14:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Brad Bell <bradley_bell at yahoo dot com> ---
Compiling with
    g++ -fsanitize=undefined -O2 int_output.cpp -o int_output
does in fact give a very useful message

I scanned the page
    https://www.gnu.org/software/gcc/bugs/
and looked for things that might be related to my bug.
Sorry I missed the most important flag (for my case) at the top.

It might help others to have the individual compiler flags in bold or in a
list.


Thanks again
Brad.

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

end of thread, other threads:[~2020-12-05 14:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-05  3:11 [Bug c++/98151] New: integer output gives different results with -O2 and -O3 bradley_bell at yahoo dot com
2020-12-05  3:18 ` [Bug c++/98151] " pinskia at gcc dot gnu.org
2020-12-05  4:07 ` bradley_bell at yahoo dot com
2020-12-05  9:21 ` jakub at gcc dot gnu.org
2020-12-05 13:56 ` redi at gcc dot gnu.org
2020-12-05 14:50 ` bradley_bell at yahoo dot com

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