Skip to content

[libc++] Fix incorrect overflow checking in std::lcm #96310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions libcxx/include/__numeric/gcd_lcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ _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;
[[maybe_unused]] bool __overflow = __builtin_mul_overflow(__val1, __val2, &__res);
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__overflow, "Overflow in lcm");
return __res;
}

#endif // _LIBCPP_STD_VER
#endif // _LIBCPP_STD_VER >= 17

_LIBCPP_END_NAMESPACE_STD

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cassert>
#include <climits>
#include <cstdint>
#include <limits>
#include <type_traits>
#include "test_macros.h"

Expand Down Expand Up @@ -89,6 +90,13 @@ constexpr bool do_test(int = 0)
return accumulate;
}

template <class T>
constexpr bool test_limits() {
assert(std::lcm(std::numeric_limits<T>::max() - 1, std::numeric_limits<T>::max() - 1) == std::numeric_limits<T>::max() - 1);
assert(std::lcm(std::numeric_limits<T>::max(), std::numeric_limits<T>::max()) == std::numeric_limits<T>::max());
return true;
}

int main(int argc, char**)
{
int non_cce = argc; // a value that can't possibly be constexpr
Expand Down Expand Up @@ -141,5 +149,22 @@ int main(int argc, char**)
assert(res1 == 1324997410816LL);
}

return 0;
// https://github.com/llvm/llvm-project/issues/96196
{
assert(test_limits<unsigned int>());
assert(test_limits<std::uint32_t>());
assert(test_limits<std::uint64_t>());
assert(test_limits<int>());
assert(test_limits<std::int32_t>());
assert(test_limits<std::int64_t>());

static_assert(test_limits<unsigned int>(), "");
static_assert(test_limits<std::uint32_t>(), "");
static_assert(test_limits<std::uint64_t>(), "");
static_assert(test_limits<int>(), "");
static_assert(test_limits<std::int32_t>(), "");
static_assert(test_limits<std::int64_t>(), "");
}

return 0;
}
Loading