public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers
@ 2011-01-28  8:38 yacwroy at gmail dot com
  2011-01-28 11:20 ` [Bug c++/47504] " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: yacwroy at gmail dot com @ 2011-01-28  8:38 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: some constexpr calculations erroneously overflow when
                    using negative numbers
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: yacwroy@gmail.com
            Target: x86_64-unknown-linux-gnu


Created attachment 23148
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23148
overflow.cpp

Easiest to describe via code:

overflow.cpp
=================================================
char constexpr sub(char arg)
  { return char(arg - char(1)); }

int main()
  { static char constexpr m = sub(-1); }
=================================================
g++ overflow.cpp --std=c++0x
=================================================
overflow.cpp: In function ‘int main()’:
overflow.cpp:5:36: error: overflow in constant expression [-fpermissive]

This is simply attempting to subtract 1 from -1, using "char"s.
I cannot see any reason why this should trigger an overflow error.
I have attempted to cast every intermediate state to char to avoid any
unintended casts.
The code is as simple as I could get it while still triggering the seemingly
erroneous overflow.

Compiles OK using int instead of char. (int can still erroneously overflow in
other circumstances, though. Would examples be useful?).
Compiles OK using 1 instead of -1.
Compiles OK using a literal -1 instead of a parameter.
Compiles OK if main()::m isn't constexpr.

Attached source code, but it's probably easier to just copy/paste the above.


SPECS:
gcc: version 4.6.0 2010-12-30 (experimental) (svn = 168358)
 - manually patched by
(http://gcc.gnu.org/ml/gcc-patches/2007-04/msg00620.html)
 - patch shouldn't have any effect here.
ubuntu: 10.10 (64 bit)
intel: core2 duo

Are any other specs relevant here, such as GMP. (Note: my GMP is the standard
one).


I searched other constexpr bugs here - AFAIK none appear to be about overflow.


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
@ 2011-01-28 11:20 ` rguenth at gcc dot gnu.org
  2011-03-16 23:35 ` jason at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-01-28 11:20 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.01.28 10:59:58
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-01-28 10:59:58 UTC ---
Confirmed (with -fsigned-char, -funsigned-char works ok).


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
  2011-01-28 11:20 ` [Bug c++/47504] " rguenth at gcc dot gnu.org
@ 2011-03-16 23:35 ` jason at gcc dot gnu.org
  2011-03-16 23:59 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-16 23:35 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org,
                   |                            |kazu at gcc dot gnu.org

--- Comment #2 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-16 23:34:25 UTC ---
This is happening because convert_to_integer is doing what seems to me like an
invalid transformation: it forces the arithmetic to be done in unsigned char,
so we end up with char((unsigned char)-2), or char(254), which has
implementation-defined behavior according to the C and C++ standards.

This seems to have been introduced with the fix for PR 25125.


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
  2011-01-28 11:20 ` [Bug c++/47504] " rguenth at gcc dot gnu.org
  2011-03-16 23:35 ` jason at gcc dot gnu.org
@ 2011-03-16 23:59 ` pinskia at gcc dot gnu.org
  2011-03-17  5:26 ` jason at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-03-16 23:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-03-16 23:43:38 UTC ---
>which has implementation-defined behavior according to the C and C++ standards.

But that does not mean it has an overflow though, right?


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
                   ` (2 preceding siblings ...)
  2011-03-16 23:59 ` pinskia at gcc dot gnu.org
@ 2011-03-17  5:26 ` jason at gcc dot gnu.org
  2011-03-17 22:05 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-17  5:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-17 03:14:22 UTC ---
True, the language standards seem to distinguish between this and the overflow
you get from saying INT_MAX+1.  But GCC internals do not make this distinction;
in either case, we end up setting TREE_OVERFLOW in force_fit_type_double. 
Given that, I would prefer to avoid this transformation.


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
                   ` (3 preceding siblings ...)
  2011-03-17  5:26 ` jason at gcc dot gnu.org
@ 2011-03-17 22:05 ` jason at gcc dot gnu.org
  2011-03-18  4:02 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-17 22:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-17 22:00:51 UTC ---
Author: jason
Date: Thu Mar 17 22:00:47 2011
New Revision: 171116

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171116
Log:
    PR c++/47504
    * semantics.c (cxx_eval_constant_expression) [NOP_EXPR]: Don't let
    the conversion set TREE_OVERFLOW.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-overflow2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
                   ` (4 preceding siblings ...)
  2011-03-17 22:05 ` jason at gcc dot gnu.org
@ 2011-03-18  4:02 ` jason at gcc dot gnu.org
  2011-03-29 14:26 ` jason at gcc dot gnu.org
  2011-03-29 14:38 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-18  4:02 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |jason at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #6 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-18 02:49:36 UTC ---
Fixed for 4.7, planning to fix in 4.6.1.


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
                   ` (5 preceding siblings ...)
  2011-03-18  4:02 ` jason at gcc dot gnu.org
@ 2011-03-29 14:26 ` jason at gcc dot gnu.org
  2011-03-29 14:38 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-29 14:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-29 14:24:24 UTC ---
Author: jason
Date: Tue Mar 29 14:24:19 2011
New Revision: 171664

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171664
Log:
    PR c++/47504
    * semantics.c (cxx_eval_constant_expression) [NOP_EXPR]: Don't let
    the conversion set TREE_OVERFLOW.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-overflow2.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/semantics.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C


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

* [Bug c++/47504] some constexpr calculations erroneously overflow when using negative numbers
  2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
                   ` (6 preceding siblings ...)
  2011-03-29 14:26 ` jason at gcc dot gnu.org
@ 2011-03-29 14:38 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-03-29 14:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.6.1

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> 2011-03-29 14:37:13 UTC ---
Fixed for 4.6.1.


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

end of thread, other threads:[~2011-03-29 14:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-28  8:38 [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers yacwroy at gmail dot com
2011-01-28 11:20 ` [Bug c++/47504] " rguenth at gcc dot gnu.org
2011-03-16 23:35 ` jason at gcc dot gnu.org
2011-03-16 23:59 ` pinskia at gcc dot gnu.org
2011-03-17  5:26 ` jason at gcc dot gnu.org
2011-03-17 22:05 ` jason at gcc dot gnu.org
2011-03-18  4:02 ` jason at gcc dot gnu.org
2011-03-29 14:26 ` jason at gcc dot gnu.org
2011-03-29 14:38 ` jason 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).