public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value
@ 2013-01-06 10:50 klaus.wieder at endolandia dot de
  2013-01-06 11:01 ` [Bug c++/55885] " schwab@linux-m68k.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: klaus.wieder at endolandia dot de @ 2013-01-06 10:50 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

             Bug #: 55885
           Summary: Modulo operator crashes for int and long variables if
                    they have minimal value
    Classification: Unclassified
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: klaus.wieder@endolandia.de


Error description in source file
-------------------------------------

Compiled with
-------------------------------------
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.11.00) on MAC OS X Version 10.7.5 (Lion)
-------------------------------------

Result of gcc -v:
-------------------------------------
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/src/configure
--disable-checking --enable-werror
--prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2
--mandir=/share/man --enable-languages=c,objc,c++,obj-c++
--program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/
--with-slibdir=/usr/lib --build=i686-apple-darwin11
--enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/dst-llvmCore/Developer/usr/local
--program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11
--target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
-------------------------------------

Compiler and linker output
-------------------------------------
make all 
Building file: ../src/iotest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -Wextra -c -fmessage-length=0 -MMD -MP -MF"src/iotest.d"
-MT"src/iotest.d" -o "src/iotest.o" "../src/iotest.cpp"
Finished building: ../src/iotest.cpp

Building target: iotest
Invoking: MacOS X C++ Linker
g++  -o "iotest"  ./src/iotest.o   
Finished building target: iotest
-------------------------------------

Compiling with options -fno-strict-aliasing -fwrapv does not make a difference
-------------------------------------

file: iotest.cpp
-------------------------------------
#include <iostream>
#include <limits>

// Assumption: If a is a signed variable, then a % -1 should always be 0
//
// ok    if myType is of type char or short
// fails if myType is of type int  or long

int main() {

    typedef int myType;

    myType c;

    myType a = std::numeric_limits<myType>::min();
    int    b = -1;

        // always ok
    c = a % -1;
    std::cout << a << ", " << b << ", " << c << std::endl;

        // signals EXC ARITHMETIC:Arithmetic exception 
        // if myType is int or is long 
    c = a % b;
        std:: cout << a << ", " << b << ", " << c << std::endl;

    return 0;
}


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
@ 2013-01-06 11:01 ` schwab@linux-m68k.org
  2013-01-06 12:14 ` kwieder at polytechnic dot edu.na
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: schwab@linux-m68k.org @ 2013-01-06 11:01 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

Andreas Schwab <schwab@linux-m68k.org> changed:

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

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> 2013-01-06 11:01:33 UTC ---
INT_MIN % -1 is undefined due to overflow.  -fwrapv has no effect on division
operations.


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
  2013-01-06 11:01 ` [Bug c++/55885] " schwab@linux-m68k.org
