[libcxx-commits] [libcxx] [libc++] Fix incorrect overflow checking in std::lcm (PR #96310)

Dimitry Andric via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jun 22 03:40:49 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");
----------------
DimitryAndric wrote:

What is the general preference in libc++ to suppress unused warnings? I haven't made any inventory so I'm not sure what is used more often, casting to void or using maybe_unused.


https://github.com/llvm/llvm-project/pull/96310


More information about the libcxx-commits mailing list