[libcxx-commits] [libcxx] [libc++] Harden the algorithms against volatile pointers (PR #222813)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 10 18:08:13 PDT 2026


================
@@ -59,11 +61,15 @@ __assume_valid_range([[__maybe_unused__]] _Iter&& __first, [[__maybe_unused__]]
                 is_same<__remove_cvref_t<_Iter>, __remove_cvref_t<_Sent>>::value) {
     _LIBCPP_ASSERT_INTERNAL(std::__is_valid_range(std::__to_address(__first), std::__to_address(__last)),
                             "Valid range assumption does not hold");
-    if (!__libcpp_is_constant_evaluated()) {
-      using __value_type = typename iterator_traits<__remove_cvref_t<_Iter>>::value_type;
-      __builtin_assume_dereferenceable(std::__to_address(__first), (__last - __first) * sizeof(__value_type));
-      (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__first));
-      (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__last));
+    // We can't make these assumptions for volatile pointers since the builtins take a plain pointer.
----------------
frederick-vs-ja wrote:

Shouldn't we use `const_cast` to drop volatile-ness here? This should be fine as long as we don't access the elements via pointers to volatile type.

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


More information about the libcxx-commits mailing list