[libcxx-commits] [libcxx] [libc++] Fix possible out of range access in bitset::to_ullong implementation (PR #121348)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 30 21:20:41 PST 2024
================
@@ -381,8 +382,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
unsigned long long __r = __first_[0];
_LIBCPP_DIAGNOSTIC_PUSH
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
- for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
- __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
+ size_t __n_words = std::min<size_t>(_N_words, sizeof(unsigned long long) / sizeof(__storage_type));
----------------
frederick-vs-ja wrote:
> I am not sure if your intention of replacing `std::min` by the conditional expression is to avoid including the `<__algorithm/min.h>` header?
No. `std::min` is constexpr only since C++14, so it's necessary to avoid call to it to make `__n_words` a constant in older modes.
https://github.com/llvm/llvm-project/pull/121348
More information about the libcxx-commits
mailing list