[libcxx-commits] [libcxx] [libc++] Assert that valarray operands and assignment sources have matching sizes (PR #230606)

Ilyas Mallah via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 9 10:58:10 PDT 2026


https://github.com/ilyas-mallah created https://github.com/llvm/llvm-project/pull/230606

Fixes #153518.

The standard requires matching sizes for the operands of a binary valarray operator, for compound assignment, and for assigning a proxy array to a valarray. libc++ doesn't check any of it. The assignments that copy through raw pointers overflow: `v = big[std::slice(0, 64, 1)]` with a 2-element `v` writes 64 elements into a 2-element buffer, and ASan reports it as a heap-buffer-overflow on `main`.

This adds `_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS` size checks to the `_BinaryOp` constructors, the valarray compound assignments and assignments from proxy arrays, the proxy arrays' copy assignments, and `slice_array::operator=(const valarray&)`. The other proxy assignments are left out, since `operator[]` and `__get()` already trap on a shorter argument there.

The standard doesn't state a size precondition for the proxy assignments. I went with an exact match, as libstdc++ does for `mask_array`. Only one direction is unsafe, so I can make those checks one-sided if that's preferred.

AI usage: Claude Code helped with the test runs and test scaffolding. I traced the issue, made the fix, and validated every change.


>From 0c71e34de348ae15019efc7062a4ff4600484313 Mon Sep 17 00:00:00 2001
From: Ilyas Mallah <github at ilyasm.dev>
Date: Fri, 9 Oct 2026 03:45:07 +0200
Subject: [PATCH 1/3] [libc++] Assert matching operand sizes in valarray binary
 operations

---
 libcxx/include/valarray                       | 16 +++--
 .../numarray/assert.binary_operators.pass.cpp | 64 +++++++++++++++++++
 2 files changed, 76 insertions(+), 4 deletions(-)
 create mode 100644 libcxx/test/libcxx/numerics/numarray/assert.binary_operators.pass.cpp

diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index df3ce3252e3d2c..2dc8cb1ddb4084 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -460,7 +460,9 @@ struct _BinaryOp {
   _A1 __a1_;
 
   _LIBCPP_HIDE_FROM_ABI _BinaryOp(const _Op& __op, const _A0& __a0, const _A1& __a1)
-      : __op_(__op), __a0_(__a0), __a1_(__a1) {}
+      : __op_(__op), __a0_(__a0), __a1_(__a1) {
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__a0.size() == __a1.size(), "valarray binary operation size mismatch");
+  }
 
   _LIBCPP_HIDE_FROM_ABI __result_type operator[](size_t __i) const { return __op_(__a0_[__i], __a1_[__i]); }
 
@@ -1006,7 +1008,9 @@ struct _BinaryOp<_Op, valarray<_Tp>, _A1> {
   _A1 __a1_;
 
   _LIBCPP_HIDE_FROM_ABI _BinaryOp(const _Op& __op, const valarray<_Tp>& __a0, const _A1& __a1)
-      : __op_(__op), __a0_(__a0), __a1_(__a1) {}
+      : __op_(__op), __a0_(__a0), __a1_(__a1) {
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__a0.size() == __a1.size(), "valarray binary operation size mismatch");
+  }
 
   _LIBCPP_HIDE_FROM_ABI __result_type operator[](size_t __i) const { return __op_(__a0_[__i], __a1_[__i]); }
 
@@ -1023,7 +1027,9 @@ struct _BinaryOp<_Op, _A0, valarray<_Tp> > {
   const valarray<_Tp>& __a1_;
 
   _LIBCPP_HIDE_FROM_ABI _BinaryOp(const _Op& __op, const _A0& __a0, const valarray<_Tp>& __a1)
-      : __op_(__op), __a0_(__a0), __a1_(__a1) {}
+      : __op_(__op), __a0_(__a0), __a1_(__a1) {
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__a0.size() == __a1.size(), "valarray binary operation size mismatch");
+  }
 
   _LIBCPP_HIDE_FROM_ABI __result_type operator[](size_t __i) const { return __op_(__a0_[__i], __a1_[__i]); }
 
@@ -1040,7 +1046,9 @@ struct _BinaryOp<_Op, valarray<_Tp>, valarray<_Tp> > {
   const valarray<_Tp>& __a1_;
 
   _LIBCPP_HIDE_FROM_ABI _BinaryOp(const _Op& __op, const valarray<_Tp>& __a0, const valarray<_Tp>& __a1)
-      : __op_(__op), __a0_(__a0), __a1_(__a1) {}
+      : __op_(__op), __a0_(__a0), __a1_(__a1) {
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__a0.size() == __a1.size(), "valarray binary operation size mismatch");
+  }
 
   _LIBCPP_HIDE_FROM_ABI __result_type operator[](size_t __i) const { return __op_(__a0_[__i], __a1_[__i]); }
 
diff --git a/libcxx/test/libcxx/numerics/numarray/assert.binary_operators.pass.cpp b/libcxx/test/libcxx/numerics/numarray/assert.binary_operators.pass.cpp
new file mode 100644
index 00000000000000..86a8a2a969daae
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/assert.binary_operators.pass.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// Test that the operands of a binary operation on valarrays must have the same size.
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> a(1, 3);
+  std::valarray<int> b(2, 3);
+  std::valarray<int> e(3, 2);
+
+  (void)(a + b); // Check that there's no assertion for operands of the same size.
+  (void)(a + b + a);
+
+  // valarray and valarray
+  TEST_LIBCPP_ASSERT_FAILURE(a + e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(e + a, "valarray binary operation size mismatch");
+
+  // expression and valarray
+  TEST_LIBCPP_ASSERT_FAILURE(a + b + e, "valarray binary operation size mismatch");
+
+  // valarray and expression
+  TEST_LIBCPP_ASSERT_FAILURE(e + (a + b), "valarray binary operation size mismatch");
+
+  // expression and expression
+  TEST_LIBCPP_ASSERT_FAILURE((a + b) + (e + e), "valarray binary operation size mismatch");
+
+  TEST_LIBCPP_ASSERT_FAILURE(a * e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a / e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a % e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a - e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a ^ e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a & e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a | e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a << e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a >> e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a && e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a || e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a == e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a != e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a < e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a > e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a <= e, "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(a >= e, "valarray binary operation size mismatch");
+
+  std::valarray<double> x(1.0, 3);
+  std::valarray<double> y(2.0, 2);
+  TEST_LIBCPP_ASSERT_FAILURE(std::atan2(x, y), "valarray binary operation size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(std::pow(x, y), "valarray binary operation size mismatch");
+
+  return 0;
+}

>From 23acdbd9fbb619fc9efd583d77e0e6f303d1e324 Mon Sep 17 00:00:00 2001
From: Ilyas Mallah <github at ilyasm.dev>
Date: Fri, 9 Oct 2026 03:45:18 +0200
Subject: [PATCH 2/3] [libc++] Assert matching sizes in valarray compound and
 proxy array assignment

---
 libcxx/include/valarray                       | 37 +++++++++++++
 .../numerics/numarray/assert.assign.pass.cpp  | 41 ++++++++++++++
 .../assert.compound_assignment.pass.cpp       | 53 +++++++++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 libcxx/test/libcxx/numerics/numarray/assert.assign.pass.cpp
 create mode 100644 libcxx/test/libcxx/numerics/numarray/assert.compound_assignment.pass.cpp

diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index 2dc8cb1ddb4084..c7b3fa7350d3f9 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -768,6 +768,9 @@ struct __is_val_expr<indirect_array<_Tp> > : true_type {};
 //   __get(size_t)
 // else it uses the __val_expr's member function
 //   operator[](size_t)
+//
+// The non-member function __get_size(__val_expr) likewise uses either the
+// member function __get_size() or size().
 template <class _ValExpr>
 struct __val_expr_use_member_functions;
 
@@ -981,6 +984,18 @@ _LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t
   return __v[__i];
 }
 
+template <class _Expr,
+          __enable_if_t<__is_val_expr<_Expr>::value && __val_expr_use_member_functions<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI size_t __get_size(const _Expr& __v) {
+  return __v.__get_size();
+}
+
+template <class _Expr,
+          __enable_if_t<__is_val_expr<_Expr>::value && !__val_expr_use_member_functions<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI size_t __get_size(const _Expr& __v) {
+  return __v.size();
+}
+
 extern template _LIBCPP_EXPORTED_FROM_ABI void valarray<size_t>::resize(size_t, size_t);
 
 template <class _Op, class _Tp>
@@ -1115,6 +1130,8 @@ public:
     return __vp_[__i * __stride_];
   }
 
+  _LIBCPP_HIDE_FROM_ABI size_t __get_size() const { return __size_; }
+
 private:
   _LIBCPP_HIDE_FROM_ABI slice_array(const slice& __sl, const valarray<value_type>& __v)
       : __vp_(const_cast<value_type*>(__v.__begin_ + __sl.start())), __size_(__sl.size()), __stride_(__sl.stride()) {}
@@ -1342,6 +1359,8 @@ public:
     return __vp_[__1d_[__i]];
   }
 
