public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression
@ 2012-02-04  1:16 jyasskin at gcc dot gnu.org
  2012-02-04  2:13 ` [Bug libstdc++/52119] " richard-gccbugzilla at metafoo dot co.uk
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: jyasskin at gcc dot gnu.org @ 2012-02-04  1:16 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52119
           Summary: numeric_limits::min() is not a constant expression
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jyasskin@gcc.gnu.org


numeric_limits<T>::min() is defined as (__glibcxx_signed (T) ? (T)1 <<
__glibcxx_digits (T) : (T)0). Unfortunately, shifting into the sign bit is
undefined behavior (C++11[expr.shift]p2), and undefined behavior makes an
expression non-constant.


clang as of (their) r149727 diagnoses this as:

$ echo '#include <limits>' | clang++ -nostdinc++
-Igcc-4.7-svn/include/c++/4.7.0/{,x86_64-unknown-linux-gnu} -std=c++11
-Wsystem-headers -fsyntax-only -x c++ -

gcc-4.7-svn/include/c++/4.7.0/limits:654:7: error: constexpr function never
      produces a constant expression
      min() _GLIBCXX_USE_NOEXCEPT { return __glibcxx_min (wchar_t); }
      ^
gcc-4.7-svn/include/c++/4.7.0/limits:654:44: note: value 2147483648 is
      outside the range of representable values of type 'int'
      min() _GLIBCXX_USE_NOEXCEPT { return __glibcxx_min (wchar_t); }
                                           ^
gcc-4.7-svn/include/c++/4.7.0/limits:131:32: note: expanded from macro
      '__glibcxx_min'
  (__glibcxx_signed (T) ? (T)1 << __glibcxx_digits (T) : (T)0)
                               ^
gcc-4.7-svn/include/c++/4.7.0/limits:784:31: warning: shift count >= width
      of type [-Wshift-count-overflow]
      min() noexcept { return __glibcxx_min (char32_t); }
                              ^~~~~~~~~~~~~~~~~~~~~~~~


A better definition might be -__glibcxx_max(T)-1.

This bug is also in 4.6.2. 4.5.0 didn't declare min() as constexpr.


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

* [Bug libstdc++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
@ 2012-02-04  2:13 ` richard-gccbugzilla at metafoo dot co.uk
  2012-02-04  9:59 ` paolo.carlini at oracle dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2012-02-04  2:13 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Smith <richard-gccbugzilla at metafoo dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |richard-gccbugzilla at
                   |                            |metafoo dot co.uk

--- Comment #1 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> 2012-02-04 02:12:41 UTC ---
In more detail: Under the C++11 FDIS, this is not a (core) constant expression
due to the "— a result that is not mathematically defined or not in the range of
representable values for its type;" bullet in [expr.const]p2. Under DR1313,
that rule is generalized to all undefined behavior. And a constexpr function
which can't produce a constant expression is ill-formed by [dcl.constexpr]p5.


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

* [Bug libstdc++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
  2012-02-04  2:13 ` [Bug libstdc++/52119] " richard-gccbugzilla at metafoo dot co.uk
@ 2012-02-04  9:59 ` paolo.carlini at oracle dot com
  2012-02-04 10:04 ` paolo.carlini at oracle dot com
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-02-04  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-02-04
     Ever Confirmed|0                           |1

--- Comment #2 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-02-04 09:58:38 UTC ---
The problematic macro is extremely old. Using

#define __glibcxx_min(T) \
  (__glibcxx_signed (T) ? -__glibcxx_max(T) - 1 : (T)0)

seems indeed ok to me. Did you actually test it with clang?


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

