[libcxx-commits] [libcxx] [libc++] Fix UB in bitwise logic of {std, ranges}::{fill, fill_n} algorithms (PR #122410)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 14 09:26:32 PST 2025


================
@@ -56,7 +56,7 @@ __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
   // do last partial word
   if (__n > 0) {
     __first.__seg_ += __nw;
-    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
+    __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
     if (_FillVal)
       *__first.__seg_ |= __m;
     else
----------------
ldionne wrote:

Would it make sense to instead introduce an API to set the given bits of the `__bit_reference` to a given value? Dealing with masks in this algorithm seems like pretty low-level, instead perhaps we could move that logic over to `__bit_reference` itself.

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


More information about the libcxx-commits mailing list