@ 2013-01-06 12:14 ` kwieder at polytechnic dot edu.na
  2013-01-06 12:26 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: kwieder at polytechnic dot edu.na @ 2013-01-06 12:14 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

--- Comment #2 from kwieder at polytechnic dot edu.na 2013-01-06 12:14:16 UTC ---
Thanks for the quick response.

Why is    

    INT_MIN  % -1 

ok

and why is 

    int b = -1;
    INT_MIN % b 

not ok.

I"m well aware of  INT_MIN /  -1 is overflowing. If your are using a division
for the % operator, which you probably do, then INT_MIN % -1 should also crash.

Regards,
Klaus 



On Jan 6, 2013, at 1:01 PM, schwab@linux-m68k.org wrote:

> if they have minimal value
> 
> Date: Sun, 06 Jan 2013 11:01:33 +0000
> 
> X-Bugzilla-Reason: Reporter
> 
> X-Bugzilla-Type: changed
> 
> X-Bugzilla-Watch-Reason: None
> 
> X-Bugzilla-Product: gcc
> 
> X-Bugzilla-Component: c++
> 
> X-Bugzilla-Keywords: 
> 
> X-Bugzilla-Severity: normal
> 
> X-Bugzilla-Who: schwab@linux-m68k.org
> 
> X-Bugzilla-Status: RESOLVED
> 
> X-Bugzilla-Priority: P3
> 
> X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
> 
> X-Bugzilla-Target-Milestone: ---
> 
> X-Bugzilla-Changed-Fields: Status Resolution
> 
> Message-ID: <bug-55885-22726-Gx0wFhkedU@http.gcc.gnu.org/bugzilla/>
> 
> In-Reply-To: <bug-55885-22726@http.gcc.gnu.org/bugzilla/>
> 
> References: <bug-55885-22726@http.gcc.gnu.org/bugzilla/>
> 
> X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
> 
> Auto-Submitted: auto-generated
> 
> Content-Type: text/plain; charset="UTF-8"
> 
> MIME-Version: 1.0
> 
> 
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885
> 
> 
> 
> Andreas Schwab <schwab@linux-m68k.org> changed:
> 
> 
> 
>           What    |Removed                     |Added
> 
> ----------------------------------------------------------------------------
> 
>             Status|UNCONFIRMED                 |RESOLVED
> 
>         Resolution|                            |INVALID
> 
> 
> 
> --- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> 2013-01-06 11:01:33 UTC ---
> 
> INT_MIN % -1 is undefined due to overflow.  -fwrapv has no effect on division
> 
> operations.
> 
> 
> 
> -- 
> 
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> 
> ------- You are receiving this mail because: -------
> 
> You reported the bug.


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
  2013-01-06 11:01 ` [Bug c++/55885] " schwab@linux-m68k.org
  2013-01-06 12:14 ` kwieder at polytechnic dot edu.na
@ 2013-01-06 12:26 ` redi at gcc dot gnu.org
  2013-01-06 12:45 ` kwieder at polytechnic dot edu.na
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-01-06 12:26 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-06 12:26:13 UTC ---
Are you asking why undefined behaviour doesn't have consistent results?
http://c-faq.com/ansi/experiment.html


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
                   ` (2 preceding siblings ...)
  2013-01-06 12:26 ` redi at gcc dot gnu.org
@ 2013-01-06 12:45 ` kwieder at polytechnic dot edu.na
  2013-01-07 15:34 ` joseph at codesourcery dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: kwieder at polytechnic dot edu.na @ 2013-01-06 12:45 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

--- Comment #4 from kwieder at polytechnic dot edu.na 2013-01-06 12:44:42 UTC ---
Hi Jonathan,

According to the the standard C++ implementation

(a % -b)  <==>  (a % b), therefore

(INT_MIN % -1)  <==>  (INT_MIN %1) == 0


Neither INT_MIN % -1 nor INT_MIN %1 require a division, therefore there should
be no undefined behaviour in the first place.

Regards, 
Klaus



On Jan 6, 2013, at 2:26 PM, redi at gcc dot gnu.org wrote:

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-06
12:26:13 UTC ---

Are you asking why undefined behaviour doesn't have consistent results?


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
                   ` (3 preceding siblings ...)
  2013-01-06 12:45 ` kwieder at polytechnic dot edu.na
@ 2013-01-07 15:34 ` joseph at codesourcery dot com
  2021-08-22 21:52 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: joseph at codesourcery dot com @ 2013-01-07 15:34 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55885

--- Comment #5 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2013-01-07 15:34:34 UTC ---
INT_MIN % -1 and INT_MIN / -1 *should* be made defined by -fwrapv; that's 
bug 30484.

Without -fwrapv, C11 made explicit the intent that the modulo operation is 
undefined in the cases where division is undefined ("If the quotient a/b 
is representable, the expression (a/b)*b + a%b shall equal a; otherwise, 
the behavior of both a/b and a%b is undefined."); I don't know about C++.


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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
                   ` (4 preceding siblings ...)
  2013-01-07 15:34 ` joseph at codesourcery dot com
@ 2021-08-22 21:52 ` pinskia at gcc dot gnu.org
  2021-08-22 21:53 ` pinskia at gcc dot gnu.org
  2021-08-22 21:56 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-22 21:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.seifert at de dot ibm.com

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 93013 has been marked as a duplicate of this bug. ***

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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
                   ` (5 preceding siblings ...)
  2021-08-22 21:52 ` pinskia at gcc dot gnu.org
@ 2021-08-22 21:53 ` pinskia at gcc dot gnu.org
  2021-08-22 21:56 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-22 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note PR 30484 is for the -fwrapv issue with %.

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

* [Bug c++/55885] Modulo operator crashes for int and long variables if they have minimal value
  2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
                   ` (6 preceding siblings ...)
  2021-08-22 21:53 ` pinskia at gcc dot gnu.org
@ 2021-08-22 21:56 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-22 21:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Eric.Deplagne at nerim dot net

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 29511 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2021-08-22 21:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-06 10:50 [Bug c++/55885] New: Modulo operator crashes for int and long variables if they have minimal value klaus.wieder at endolandia dot de
2013-01-06 11:01 ` [Bug c++/55885] " schwab@linux-m68k.org
2013-01-06 12:14 ` kwieder at polytechnic dot edu.na
2013-01-06 12:26 ` redi at gcc dot gnu.org
2013-01-06 12:45 ` kwieder at polytechnic dot edu.na
2013-01-07 15:34 ` joseph at codesourcery dot com
2021-08-22 21:52 ` pinskia at gcc dot gnu.org
2021-08-22 21:53 ` pinskia at gcc dot gnu.org
2021-08-22 21:56 ` 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).