From: Jonathan Wakely Date: Mon, 6 Mar 2023 14:41:59 +0000 (+0000) Subject: libstdc++: Add assertions to std::mask_array operations [PR62196] X-Git-Tag: basepoints/gcc-14~551 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=abb958ada1e4d195f31740659cd8af8bebce7bfd;p=thirdparty%2Fgcc.git libstdc++: Add assertions to std::mask_array operations [PR62196] Add assertions to diagnose incorrect uses of valarray masks. The assignment operators of std::mask_array do not have any explicit preconditions in the standard, but the assignment operator valarray::operator=(const mask_array&) requires the lengths to match, so it seems consistent to also require that when the operands are reversed. In support of that interpretation, libstdc++ has undefined behaviour if the right-hand operand has more elements than are selected by the mask, and libc++ has undefined behaviour if it has fewer elements. Our std::mask_array stores the number of selected elements as _M_sz so it's easy to add an assertion that checks it. For the valarray::operator[] that takes a valarray mask, [valarray.sub] in the standard says: "In each case the selected element(s) shall exist." This makes it undefined to have a mask that refers to out-of-range elements. We can easily check this too. libstdc++-v3/ChangeLog: PR libstdc++/62196 * include/bits/mask_array.h (mask_array): Add assertions to assignment operators. * include/std/valarray (valarray::operator[](valarray)): Add assertions. * testsuite/26_numerics/valarray/mask-1_neg.cc: New test. * testsuite/26_numerics/valarray/mask-2_neg.cc: New test. * testsuite/26_numerics/valarray/mask-3_neg.cc: New test. * testsuite/26_numerics/valarray/mask-4_neg.cc: New test. * testsuite/26_numerics/valarray/mask-5_neg.cc: New test. * testsuite/26_numerics/valarray/mask-6_neg.cc: New test. * testsuite/26_numerics/valarray/mask-7_neg.cc: New test. * testsuite/26_numerics/valarray/mask-8_neg.cc: New test. * testsuite/26_numerics/valarray/mask.cc: New test. --- diff --git a/libstdc++-v3/include/bits/mask_array.h b/libstdc++-v3/include/bits/mask_array.h index 657ab43fa7b0..d4112a9d0a3a 100644 --- a/libstdc++-v3/include/bits/mask_array.h +++ b/libstdc++-v3/include/bits/mask_array.h @@ -153,6 +153,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline mask_array<_Tp>& mask_array<_Tp>::operator=(const mask_array<_Tp>& __a) { + __glibcxx_assert(__a._M_sz == _M_sz); std::__valarray_copy(__a._M_array, __a._M_mask, _M_sz, _M_array, _M_mask); return *this; @@ -166,13 +167,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline void mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const - { std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); } + { + __glibcxx_assert(__v.size() == _M_sz); + std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); + } template template inline void mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const - { std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); } + { + __glibcxx_assert(__e.size() == _M_sz); + std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); + } #undef _DEFINE_VALARRAY_OPERATOR #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \ @@ -180,6 +187,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void \ mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \ { \ + __glibcxx_assert(__v.size() == _M_sz); \ _Array_augmented_##_Name(_M_array, _M_mask, \ _Array<_Tp>(__v), __v.size()); \ } \ @@ -189,6 +197,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void \ mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\ { \ + __glibcxx_assert(__e.size() == _M_sz); \ _Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size()); \ } diff --git a/libstdc++-v3/include/std/valarray b/libstdc++-v3/include/std/valarray index 7a23c27a0ce2..504d02b73591 100644 --- a/libstdc++-v3/include/std/valarray +++ b/libstdc++-v3/include/std/valarray @@ -893,6 +893,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t __e = __m.size(); for (size_t __i=0; __i<__e; ++__i) if (__m[__i]) ++__s; + __glibcxx_assert(__s <= _M_size); return valarray<_Tp>(mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array (__m))); } @@ -905,6 +906,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t __e = __m.size(); for (size_t __i=0; __i<__e; ++__i) if (__m[__i]) ++__s; + __glibcxx_assert(__s <= _M_size); return mask_array<_Tp>(_Array<_Tp>(_M_data), __s, _Array(__m)); } diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-1_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-1_neg.cc new file mode 100644 index 000000000000..7ef11736d962 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-1_neg.cc @@ -0,0 +1,16 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + + // This is adapted from an example in C++11 [valarray.sub]. + // valarray operator[](const valarray& boolarr) const; + + const valarray v0("ab", 2); + const bool vb[] = {false, false, true, true, false, true}; + (void) v0[valarray(vb, 6)]; // aborts, mask has more elements than v0 +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-2_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-2_neg.cc new file mode 100644 index 000000000000..f380dba17a93 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-2_neg.cc @@ -0,0 +1,16 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + + // This is adapted from an example in C++11 [valarray.sub]. + // mask_array operator[](const valarray& boolarr); + + valarray v0("ab", 2); + const bool vb[] = {false, false, true, true, false, true}; + (void) v0[valarray(vb, 6)]; // aborts, mask has more elements than v0 +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-3_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-3_neg.cc new file mode 100644 index 000000000000..0b9e6fb366d1 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-3_neg.cc @@ -0,0 +1,19 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + // See also PR libstdc++/62196. + + valarray v0("abcdefghijklmnop", 16); + valarray v1("ABCD", 4); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m = v0[valarray(vb, 6)]; + m = v1; // aborts, v1 has more elements than m +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-4_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-4_neg.cc new file mode 100644 index 000000000000..b996967f4ce0 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-4_neg.cc @@ -0,0 +1,18 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + + valarray v0("abcdefghijklmnop", 16); + valarray v1("AB", 2); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m = v0[valarray(vb, 6)]; + m = v1; // aborts, m has more elements than v1 +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-5_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-5_neg.cc new file mode 100644 index 000000000000..8e708903b000 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-5_neg.cc @@ -0,0 +1,19 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + + valarray v0("abcdef", 6); + valarray v1("ABCDEF", 6); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m0 = v0[valarray(vb, 6)]; + const mask_array m1 = v1[valarray(vb, 5)]; + m0 = m1; // aborts, m0 has more elements than m1 +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-6_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-6_neg.cc new file mode 100644 index 000000000000..cded68c45b48 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-6_neg.cc @@ -0,0 +1,19 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + + valarray v0("abcdef", 6); + valarray v1("ABCDEF", 6); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m0 = v0[valarray(vb, 5)]; + const mask_array m1 = v1[valarray(vb, 6)]; + m0 = m1; // aborts, m0 has fewer elements than m1 +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-7_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-7_neg.cc new file mode 100644 index 000000000000..246977b8a8fc --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-7_neg.cc @@ -0,0 +1,18 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + + valarray v0("abcdefghijklmnop", 16); + valarray v1("ABCD", 4); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m = v0[valarray(vb, 6)]; + m += v1; // aborts, v1 has more elements than m +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask-8_neg.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask-8_neg.cc new file mode 100644 index 000000000000..70f9ea253184 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask-8_neg.cc @@ -0,0 +1,18 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } + +#include + +int main() +{ + using std::valarray; + using std::mask_array; + + // This is adapted from an example in C++11 [valarray.sub]. + + valarray v0("abcdefghijklmnop", 16); + valarray v1("AB", 2); + const bool vb[] = {false, false, true, true, false, true}; + const mask_array m = v0[valarray(vb, 6)]; + m += v1; // aborts, v1 has more elements than m +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/mask.cc b/libstdc++-v3/testsuite/26_numerics/valarray/mask.cc new file mode 100644 index 000000000000..cb18701033e1 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/mask.cc @@ -0,0 +1,47 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run } + +#include +#include + +using std::valarray; + +template +bool equal(const valarray& lhs, const valarray& rhs) +{ + if (lhs.size() != rhs.size()) + return false; + for (unsigned i = 0; i < lhs.size(); ++i) + if (lhs[i] != rhs[i]) + return false; + return true; +} + +// Taken from examples in C++11 [valarray.sub]. + +void +test01() // valarray operator[](const valarray& boolarr) const; +{ + const valarray v0("abcdefghijklmnop", 16); + const bool vb[] = {false, false, true, true, false, true}; + valarray v1 = v0[valarray(vb, 6)]; + + VERIFY( equal(v1, valarray("cdf", 3)) ); +} + +void +test02() // mask_array operator[](const valarray& boolarr); +{ + valarray v0("abcdefghijklmnop", 16); + valarray v1("ABC", 3); + const bool vb[] = {false, false, true, true, false, true}; + v0[valarray(vb, 6)] = v1; + + VERIFY( equal(v0, valarray("abABeCghijklmnop", 16)) ); +} + +int main() +{ + test01(); + test02(); +}