Skip to content

Commit dfdccda

Browse files
STL Hardening (#5274)
1 parent dfbe5ea commit dfdccda

File tree

53 files changed

+1203
-416
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1203
-416
lines changed

stl/inc/__msvc_string_view.hpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,8 @@ class basic_string_view { // wrapper for any kind of contiguous character buffer
13791379
constexpr basic_string_view(
13801380
_In_reads_(_Count) const const_pointer _Cts, const size_type _Count) noexcept // strengthened
13811381
: _Mydata(_Cts), _Mysize(_Count) {
1382-
#if _CONTAINER_DEBUG_LEVEL > 0
1383-
_STL_VERIFY(_Count == 0 || _Cts, "non-zero size null string_view");
1382+
#if _ITERATOR_DEBUG_LEVEL != 0
1383+
_STL_VERIFY(_Count == 0 || _Cts, "cannot construct a string_view from a null pointer and a non-zero size");
13841384
#endif
13851385
}
13861386

@@ -1474,7 +1474,7 @@ class basic_string_view { // wrapper for any kind of contiguous character buffer
14741474
}
14751475

14761476
_NODISCARD constexpr const_reference operator[](const size_type _Off) const noexcept /* strengthened */ {
1477-
#if _CONTAINER_DEBUG_LEVEL > 0
1477+
#if _MSVC_STL_HARDENING_BASIC_STRING_VIEW || _ITERATOR_DEBUG_LEVEL != 0
14781478
_STL_VERIFY(_Off < _Mysize, "string_view subscript out of range");
14791479
#endif
14801480

@@ -1489,31 +1489,35 @@ class basic_string_view { // wrapper for any kind of contiguous character buffer
14891489
}
14901490

14911491
_NODISCARD constexpr const_reference front() const noexcept /* strengthened */ {
1492-
#if _CONTAINER_DEBUG_LEVEL > 0
1492+
#if _MSVC_STL_HARDENING_BASIC_STRING_VIEW || _ITERATOR_DEBUG_LEVEL != 0
14931493
_STL_VERIFY(_Mysize != 0, "front() called on empty string_view");
14941494
#endif
1495+
14951496
return _Mydata[0];
14961497
}
14971498

14981499
_NODISCARD constexpr const_reference back() const noexcept /* strengthened */ {
1499-
#if _CONTAINER_DEBUG_LEVEL > 0
1500+
#if _MSVC_STL_HARDENING_BASIC_STRING_VIEW || _ITERATOR_DEBUG_LEVEL != 0
15001501
_STL_VERIFY(_Mysize != 0, "back() called on empty string_view");
15011502
#endif
1503+
15021504
return _Mydata[_Mysize - 1];
15031505
}
15041506

15051507
constexpr void remove_prefix(const size_type _Count) noexcept /* strengthened */ {
1506-
#if _CONTAINER_DEBUG_LEVEL > 0
1508+
#if _MSVC_STL_HARDENING_BASIC_STRING_VIEW || _ITERATOR_DEBUG_LEVEL != 0
15071509
_STL_VERIFY(_Mysize >= _Count, "cannot remove_prefix() larger than string_view size");
15081510
#endif
1511+
15091512
_Mydata += _Count;
15101513
_Mysize -= _Count;
15111514
}
15121515

15131516
constexpr void remove_suffix(const size_type _Count) noexcept /* strengthened */ {
1514-
#if _CONTAINER_DEBUG_LEVEL > 0
1517+
#if _MSVC_STL_HARDENING_BASIC_STRING_VIEW || _ITERATOR_DEBUG_LEVEL != 0
15151518
_STL_VERIFY(_Mysize >= _Count, "cannot remove_suffix() larger than string_view size");
15161519
#endif
1520+
15171521
_Mysize -= _Count;
15181522
}
15191523

stl/inc/array

+8-8
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public:
531531
}
532532

533533
_NODISCARD _CONSTEXPR17 reference operator[](_In_range_(<, _Size) size_type _Pos) noexcept /* strengthened */ {
534-
#if _CONTAINER_DEBUG_LEVEL > 0
534+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
535535
_STL_VERIFY(_Pos < _Size, "array subscript out of range");
536536
#endif
537537

@@ -540,7 +540,7 @@ public:
540540

541541
_NODISCARD constexpr const_reference operator[](_In_range_(<, _Size) size_type _Pos) const noexcept
542542
/* strengthened */ {
543-
#if _CONTAINER_DEBUG_LEVEL > 0
543+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
544544
_STL_VERIFY(_Pos < _Size, "array subscript out of range");
545545
#endif
546546

@@ -707,47 +707,47 @@ public:
707707
}
708708

709709
_NODISCARD reference operator[](size_type) noexcept /* strengthened */ {
710-
#if _CONTAINER_DEBUG_LEVEL > 0
710+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
711711
_STL_REPORT_ERROR("array<T, 0> subscript is invalid");
712712
#endif
713713

714714
return *data();
715715
}
716716

717717
_NODISCARD const_reference operator[](size_type) const noexcept /* strengthened */ {
718-
#if _CONTAINER_DEBUG_LEVEL > 0
718+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
719719
_STL_REPORT_ERROR("array<T, 0> subscript is invalid");
720720
#endif
721721

722722
return *data();
723723
}
724724

725725
_NODISCARD reference front() noexcept /* strengthened */ {
726-
#if _CONTAINER_DEBUG_LEVEL > 0
726+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
727727
_STL_REPORT_ERROR("array<T, 0>::front() is invalid");
728728
#endif
729729

730730
return *data();
731731
}
732732

733733
_NODISCARD const_reference front() const noexcept /* strengthened */ {
734-
#if _CONTAINER_DEBUG_LEVEL > 0
734+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
735735
_STL_REPORT_ERROR("array<T, 0>::front() is invalid");
736736
#endif
737737

738738
return *data();
739739
}
740740

741741
_NODISCARD reference back() noexcept /* strengthened */ {
742-
#if _CONTAINER_DEBUG_LEVEL > 0
742+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
743743
_STL_REPORT_ERROR("array<T, 0>::back() is invalid");
744744
#endif
745745

746746
return *data();
747747
}
748748

749749
_NODISCARD const_reference back() const noexcept /* strengthened */ {
750-
#if _CONTAINER_DEBUG_LEVEL > 0
750+
#if _MSVC_STL_HARDENING_ARRAY || _ITERATOR_DEBUG_LEVEL != 0
751751
_STL_REPORT_ERROR("array<T, 0>::back() is invalid");
752752
#endif
753753

stl/inc/bitset

+8-10
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,6 @@ public:
116116
};
117117

118118
private:
119-
static constexpr void _Validate(const size_t _Pos) noexcept { // verify that _Pos is within bounds
120-
#if _ITERATOR_DEBUG_LEVEL == 0
121-
(void) _Pos;
122-
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 0 / _ITERATOR_DEBUG_LEVEL != 0 vvv
123-
_STL_VERIFY(_Pos < _Bits, "bitset subscript out of range");
124-
#endif // ^^^ _ITERATOR_DEBUG_LEVEL != 0 ^^^
125-
}
126-
127119
constexpr bool _Subscript(size_t _Pos) const noexcept {
128120
return (_Array[_Pos / _Bitsperword] & (_Ty{1} << _Pos % _Bitsperword)) != 0;
129121
}
@@ -133,12 +125,18 @@ private:
133125

134126
public:
135127
_NODISCARD constexpr bool operator[](const size_t _Pos) const noexcept /* strengthened */ {
136-
_Validate(_Pos);
128+
#if _MSVC_STL_HARDENING_BITSET || _ITERATOR_DEBUG_LEVEL != 0
129+
_STL_VERIFY(_Pos < _Bits, "bitset subscript out of range");
130+
#endif
131+
137132
return _Subscript(_Pos);
138133
}
139134

140135
_NODISCARD _CONSTEXPR23 reference operator[](const size_t _Pos) noexcept /* strengthened */ {
141-
_Validate(_Pos);
136+
#if _MSVC_STL_HARDENING_BITSET || _ITERATOR_DEBUG_LEVEL != 0
137+
_STL_VERIFY(_Pos < _Bits, "bitset subscript out of range");
138+
#endif
139+
142140
return reference(*this, _Pos);
143141
}
144142

stl/inc/deque

+19-32
Original file line numberDiff line numberDiff line change
@@ -1063,15 +1063,15 @@ public:
10631063
}
10641064

10651065
_NODISCARD const_reference operator[](size_type _Pos) const noexcept /* strengthened */ {
1066-
#if _CONTAINER_DEBUG_LEVEL > 0
1066+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
10671067
_STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range");
10681068
#endif
10691069

10701070
return _Subscript(_Pos);
10711071
}
10721072

10731073
_NODISCARD reference operator[](size_type _Pos) noexcept /* strengthened */ {
1074-
#if _CONTAINER_DEBUG_LEVEL > 0
1074+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
10751075
_STL_VERIFY(_Pos < _Mysize(), "deque subscript out of range");
10761076
#endif
10771077

@@ -1095,31 +1095,31 @@ public:
10951095
}
10961096

10971097
_NODISCARD reference front() noexcept /* strengthened */ {
1098-
#if _CONTAINER_DEBUG_LEVEL > 0
1098+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
10991099
_STL_VERIFY(!empty(), "front() called on empty deque");
11001100
#endif
11011101

11021102
return _Subscript(0);
11031103
}
11041104

11051105
_NODISCARD const_reference front() const noexcept /* strengthened */ {
1106-
#if _CONTAINER_DEBUG_LEVEL > 0
1106+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
11071107
_STL_VERIFY(!empty(), "front() called on empty deque");
11081108
#endif
11091109

11101110
return _Subscript(0);
11111111
}
11121112

11131113
_NODISCARD reference back() noexcept /* strengthened */ {
1114-
#if _CONTAINER_DEBUG_LEVEL > 0
1114+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
11151115
_STL_VERIFY(!empty(), "back() called on empty deque");
11161116
#endif
11171117

11181118
return _Subscript(_Mysize() - 1);
11191119
}
11201120

11211121
_NODISCARD const_reference back() const noexcept /* strengthened */ {
1122-
#if _CONTAINER_DEBUG_LEVEL > 0
1122+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
11231123
_STL_VERIFY(!empty(), "back() called on empty deque");
11241124
#endif
11251125

@@ -1473,47 +1473,34 @@ private:
14731473

14741474
public:
14751475
void pop_front() noexcept /* strengthened */ {
1476+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
1477+
_STL_VERIFY(!empty(), "pop_front() called on empty deque");
1478+
#endif
1479+
14761480
#if _ITERATOR_DEBUG_LEVEL == 2
1477-
if (empty()) {
1478-
_STL_REPORT_ERROR("pop_front() called on empty deque");
1479-
} else { // something to erase, do it
1480-
_Orphan_off(_Myoff());
1481-
_Alty_traits::destroy(_Getal(), _Get_data()._Address_subscript(_Myoff()));
1482-
if (--_Mysize() == 0) {
1483-
_Myoff() = 0;
1484-
} else {
1485-
++_Myoff();
1486-
}
1487-
}
1488-
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv
1481+
_Orphan_off(_Myoff());
1482+
#endif
14891483
_Alty_traits::destroy(_Getal(), _Get_data()._Address_subscript(_Myoff()));
14901484
if (--_Mysize() == 0) {
14911485
_Myoff() = 0;
14921486
} else {
14931487
++_Myoff();
14941488
}
1495-
#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^
14961489
}
14971490

14981491
void pop_back() noexcept /* strengthened */ {
1499-
#if _ITERATOR_DEBUG_LEVEL == 2
1500-
if (empty()) {
1501-
_STL_REPORT_ERROR("pop_back() called on empty deque");
1502-
} else { // something to erase, do it
1503-
size_type _Newoff = _Myoff() + _Mysize() - 1;
1504-
_Orphan_off(_Newoff);
1505-
_Alty_traits::destroy(_Getal(), _Get_data()._Address_subscript(_Newoff));
1506-
if (--_Mysize() == 0) {
1507-
_Myoff() = 0;
1508-
}
1509-
}
1510-
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL < 2 vvv
1492+
#if _MSVC_STL_HARDENING_DEQUE || _ITERATOR_DEBUG_LEVEL != 0
1493+
_STL_VERIFY(!empty(), "pop_back() called on empty deque");
1494+
#endif
1495+
15111496
size_type _Newoff = _Myoff() + _Mysize() - 1;
1497+
#if _ITERATOR_DEBUG_LEVEL == 2
1498+
_Orphan_off(_Newoff);
1499+
#endif
15121500
_Alty_traits::destroy(_Getal(), _Get_data()._Address_subscript(_Newoff));
15131501
if (--_Mysize() == 0) {
15141502
_Myoff() = 0;
15151503
}
1516-
#endif // ^^^ _ITERATOR_DEBUG_LEVEL < 2 ^^^
15171504
}
15181505

15191506
iterator erase(const_iterator _Where) noexcept(is_nothrow_move_assignable_v<value_type>) /* strengthened */ {

0 commit comments

Comments
 (0)