+  _LIBCPP_HIDE_FROM_ABI size_t __get_size() const { return __1d_.size(); }
+
 private:
   gslice_array(const gslice& __gs, const valarray<value_type>& __v)
       : __vp_(const_cast<value_type*>(__v.__begin_)), __1d_(__gs.__1d_) {}
@@ -1527,6 +1546,8 @@ public:
     return __vp_[__1d_[__i]];
   }
 
+  _LIBCPP_HIDE_FROM_ABI size_t __get_size() const { return __1d_.size(); }
+
 private:
   _LIBCPP_HIDE_FROM_ABI mask_array(const valarray<bool>& __vb, const valarray<value_type>& __v)
       : __vp_(const_cast<value_type*>(__v.__begin_)),
@@ -1732,6 +1753,8 @@ public:
     return __vp_[__1d_[__i]];
   }
 
+  _LIBCPP_HIDE_FROM_ABI size_t __get_size() const { return __1d_.size(); }
+
 private:
   _LIBCPP_HIDE_FROM_ABI indirect_array(const valarray<size_t>& __ia, const valarray<value_type>& __v)
       : __vp_(const_cast<value_type*>(__v.__begin_)), __1d_(__ia) {}
@@ -2176,6 +2199,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(const value_type& __x) {
 
 template <class _Tp>
 inline valarray<_Tp>& valarray<_Tp>::operator=(const slice_array<value_type>& __sa) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__sa.__size_ == size(), "valarray::operator=(slice_array) size mismatch");
   value_type* __t       = __begin_;
   const value_type* __s = __sa.__vp_;
   for (size_t __n = __sa.__size_; __n; --__n, __s += __sa.__stride_, ++__t)
