Skip to content

Commit ac1d560

Browse files
authored
[libc++][hardening] Add a bounds check for valarray and bitset. (#120685)
Add a `valid-element-access` check to `valarray::operator[]` and `bitset::operator[]`.
1 parent b5f0ec8 commit ac1d560

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

libcxx/docs/Hardening.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ Hardened containers status
458458
- Partial
459459
- N/A
460460
* - ``bitset``
461-
-
461+
-
462462
- N/A
463463

464464
Note: for ``vector`` and ``string``, the iterator does not check for

libcxx/include/bitset

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ template <size_t N> struct hash<std::bitset<N>>;
133133
# include <__algorithm/fill.h>
134134
# include <__algorithm/fill_n.h>
135135
# include <__algorithm/find.h>
136+
# include <__assert>
136137
# include <__bit_reference>
137138
# include <__config>
138139
# include <__functional/hash.h>
@@ -683,13 +684,18 @@ public:
683684

684685
// element access:
685686
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
686-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const { return __base::__make_ref(__p); }
687+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
688+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
689+
return __base::__make_ref(__p);
690+
}
687691
# else
688692
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {
693+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
689694
return __base::__make_ref(__p);
690695
}
691696
# endif
692697
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
698+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
693699
return __base::__make_ref(__p);
694700
}
695701
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const;

libcxx/include/valarray

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,15 @@ public:
821821
_LIBCPP_HIDE_FROM_ABI valarray& operator=(const __val_expr<_ValExpr>& __v);
822822

823823
// element access:
824-
_LIBCPP_HIDE_FROM_ABI const value_type& operator[](size_t __i) const { return __begin_[__i]; }
824+
_LIBCPP_HIDE_FROM_ABI const value_type& operator[](size_t __i) const {
825+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "valarray::operator[] index out of bounds");
826+
return __begin_[__i];
827+
}
825828

826-
_LIBCPP_HIDE_FROM_ABI value_type& operator[](size_t __i) { return __begin_[__i]; }
829+
_LIBCPP_HIDE_FROM_ABI value_type& operator[](size_t __i) {
830+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "valarray::operator[] index out of bounds");
831+
return __begin_[__i];
832+
}
827833

828834
// subset operations:
829835
_LIBCPP_HIDE_FROM_ABI __val_expr<__slice_expr<const valarray&> > operator[](slice __s) const;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <valarray>
10+
11+
// Test hardening assertions for std::valarray.
12+
13+
// REQUIRES: has-unix-headers
14+
// UNSUPPORTED: libcpp-hardening-mode=none
15+
// UNSUPPORTED: c++03
16+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
17+
18+
#include <valarray>
19+
20+
#include "check_assertion.h"
21+
22+
int main(int, char**) {
23+
{ // Empty valarray
24+
std::valarray<int> c;
25+
const auto& const_c = c;
26+
TEST_LIBCPP_ASSERT_FAILURE(c[0], "valarray::operator[] index out of bounds");
27+
TEST_LIBCPP_ASSERT_FAILURE(const_c[0], "valarray::operator[] index out of bounds");
28+
TEST_LIBCPP_ASSERT_FAILURE(c[42], "valarray::operator[] index out of bounds");
29+
TEST_LIBCPP_ASSERT_FAILURE(const_c[42], "valarray::operator[] index out of bounds");
30+
}
31+
32+
{ // Non-empty valarray
33+
std::valarray<int> c(4);
34+
const auto& const_c = c;
35+
(void)c[3]; // Check that there's no assertion on valid access.
36+
TEST_LIBCPP_ASSERT_FAILURE(c[4], "valarray::operator[] index out of bounds");
37+
(void)const_c[3]; // Check that there's no assertion on valid access.
38+
TEST_LIBCPP_ASSERT_FAILURE(const_c[4], "valarray::operator[] index out of bounds");
39+
}
40+
41+
return 0;
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <bitset>
10+
11+
// Test hardening assertions for std::bitset.
12+
13+
// REQUIRES: has-unix-headers
14+
// UNSUPPORTED: libcpp-hardening-mode=none
15+
// UNSUPPORTED: c++03
16+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
17+
18+
#include <bitset>
19+
20+
#include "check_assertion.h"
21+
22+
int main(int, char**) {
23+
{ // Empty bitset
24+
std::bitset<0> c;
25+
const auto& const_c = c;
26+
TEST_LIBCPP_ASSERT_FAILURE(c[0], "bitset::operator[] index out of bounds");
27+
TEST_LIBCPP_ASSERT_FAILURE(const_c[0], "bitset::operator[] index out of bounds");
28+
TEST_LIBCPP_ASSERT_FAILURE(c[42], "bitset::operator[] index out of bounds");
29+
TEST_LIBCPP_ASSERT_FAILURE(const_c[42], "bitset::operator[] index out of bounds");
30+
}
31+
32+
{ // Non-empty bitset
33+
std::bitset<4> c(42);
34+
const auto& const_c = c;
35+
(void)c[3]; // Check that there's no assertion on valid access.
36+
TEST_LIBCPP_ASSERT_FAILURE(c[4], "bitset::operator[] index out of bounds");
37+
(void)const_c[3]; // Check that there's no assertion on valid access.
38+
TEST_LIBCPP_ASSERT_FAILURE(const_c[4], "bitset::operator[] index out of bounds");
39+
}
40+
41+
return 0;
42+
}

libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
10+
911
// bitset<N>& operator&=(const bitset<N>& rhs); // constexpr since C++23
1012

1113
#include <bitset>

0 commit comments

Comments
 (0)