]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix std::deque::insert(pos, first, last) undefined behaviour [PR118035]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 16 Dec 2024 17:42:24 +0000 (17:42 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 9 Jan 2025 23:52:29 +0000 (23:52 +0000)
commit67c457de5a3f74151fc2d0085387127bf9e4e3c5
tree762a7c21188c9cc6ad0cca979eb215b51ccfa1e2
parentf73ecaf1957d35a99909999735ebe228c8dd6188
libstdc++: Fix std::deque::insert(pos, first, last) undefined behaviour [PR118035]

Inserting an empty range into a std::deque results in undefined calls to
either std::copy, std::copy_backward, std::move, or std::move_backward.
We call those algos with invalid arguments where the output range is the
same as the input range, e.g.  std::copy(first, last, first) which
violates the preconditions for the algorithms.

This fix simply returns early if there's nothing to insert. Most callers
already ensure that we don't even call _M_range_insert_aux with an empty
range, but some callers don't. Rather than checking for n == 0 in each
of the callers, this just does the check once and uses __builtin_expect
to treat empty insertions as unlikely.

libstdc++-v3/ChangeLog:

PR libstdc++/118035
* include/bits/deque.tcc (_M_range_insert_aux): Return
immediately if inserting an empty range.
* testsuite/23_containers/deque/modifiers/insert/118035.cc: New
test.

(cherry picked from commit b273e25e11c842a5729d0e03c85088cf5ba8e06c)
libstdc++-v3/include/bits/deque.tcc
libstdc++-v3/testsuite/23_containers/deque/modifiers/insert/118035.cc [new file with mode: 0644]