@@ -2185,6 +2209,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(const slice_array<value_type>& __
 
 template <class _Tp>
 inline valarray<_Tp>& valarray<_Tp>::operator=(const gslice_array<value_type>& __ga) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__ga.__1d_.size() == size(), "valarray::operator=(gslice_array) size mismatch");
   typedef const size_t* _Ip;
   value_type* __t       = __begin_;
   const value_type* __s = __ga.__vp_;
@@ -2195,6 +2220,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(const gslice_array<value_type>& _
 
 template <class _Tp>
 inline valarray<_Tp>& valarray<_Tp>::operator=(const mask_array<value_type>& __ma) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__ma.__1d_.size() == size(), "valarray::operator=(mask_array) size mismatch");
   typedef const size_t* _Ip;
   value_type* __t       = __begin_;
   const value_type* __s = __ma.__vp_;
@@ -2205,6 +2231,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator=(const mask_array<value_type>& __m
 
 template <class _Tp>
 inline valarray<_Tp>& valarray<_Tp>::operator=(const indirect_array<value_type>& __ia) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__ia.__1d_.size() == size(), "valarray::operator=(indirect_array) size mismatch");
   typedef const size_t* _Ip;
   value_type* __t       = __begin_;
   const value_type* __s = __ia.__vp_;
