Skip to content

Commit 902653c

Browse files
[libc++] Fix assignment in insertion into vector
Changes: - Avoid direct assignment in iterator-pair `insert` overload and `insert_range`, except when the assignment is move assignment.
1 parent c71f914 commit 902653c

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

libcxx/include/__vector/vector.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <__type_traits/is_same.h>
5959
#include <__type_traits/is_trivially_relocatable.h>
6060
#include <__type_traits/type_identity.h>
61+
#include <__utility/declval.h>
6162
#include <__utility/exception_guard.h>
6263
#include <__utility/forward.h>
6364
#include <__utility/is_pointer_in_range.h>
@@ -602,6 +603,30 @@ class _LIBCPP_TEMPLATE_VIS vector {
602603
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
603604
__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n);
604605

606+
template <class _Iterator,
607+
__enable_if_t<!is_same<decltype(*std::declval<_Iterator&>())&&, value_type&&>::value, int> = 0>
608+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
609+
__insert_assign_n_unchecked(_Iterator __first, difference_type __n, pointer __position) {
610+
for (pointer __end_position = __position + __n; __position != __end_position; (void)++__position, ++__first) {
611+
__temp_value<value_type, _Allocator> __tmp(this->__alloc(), *__first);
612+
*__position = std::move(__tmp.get());
613+
}
614+
}
615+
616+
template <class _Iterator,
617+
__enable_if_t<is_same<decltype(*std::declval<_Iterator&>())&&, value_type&&>::value, int> = 0>
618+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
619+
__insert_assign_n_unchecked(_Iterator __first, difference_type __n, pointer __position) {
620+
#if _LIBCPP_STD_VER >= 23
621+
if constexpr (!forward_iterator<_Iterator>) {
622+
ranges::copy_n(std::move(__first), __n, __position);
623+
} else
624+
#endif
625+
{
626+
std::copy_n(__first, __n, __position);
627+
}
628+
}
629+
605630
template <class _InputIterator, class _Sentinel>
606631
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
607632
__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
@@ -1322,19 +1347,12 @@ vector<_Tp, _Allocator>::__insert_with_size(
13221347
__construct_at_end(__m, __last, __n - __dx);
13231348
if (__dx > 0) {
13241349
__move_range(__p, __old_last, __p + __n);
1325-
std::copy(__first, __m, __p);
1350+
__insert_assign_n_unchecked(__first, __dx, __p);
13261351
}
13271352
}
13281353
} else {
13291354
__move_range(__p, __old_last, __p + __n);
1330-
#if _LIBCPP_STD_VER >= 23
1331-
if constexpr (!forward_iterator<_Iterator>) {
1332-
ranges::copy_n(std::move(__first), __n, __p);
1333-
} else
1334-
#endif
1335-
{
1336-
std::copy_n(__first, __n, __p);
1337-
}
1355+
__insert_assign_n_unchecked(std::move(__first), __n, __p);
13381356
}
13391357
} else {
13401358
__split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, this->__alloc_);

libcxx/test/std/containers/sequences/vector/vector.modifiers/insert_iter_iter_iter.pass.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ TEST_CONSTEXPR_CXX20 bool tests() {
161161
for (; j < 105; ++j)
162162
assert(v[j] == 0);
163163
}
164+
{
165+
struct Wrapper {
166+
TEST_CONSTEXPR Wrapper(int n) : n_(n) {}
167+
168+
int n_;
169+
170+
private:
171+
void operator=(int);
172+
};
173+
174+
int a[] = {1, 2, 3, 4, 5};
175+
const std::size_t count = sizeof(a) / sizeof(a[0]);
176+
std::vector<Wrapper> v;
177+
v.insert(v.end(), a, a + count);
178+
assert(v.size() == count);
179+
for (std::size_t i = 0; i != count; ++i)
180+
assert(v[i].n_ == a[i]);
181+
}
164182
#if TEST_STD_VER >= 11
165183
{
166184
typedef std::vector<int, min_allocator<int> > V;

libcxx/test/std/containers/sequences/vector/vector.modifiers/insert_range.pass.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ constexpr bool test() {
6464
}
6565
}
6666

67+
{ // Ensure that insert_range doesn't use unexpected assignment.
68+
struct Wrapper {
69+
constexpr Wrapper(int n) : n_(n) {}
70+
void operator=(int) = delete;
71+
72+
int n_;
73+
};
74+
75+
int a[]{1, 2, 3, 4, 5};
76+
std::vector<Wrapper> v;
77+
v.insert_range(v.end(), a);
78+
assert(v.size() == std::size(a));
79+
for (std::size_t i = 0; i != std::size(a); ++i)
80+
assert(v[i].n_ == a[i]);
81+
}
82+
6783
return true;
6884
}
6985

0 commit comments

Comments
 (0)