* [Bug libstdc++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
  2012-02-04  2:13 ` [Bug libstdc++/52119] " richard-gccbugzilla at metafoo dot co.uk
  2012-02-04  9:59 ` paolo.carlini at oracle dot com
@ 2012-02-04 10:04 ` paolo.carlini at oracle dot com
  2012-02-05  2:05 ` jyasskin at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-02-04 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

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

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-02-04 10:04:09 UTC ---
And of course the interesting issue here is C++ front-end, which doesn't
produce any diagnostics (thus nobody would ever notice the library issue with
GCC only). Let's add Jason in CC for confirmation.


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

* [Bug libstdc++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2012-02-04 10:04 ` paolo.carlini at oracle dot com
@ 2012-02-05  2:05 ` jyasskin at gcc dot gnu.org
  2012-02-05 12:59 ` paolo at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jyasskin at gcc dot gnu.org @ 2012-02-05  2:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jeffrey Yasskin <jyasskin at gcc dot gnu.org> 2012-02-05 02:04:00 UTC ---
I hadn't tested

#define __glibcxx_min(T) \
  (__glibcxx_signed (T) ? -__glibcxx_max(T) - 1 : (T)0)

but now I have, and it works with clang.


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

* [Bug libstdc++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2012-02-05  2:05 ` jyasskin at gcc dot gnu.org
@ 2012-02-05 12:59 ` paolo at gcc dot gnu.org
  2012-02-05 13:01 ` [Bug c++/52119] " paolo.carlini at oracle dot com
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo at gcc dot gnu.org @ 2012-02-05 12:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from paolo at gcc dot gnu.org <paolo at gcc dot gnu.org> 2012-02-05 12:58:56 UTC ---
Author: paolo
Date: Sun Feb  5 12:58:51 2012
New Revision: 183905

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183905
Log:
2012-02-05  Jeffrey Yasskin  <jyasskin@gcc.gnu.org>
        Paolo Carlini  <paolo.carlini@oracle.com>

    PR libstdc++/52119
    * include/std/limits (__glibcxx_min): Fix to avoid undefined behavior.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/std/limits


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

* [Bug c++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2012-02-05 12:59 ` paolo at gcc dot gnu.org
@ 2012-02-05 13:01 ` paolo.carlini at oracle dot com
  2012-02-13 20:22 ` jason at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-02-05 13:01 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
          Component|libstdc++                   |c++

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-02-05 13:01:17 UTC ---
This is now a C++ front-end issue.


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

* [Bug c++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2012-02-05 13:01 ` [Bug c++/52119] " paolo.carlini at oracle dot com
@ 2012-02-13 20:22 ` jason at gcc dot gnu.org
  2012-02-13 22:18 ` richard-gccbugzilla at metafoo dot co.uk
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu.org @ 2012-02-13 20:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> 2012-02-13 20:22:37 UTC ---
At the C++ meeting last week we changed that shift from undefined to
implementation-defined, so there is no front end bug.


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

* [Bug c++/52119] numeric_limits::min() is not a constant expression
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2012-02-13 20:22 ` jason at gcc dot gnu.org
@ 2012-02-13 22:18 ` richard-gccbugzilla at metafoo dot co.uk
  2012-02-14  0:39 ` [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2012-02-13 22:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> 2012-02-13 22:17:27 UTC ---
Signed left shift which overflows the corresponding unsigned type is still
undefined, so g++ should reject this:

constexpr int n = 2 << 31;


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

* [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2012-02-13 22:18 ` richard-gccbugzilla at metafoo dot co.uk
@ 2012-02-14  0:39 ` jason at gcc dot gnu.org
  2013-06-05 17:26 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu.org @ 2012-02-14  0:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |NEW
         Resolution|INVALID                     |
            Summary|numeric_limits::min() is    |[C++11] overflow in signed
                   |not a constant expression   |left shift isn't diagnosed

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> 2012-02-14 00:38:14 UTC ---
Good point.


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

* [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2012-02-14  0:39 ` [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed jason at gcc dot gnu.org
@ 2013-06-05 17:26 ` jason at gcc dot gnu.org
  2013-06-05 17:41 ` jason at gcc dot gnu.org
  2013-06-05 22:37 ` jyasskin at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu.org @ 2013-06-05 17:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to ajf from comment #10)
> MY apologies if I am missing something obvious, but what exactly makes
> shifting a 1 into the sign bit undefined behavior?

You're right, it isn't:

5.8/2: ... if E1 has a signed type and non-negative value, and E1 × 2^E2 is
representable in the corresponding unsigned type of the result type, then that
value, converted to the result type, is the resulting value ....
>From gcc-bugs-return-423859-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Wed Jun 05 17:30:55 2013
Return-Path: <gcc-bugs-return-423859-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 1203 invoked by alias); 5 Jun 2013 17:30:55 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 1160 invoked by uid 48); 5 Jun 2013 17:30:51 -0000
From: "jyasskin at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed
Date: Wed, 05 Jun 2013 17:30:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c++
X-Bugzilla-Version: 4.7.0
X-Bugzilla-Keywords: accepts-invalid
X-Bugzilla-Severity: normal
X-Bugzilla-Who: jyasskin at gcc dot gnu.org
X-Bugzilla-Status: NEW
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-52119-4-PB7c2TuWg2@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-52119-4@http.gcc.gnu.org/bugzilla/>
References: <bug-52119-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2013-06/txt/msg00238.txt.bz2
Content-length: 233

http://gcc.gnu.org/bugzilla/show_bug.cgi?idR119

--- Comment #12 from Jeffrey Yasskin <jyasskin at gcc dot gnu.org> ---
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3675.html#1457 changed
this between C++11 and C++14.


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

* [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2013-06-05 17:26 ` jason at gcc dot gnu.org
@ 2013-06-05 17:41 ` jason at gcc dot gnu.org
  2013-06-05 22:37 ` jyasskin at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu.org @ 2013-06-05 17:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Jeffrey Yasskin from comment #12)
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3675.html#1457
> changed this between C++11 and C++14.

True, but the defect report applies to C++11.


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

* [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed
  2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
                   ` (10 preceding siblings ...)
  2013-06-05 17:41 ` jason at gcc dot gnu.org
@ 2013-06-05 22:37 ` jyasskin at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jyasskin at gcc dot gnu.org @ 2013-06-05 22:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from Jeffrey Yasskin <jyasskin at gcc dot gnu.org> ---
Whoops, I missed your comment 7 where you already knew the history. Sorry.


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

end of thread, other threads:[~2013-06-05 22:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-04  1:16 [Bug libstdc++/52119] New: numeric_limits::min() is not a constant expression jyasskin at gcc dot gnu.org
2012-02-04  2:13 ` [Bug libstdc++/52119] " richard-gccbugzilla at metafoo dot co.uk
2012-02-04  9:59 ` paolo.carlini at oracle dot com
2012-02-04 10:04 ` paolo.carlini at oracle dot com
2012-02-05  2:05 ` jyasskin at gcc dot gnu.org
2012-02-05 12:59 ` paolo at gcc dot gnu.org
2012-02-05 13:01 ` [Bug c++/52119] " paolo.carlini at oracle dot com
2012-02-13 20:22 ` jason at gcc dot gnu.org
2012-02-13 22:18 ` richard-gccbugzilla at metafoo dot co.uk
2012-02-14  0:39 ` [Bug c++/52119] [C++11] overflow in signed left shift isn't diagnosed jason at gcc dot gnu.org
2013-06-05 17:26 ` jason at gcc dot gnu.org
2013-06-05 17:41 ` jason at gcc dot gnu.org
2013-06-05 22:37 ` jyasskin 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).