Skip to content

Commit 30bc8d0

Browse files
committed
Add explicit precondition check
1 parent 182f830 commit 30bc8d0

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -549,28 +549,29 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
549549
}
550550

551551
// Default constructs __n objects starting at __end_
552-
// Precondition: __n > 0
553552
// Precondition: size() + __n <= capacity()
554553
// Postcondition: size() == size() + __n
555554
template <class _Allocator>
556555
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
557556
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
558-
_LIBCPP_ASSERT_INTERNAL(__n > 0, "This function expects __n > 0");
559-
iterator __old_end = end();
557+
_LIBCPP_ASSERT_INTERNAL(
558+
capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
559+
std::fill_n(end(), __n, __x);
560560
this->__size_ += __n;
561-
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
562-
std::fill_n(__old_end, __n, __x);
561+
if (end().__ctz_ != 0) // has uninitialized trailing bits in the last word
562+
std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
563563
}
564564

565565
template <class _Allocator>
566566
template <class _InputIterator, class _Sentinel>
567567
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
568568
vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
569-
_LIBCPP_ASSERT_INTERNAL(__n > 0, "This function expects __n > 0");
570-
iterator __old_end = end();
569+
_LIBCPP_ASSERT_INTERNAL(
570+
capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
571+
std::__copy(__first, __last, end());
571572
this->__size_ += __n;
572-
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
573-
std::__copy(__first, __last, __old_end);
573+
if (end().__ctz_ != 0) // has uninitialized trailing bits in the last word
574+
std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
574575
}
575576

576577
template <class _Allocator>

0 commit comments

Comments
 (0)