]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix std::vector::append_range for overlapping ranges
authorJonathan Wakely <jwakely@redhat.com>
Fri, 21 Mar 2025 23:21:42 +0000 (23:21 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 25 Mar 2025 17:38:39 +0000 (17:38 +0000)
commitd4f7d18b3ed83a8265be913bb230f6efa395e527
treec68e552996c517ea6a38c0f1f4c2864f64b7e47c
parent7bec4570301c43ab948aca624d4cd7a3b33d65a1
libstdc++: Fix std::vector::append_range for overlapping ranges

Unlike insert_range and assign_range, the append_range function does not
have a precondition that the range doesn't overlap *this. That means we
need to avoid relocating the existing elements until after copying from
the range. This means I need to revert r15-8488-g3e1d760bf49d0e which
made the from_range_t constructor use append_range, because the
constructor can avoid the additional complexity needed by append_range.
When relocating the existing elements in append_range we can use
std::__relocate_a to do it more efficiently, if that's valid.

std::vector<bool>::append_range needs similar treatment, although it's a
bit simpler as we know that the elements are trivially copyable and so
we don't need to worry about them throwing. assign_range doesn't allow
overlapping ranges, so can be rewritten to be more efficient than
calling append_range for the forward or sized range case.

libstdc++-v3/ChangeLog:

* include/bits/stl_bvector.h (vector::assign_range): More
efficient implementation for forward/sized ranges.
(vector::append_range): Handle potentially overlapping range.
* include/bits/stl_vector.h (vector(from_range_t, R&&, Alloc)):
Do not use append_range for non-sized input range case.
(vector::append_range): Handle potentially overlapping range.
* include/bits/vector.tcc (vector::insert_range): Forward range
instead of moving it.
* testsuite/23_containers/vector/bool/modifiers/insert/append_range.cc:
Test overlapping ranges.
* testsuite/23_containers/vector/modifiers/append_range.cc:
Likewise.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/bits/vector.tcc
libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/append_range.cc
libstdc++-v3/testsuite/23_containers/vector/modifiers/append_range.cc