Skip to content

Commit 979e936

Browse files
authored
[libc++] Fix improper static_cast in std::deque and __split_buffer (#119106)
This PR addresses the improper use of `static_cast` to `size_t` where `size_type` is intended. Although the `size_type` member type of STL containers is usually a synonym of `std::size_t`, there is no guarantee that they are always equivalent. The C++ standard does not mandate this equivalence. In libc++'s implementations of `std::deque`, `std::vector`, and `__split_buffer`, the `size_type` member type is defined as `std::allocator_traits<allocator_type>::size_type`, which is either `allocator_type::size_type` if available or `std::make_unsigned<difference_type>::type`. While it is true for `std::allocator` that the `size_type` member type is `std::size_t`, for user-defined allocator types, they may mismatch. This justifies the need to replace `static_cast<size_t>` with `static_cast<size_type>` in this PR.
1 parent 7a3504a commit 979e936

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

libcxx/include/__split_buffer

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_fron
435435
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
436436
__end_ += __d;
437437
} else {
438-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
438+
size_type __c = std::max<size_type>(2 * static_cast<size_type>(__cap_ - __first_), 1);
439439
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
440440
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
441441
std::swap(__first_, __t.__first_);
@@ -458,7 +458,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back
458458
__end_ = std::move(__begin_, __end_, __begin_ - __d);
459459
__begin_ -= __d;
460460
} else {
461-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
461+
size_type __c = std::max<size_type>(2 * static_cast<size_type>(__cap_ - __first_), 1);
462462
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
463463
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
464464
std::swap(__first_, __t.__first_);

libcxx/include/deque

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,7 @@ typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_it
24332433
difference_type __pos = __f - __b;
24342434
iterator __p = __b + __pos;
24352435
allocator_type& __a = __alloc();
2436-
if (static_cast<size_t>(__pos) <= (size() - 1) / 2) { // erase from front
2436+
if (static_cast<size_type>(__pos) <= (size() - 1) / 2) { // erase from front
24372437
std::move_backward(__b, __p, std::next(__p));
24382438
__alloc_traits::destroy(__a, std::addressof(*__b));
24392439
--__size();
@@ -2461,7 +2461,7 @@ typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_it
24612461
iterator __p = __b + __pos;
24622462
if (__n > 0) {
24632463
allocator_type& __a = __alloc();
2464-
if (static_cast<size_t>(__pos) <= (size() - __n) / 2) { // erase from front
2464+
if (static_cast<size_type>(__pos) <= (size() - __n) / 2) { // erase from front
24652465
iterator __i = std::move_backward(__b, __p, __p + __n);
24662466
for (; __b != __i; ++__b)
24672467
__alloc_traits::destroy(__a, std::addressof(*__b));

0 commit comments

Comments
 (0)