public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/105844] [10/11/12/13 Regression] std::lcm(50000, 49999) is UB but accepted in a constexpr due to cast to unsigned
Date: Mon, 06 Jun 2022 15:19:45 +0000	[thread overview]
Message-ID: <bug-105844-4-5OHapVPxry@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105844-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think the fix we want is simply:

--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -68,6 +68,7 @@
 #if __cplusplus >= 201402L
 # include <type_traits>
 # include <bit>
+# include <ext/numeric_traits.h>
 #endif

 #if __cplusplus >= 201703L
@@ -102,7 +103,15 @@ namespace __detail
       static_assert(is_unsigned<_Up>::value, "result type must be unsigned");
       static_assert(sizeof(_Up) >= sizeof(_Tp),
          "result type must be at least as wide as the input type");
-      return __val < 0 ? -(_Up)__val : (_Up)__val;
+
+      if (__val >= 0)
+       return static_cast<_Up>(__val);
+
+      if _GLIBCXX17_CONSTEXPR (sizeof(_Up) == sizeof(_Tp))
+       {
+         __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Tp>::__min);
+       }
+      return -static_cast<_Up>(__val);
     }

   template<typename _Up> void __absu(bool) = delete;

  parent reply	other threads:[~2022-06-06 15:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-04  1:08 [Bug libstdc++/105844] New: " goswin-v-b at web dot de
2022-06-04  1:11 ` [Bug libstdc++/105844] " goswin-v-b at web dot de
2022-06-04  1:32 ` goswin-v-b at web dot de
2022-06-04  8:24 ` goswin-v-b at web dot de
2022-06-06 14:22 ` [Bug libstdc++/105844] [10/11/12/13 Regression] " redi at gcc dot gnu.org
2022-06-06 15:19 ` redi at gcc dot gnu.org [this message]
2022-06-07 10:07 ` redi at gcc dot gnu.org
2022-06-10 14:24 ` cvs-commit at gcc dot gnu.org
2022-06-28 10:49 ` jakub at gcc dot gnu.org
2022-08-03 13:46 ` [Bug libstdc++/105844] [10/11/12 " cvs-commit at gcc dot gnu.org
2022-08-03 13:49 ` [Bug libstdc++/105844] [10/11 " redi at gcc dot gnu.org
2023-05-03 15:02 ` cvs-commit at gcc dot gnu.org
2023-05-03 15:16 ` cvs-commit at gcc dot gnu.org
2023-05-03 16:34 ` cvs-commit at gcc dot gnu.org
2023-05-03 16:35 ` redi at gcc dot gnu.org
2023-05-03 20:59 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105844-4-5OHapVPxry@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).