public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108720] New: INT_MIN * -1 is incorrect
@ 2023-02-08 16:41 amk7r at mtmail dot mtsu.edu
  2023-02-08 16:43 ` [Bug c/108720] " amk7r at mtmail dot mtsu.edu
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: amk7r at mtmail dot mtsu.edu @ 2023-02-08 16:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108720
           Summary: INT_MIN * -1 is incorrect
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: amk7r at mtmail dot mtsu.edu
  Target Milestone: ---

Created attachment 54434
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54434&action=edit
Attached is a file with the source code (.c) and the preprocessed file (.i)

Version of GCC: 11.3.0
System type: Ubuntu 22.04.1 LTS
Options given when GCC was configured/built: ../src/configure -v
--with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04'
--with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-11
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet
--with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=2

Complete command line that triggers the bug: gcc err_file.c

The compiler output (error messages, warnings, ect.): n/a; there were no errors
or warnings
Expected behavior: 
   The program should print out the value of y, and then print out “true 4” 
   because -INT_MIN  is equal to INT_MIN, which is less than 0.
Actual behavior: 
   The program prints out the value of y, and then prints out “true 2.”  
   Case 1: y>= 0. "true 1" should not be printed because y is INT_MIN.  -> Test
Pass
   Case 2: (x-1)>= 0. "true 2" should not be printed because x is INT_MIN. ->
Test Fail
     o INT_MIN * -1 = INT_MIN.  
   Case 3: (x-1)>= zero. "true 3" should not be printed because x is INT_MIN. 
-> Test Pass
     o INT_MIN * -1 = INT_MIN and int zero has 0 
   Case 4: (x-1)< 0. "true 4" should be printed because x is INT_MIN. 
     o INT_MIN * -1 = INT_MIN and INT_MIN is less than 0 -> Test Fail

More information:
If you translate the C code into assembly code using the command “gcc -s
err_file.c,” and check the .L2 block: 
In .L2:
The assembly code should be:                                                  
#equivalent c code
cmpl $0, -8(%rbp)     #because it has the result of x * -1                     
 if ((x*-1)>=0)  
js .L3                #instead of jg .L3 

Explanation:
        * x *-1 is INT_MIN. 
        * “jg” instruction will skip the statement in “if block” if (x*-1) or
INT_MIN is greater than 0. 
        * Since, (x*-1) or INT_MIN is not greater than 0, the statement in the
“if block” is printed (“true 2”).
        * But “true 2” should only be printed if (x*-1) or INT_MIN is greater
than or equal to 0 (check c code line 9).
        * In this case, doing  “js .L3” would make more sense and will give the
expected result.

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

* [Bug c/108720] INT_MIN * -1 is incorrect
  2023-02-08 16:41 [Bug c/108720] New: INT_MIN * -1 is incorrect amk7r at mtmail dot mtsu.edu
@ 2023-02-08 16:43 ` amk7r at mtmail dot mtsu.edu
  2023-02-08 16:45 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: amk7r at mtmail dot mtsu.edu @ 2023-02-08 16:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Abigail Kelly <amk7r at mtmail dot mtsu.edu> ---
Created attachment 54435
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54435&action=edit
preprocessed file

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

* [Bug c/108720] INT_MIN * -1 is incorrect
  2023-02-08 16:41 [Bug c/108720] New: INT_MIN * -1 is incorrect amk7r at mtmail dot mtsu.edu
  2023-02-08 16:43 ` [Bug c/108720] " amk7r at mtmail dot mtsu.edu
@ 2023-02-08 16:45 ` jakub at gcc dot gnu.org
  2023-02-08 16:47 ` pinskia at gcc dot gnu.org
  2023-02-08 16:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-02-08 16:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
INT_MIN * -1 or -INT_MIN is undefined behavior in C/C++, so any expectations
about it are incorrect.

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

* [Bug c/108720] INT_MIN * -1 is incorrect
  2023-02-08 16:41 [Bug c/108720] New: INT_MIN * -1 is incorrect amk7r at mtmail dot mtsu.edu
  2023-02-08 16:43 ` [Bug c/108720] " amk7r at mtmail dot mtsu.edu
  2023-02-08 16:45 ` jakub at gcc dot gnu.org
@ 2023-02-08 16:47 ` pinskia at gcc dot gnu.org
  2023-02-08 16:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-08 16:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

    int x = 
# 6 "err_file.c" 3 4
           (-0x7fffffff - 1)
# 6 "err_file.c"
                  ;
    int y = x * -1;


x*-1 invokes undefined behavior because signed integer overflow is undefined.
So anything can happen after undefined behavior including but not limited to
inconsistent results.

You can detect signed integer overflow at run time with -fsanitize=undefined.

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

* [Bug c/108720] INT_MIN * -1 is incorrect
  2023-02-08 16:41 [Bug c/108720] New: INT_MIN * -1 is incorrect amk7r at mtmail dot mtsu.edu
                   ` (2 preceding siblings ...)
  2023-02-08 16:47 ` pinskia at gcc dot gnu.org
@ 2023-02-08 16:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-08 16:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also if you want to make signed integer overflow as being defined, you can use
-fwrapv .

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

end of thread, other threads:[~2023-02-08 16:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 16:41 [Bug c/108720] New: INT_MIN * -1 is incorrect amk7r at mtmail dot mtsu.edu
2023-02-08 16:43 ` [Bug c/108720] " amk7r at mtmail dot mtsu.edu
2023-02-08 16:45 ` jakub at gcc dot gnu.org
2023-02-08 16:47 ` pinskia at gcc dot gnu.org
2023-02-08 16:49 ` pinskia 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).