[libcxx-commits] [libcxx] [libc++] Fix possible out of range access in bitset (PR #121348)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Fri May 16 08:05:31 PDT 2025
================
@@ -283,21 +287,30 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
template <size_t _N_words, size_t _Size>
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
# ifndef _LIBCPP_CXX03_LANG
-# if __SIZEOF_SIZE_T__ == 8
- : __first_{__v}
-# elif __SIZEOF_SIZE_T__ == 4
+# if (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 1
+ : __first_{static_cast<__storage_type>(__v)}
+# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 2
+ : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
+# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 4
+# if _N_words == 2
----------------
winner245 wrote:
Thanks for correcting me! To address this, I have used SFINAE to dispatch based on the value of `_N_words` in C++11, as you suggested. However, for `>= C++14`, I opted for pack expansion with `index_sequence` to simplify the implementation (since `index_sequence` is not available in C++11).
https://github.com/llvm/llvm-project/pull/121348
More information about the libcxx-commits
mailing list