public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-11389] libstdc++: Ensure constexpr std::lcm detects out-of-range result [PR105844]
@ 2023-05-03 20:59 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-05-03 20:59 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:b357af31b8d1e93f0f70133e25d3ad4045f7a32b

commit r10-11389-gb357af31b8d1e93f0f70133e25d3ad4045f7a32b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed May 3 21:40:01 2023 +0100

    libstdc++: Ensure constexpr std::lcm detects out-of-range result [PR105844]
    
    On the gcc-10 branch, __glibcxx_assert does not unconditionally check
    the condition during constant evaluation. This means we need an explicit
    additional check for std::lcm results that cannot be represented in an
    unsigned result type.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/105844
            * include/std/numeric (lcm): Ensure out-of-range result is
            detected in constant evaluation.
            * testsuite/26_numerics/lcm/105844.cc: Adjust dg-error string.

Diff:
---
 libstdc++-v3/include/std/numeric                 | 5 +++++
 libstdc++-v3/testsuite/26_numerics/lcm/105844.cc | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric
index b81b5e4a30f..f4636679223 100644
--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -161,6 +161,11 @@ namespace __detail
 #endif
 
       bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
+#if defined _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
+      if (__builtin_is_constant_evaluated())
+	if (__overflow)
+	  _GLIBCXX_THROW_OR_ABORT("std::lcm result is out of range of type");
+#endif
       __glibcxx_assert(!__overflow);
       return __r;
     }
diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
index 0b5ef5ae5e9..c7338f3cbf3 100644
--- a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
+++ b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
@@ -19,6 +19,6 @@ constexpr int d = std::lcm(49999, 50000); // { dg-error "overflow" }
 // Similarly for unsigned, but the diagnostic is a failed assertion instead.
 constexpr int e = std::lcm(500000u, 499999); // { dg-error "in 'constexpr'" }
 constexpr int f = std::lcm(499999u, 500000); // { dg-error "in 'constexpr'" }
-// { dg-error "unreachable" "" { target *-*-* } 0 }
+// { dg-error "throw.* not a constant expression" "" { target *-*-* } 0 }
 
 // { dg-prune-output "in 'constexpr' expansion" }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-03 20:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03 20:59 [gcc r10-11389] libstdc++: Ensure constexpr std::lcm detects out-of-range result [PR105844] Jonathan Wakely

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).