]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Ensure constexpr std::lcm detects out-of-range result [PR105844]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 3 May 2023 20:40:01 +0000 (21:40 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 3 May 2023 20:52:38 +0000 (21:52 +0100)
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.

libstdc++-v3/include/std/numeric
libstdc++-v3/testsuite/26_numerics/lcm/105844.cc

index b81b5e4a30f43134c4f60dcf20d41e042d02dc3f..f4636679223db0f8e1b700b000d1279605574908 100644 (file)
@@ -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;
     }
index 0b5ef5ae5e9096b13517b6816d5b803db1c46381..c7338f3cbf39549836aa5752bc975cd3d49b18c6 100644 (file)
@@ -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" }