[libcxx-commits] [libcxx] [libc++] Fix incorrect overflow checking in std::lcm (PR #96310)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jun 22 03:57:35 PDT 2024
================
@@ -117,8 +117,11 @@ _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> lcm(_Tp __m, _Up
using _Rp = common_type_t<_Tp, _Up>;
_Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / std::gcd(__m, __n);
_Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
- _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
- return __val1 * __val2;
+ _Rp __res;
+ bool __overflow = __builtin_mul_overflow(__val1, __val2, &__res);
+ (void)__overflow;
+ _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__overflow, "Overflow in lcm");
----------------
philnik777 wrote:
Casting to void is used more often because a lot of code has to work in C++03 and not all compilers we support have `[[]]` attributes in C++03 yet.
https://github.com/llvm/llvm-project/pull/96310
More information about the libcxx-commits
mailing list