Skip to content

Commit acc1a87

Browse files
committed
Add explicit precondition check
1 parent 1b65294 commit acc1a87

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
@@ -552,28 +552,29 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
552552
}
553553

554554
// Default constructs __n objects starting at __end_
555-
// Precondition: __n > 0
556555
// Precondition: size() + __n <= capacity()
557556
// Postcondition: size() == size() + __n
558557
template <class _Allocator>
559558
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
560559
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
561-
_LIBCPP_ASSERT_INTERNAL(__n > 0, "This function expects __n > 0");
562-
iterator __old_end = end();
560+
_LIBCPP_ASSERT_INTERNAL(
561+
capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
562+
std::fill_n(end(), __n, __x);
563563
this->__size_ += __n;
564-
this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
565-
std::fill_n(__old_end, __n, __x);
564+
if (end().__ctz_ != 0) // has uninitialized trailing bits in the last word
565+
std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
566566
}
567567

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

579580
template <class _Allocator>

0 commit comments

Comments
 (0)