]> gcc.gnu.org Git - gcc.git/commit
libstdc++: Add assertions to std::mask_array operations [PR62196]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 6 Mar 2023 14:41:59 +0000 (14:41 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 14 Mar 2023 10:28:38 +0000 (10:28 +0000)
commitabb958ada1e4d195f31740659cd8af8bebce7bfd
treec8933d09406ab6f6ad3dd6711efa5ff14d6290b2
parent0e0c18f60af51f3afd210a2929b853dd02abe996
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<T>::operator=(const mask_array<T>&) 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<bool> 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<bool>)):
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.
libstdc++-v3/include/bits/mask_array.h
libstdc++-v3/include/std/valarray
libstdc++-v3/testsuite/26_numerics/valarray/mask-1_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-2_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-3_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-4_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-5_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-6_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-7_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask-8_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/valarray/mask.cc [new file with mode: 0644]
This page took 0.057725 seconds and 6 git commands to generate.