@@ -2405,6 +2432,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator>>=(const value_type& __x) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator*=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator*= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t *= std::__get(__v, __i);
@@ -2414,6 +2442,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator*=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator/=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator/= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t /= std::__get(__v, __i);
@@ -2423,6 +2452,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator/=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator%=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator%= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t %= std::__get(__v, __i);
@@ -2432,6 +2462,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator%=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator+=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator+= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t += std::__get(__v, __i);
@@ -2441,6 +2472,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator+=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator-=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator-= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t -= std::__get(__v, __i);
@@ -2450,6 +2482,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator-=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator^=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator^= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t ^= std::__get(__v, __i);
@@ -2459,6 +2492,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator^=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator|=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator|= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t |= std::__get(__v, __i);
@@ -2468,6 +2502,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator|=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator&=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator&= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t &= std::__get(__v, __i);
@@ -2477,6 +2512,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator&=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator<<=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator<<= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t <<= std::__get(__v, __i);
@@ -2486,6 +2522,7 @@ inline valarray<_Tp>& valarray<_Tp>::operator<<=(const _Expr& __v) {
 template <class _Tp>
 template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
 inline valarray<_Tp>& valarray<_Tp>::operator>>=(const _Expr& __v) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(size() == std::__get_size(__v), "valarray::operator>>= size mismatch");
   size_t __i = 0;
   for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
     *__t >>= std::__get(__v, __i);
diff --git a/libcxx/test/libcxx/numerics/numarray/assert.assign.pass.cpp b/libcxx/test/libcxx/numerics/numarray/assert.assign.pass.cpp
new file mode 100644
index 00000000000000..b01e86bfb1aa94
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/assert.assign.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// Test that a proxy array assigned to a valarray must have the same size as the valarray.
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+#include <cstddef>
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> source(1, 8);
+  std::valarray<int> v(3);
+
+  // Proxy arrays with 2 elements.
+  std::valarray<std::size_t> gslice_size(2, 1);
+  std::valarray<std::size_t> gslice_stride(1, 1);
+  std::valarray<bool> mask(false, 8);
+  mask[std::slice(0, 2, 1)] = true;
+  std::size_t index_array[] = {0, 1};
+  std::valarray<std::size_t> indices(index_array, 2);
+
+  v = source[std::slice(0, 3, 1)]; // Check that there's no assertion for arrays of the same size.
+
+  TEST_LIBCPP_ASSERT_FAILURE(v = source[std::slice(0, 2, 1)], "valarray::operator=(slice_array) size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(
+      v = source[std::gslice(0, gslice_size, gslice_stride)], "valarray::operator=(gslice_array) size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v = source[mask], "valarray::operator=(mask_array) size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v = source[indices], "valarray::operator=(indirect_array) size mismatch");
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/assert.compound_assignment.pass.cpp b/libcxx/test/libcxx/numerics/numarray/assert.compound_assignment.pass.cpp
new file mode 100644
index 00000000000000..cfec5ca6abb10d
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/assert.compound_assignment.pass.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// Test that the argument of a valarray compound assignment must have the same size as the valarray.
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+#include <cstddef>
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> v(1, 2);
+  std::valarray<int> w(2, 3);
+
+  v += std::valarray<int>(2, 2); // Check that there's no assertion for arrays of the same size.
+
+  TEST_LIBCPP_ASSERT_FAILURE(v *= w, "valarray::operator*= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v /= w, "valarray::operator/= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v %= w, "valarray::operator%= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v += w, "valarray::operator+= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v -= w, "valarray::operator-= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v ^= w, "valarray::operator^= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v &= w, "valarray::operator&= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v |= w, "valarray::operator|= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v <<= w, "valarray::operator<<= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v >>= w, "valarray::operator>>= size mismatch");
+
+  // The argument is an expression or one of the proxy arrays, each with 3 elements.
+  std::valarray<int> source(1, 8);
+  std::valarray<std::size_t> gslice_size(3, 1);
+  std::valarray<std::size_t> gslice_stride(1, 1);
+  std::valarray<bool> mask(false, 8);
+  mask[std::slice(0, 3, 1)] = true;
+  std::size_t index_array[] = {0, 1, 2};
+  std::valarray<std::size_t> indices(index_array, 3);
+  TEST_LIBCPP_ASSERT_FAILURE(v += w + w, "valarray::operator+= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v += source[std::slice(0, 3, 1)], "valarray::operator+= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(
+      v += source[std::gslice(0, gslice_size, gslice_stride)], "valarray::operator+= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v += source[mask], "valarray::operator+= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(v += source[indices], "valarray::operator+= size mismatch");
+
+  return 0;
+}

>From de2e315ea0018aabe4bcc34f0a68432794eb4745 Mon Sep 17 00:00:00 2001
From: Ilyas Mallah <github at ilyasm.dev>
Date: Fri, 9 Oct 2026 03:45:23 +0200
Subject: [PATCH 3/3] [libc++] Assert matching sizes in slice_array,
 gslice_array and indirect_array assignment

---
 libcxx/include/valarray                       |  4 +++
 .../class.gslice.array/assert.assign.pass.cpp | 34 ++++++++++++++++++
 .../assert.assign.pass.cpp                    | 36 +++++++++++++++++++
 .../class.slice.array/assert.assign.pass.cpp  | 34 ++++++++++++++++++
 4 files changed, 108 insertions(+)
 create mode 100644 libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.assign.pass.cpp
 create mode 100644 libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.assign.pass.cpp
 create mode 100644 libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.assign.pass.cpp

diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index c7b3fa7350d3f9..d849e21d85a6de 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -1142,6 +1142,7 @@ private:
 
 template <class _Tp>
 inline const slice_array<_Tp>& slice_array<_Tp>::operator=(const slice_array& __sa) const {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__sa.__size_ == __size_, "slice_array::operator= size mismatch");
   value_type* __t       = __vp_;
   const value_type* __s = __sa.__vp_;
   for (size_t __n = __size_; __n; --__n, __t += __stride_, __s += __sa.__stride_)
@@ -1159,6 +1160,7 @@ inline void slice_array<_Tp>::operator=(const _Expr& __v) const {
 
 template <class _Tp>
 inline void slice_array<_Tp>::operator=(const valarray<value_type>& __va) const {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__va.size() == __size_, "slice_array::operator= size mismatch");
   value_type* __t = __vp_;
   for (size_t __i = 0; __i < __va.size(); ++__i, __t += __stride_)
     *__t = __va[__i];
@@ -1475,6 +1477,7 @@ inline void gslice_array<_Tp>::operator>>=(const _Expr& __v) const {
 
 template <class _Tp>
 inline const gslice_array<_Tp>& gslice_array<_Tp>::operator=(const gslice_array& __ga) const {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__ga.__1d_.size() == __1d_.size(), "gslice_array::operator= size mismatch");
   typedef const size_t* _Ip;
   const value_type* __s = __ga.__vp_;
   for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_, __j = __ga.__1d_.__begin_; __i != __e; ++__i, ++__j)
@@ -1860,6 +1863,7 @@ inline void indirect_array<_Tp>::operator>>=(const _Expr& __v) const {
 
 template <class _Tp>
 inline const indirect_array<_Tp>& indirect_array<_Tp>::operator=(const indirect_array& __ia) const {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__ia.__1d_.size() == __1d_.size(), "indirect_array::operator= size mismatch");
   typedef const size_t* _Ip;
   const value_type* __s = __ia.__vp_;
   for (_Ip __i = __1d_.__begin_, __e = __1d_.__end_, __j = __ia.__1d_.__begin_; __i != __e; ++__i, ++__j)
diff --git a/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.assign.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.assign.pass.cpp
new file mode 100644
index 00000000000000..f6b7b2721328ab
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.assign.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+// <valarray>
+
+// template<class T> class gslice_array;
+
+// const gslice_array& operator=(const gslice_array& ga) const; // where ga has a different size
+
+#include <cstddef>
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> array(1, 16);
+  std::valarray<std::size_t> stride(2, 1);
+  std::gslice_array<int> target = array[std::gslice(0, std::valarray<std::size_t>(3, 1), stride)];
+
+  // Check that there's no assertion for arrays of the same size.
+  target = array[std::gslice(1, std::valarray<std::size_t>(3, 1), stride)];
+
+  TEST_LIBCPP_ASSERT_FAILURE(target = array[std::gslice(1, std::valarray<std::size_t>(4, 1), stride)],
+                             "gslice_array::operator= size mismatch");
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.assign.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.assign.pass.cpp
new file mode 100644
index 00000000000000..919d8d3c1b5aba
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.assign.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+// <valarray>
+
+// template<class T> class indirect_array;
+
+// const indirect_array& operator=(const indirect_array& ia) const; // where ia has a different size
+
+#include <cstddef>
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> array(1, 16);
+  std::size_t target_indices[]    = {0, 2, 4};
+  std::size_t same_size_indices[] = {1, 3, 5};
+  std::size_t longer_indices[]    = {1, 3, 5, 7};
+  std::indirect_array<int> target = array[std::valarray<std::size_t>(target_indices, 3)];
+
+  // Check that there's no assertion for arrays of the same size.
+  target = array[std::valarray<std::size_t>(same_size_indices, 3)];
+
+  TEST_LIBCPP_ASSERT_FAILURE(
+      target = array[std::valarray<std::size_t>(longer_indices, 4)], "indirect_array::operator= size mismatch");
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.assign.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.assign.pass.cpp
new file mode 100644
index 00000000000000..187d4d983a5a17
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.assign.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+
+// <valarray>
+
+// template<class T> class slice_array;
+
+// void operator=(const valarray<value_type>& v) const; // where v.size() differs
+// const slice_array& operator=(const slice_array& sa) const; // where sa has a different size
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::valarray<int> array(1, 16);
+  std::slice_array<int> target = array[std::slice(0, 3, 2)];
+
+  // Check that there's no assertion for arrays of the same size.
+  target = std::valarray<int>(2, 3);
+  target = array[std::slice(1, 3, 2)];
+
+  TEST_LIBCPP_ASSERT_FAILURE(target = std::valarray<int>(2, 4), "slice_array::operator= size mismatch");
+  TEST_LIBCPP_ASSERT_FAILURE(target = array[std::slice(1, 4, 2)], "slice_array::operator= size mismatch");
+
+  return 0;
+}



More information about the libcxx-commits mailing list