]> gcc.gnu.org Git - gcc.git/commit
libstdc++: Do not use memmove for 1-element ranges [PR108846]
authorJonathan Wakely <jwakely@redhat.com>
Sat, 25 Feb 2023 14:28:36 +0000 (14:28 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 18 Mar 2024 13:50:53 +0000 (13:50 +0000)
commit8ec265c1464dec74f98e6914cd164af5090a39ff
tree23c3ee97df47993466103e8f2a13d79f047ec701
parentaf1efe9ffeb4d21e2895081be1b88031166cdb93
libstdc++: Do not use memmove for 1-element ranges [PR108846]

This avoids overwriting tail padding when algorithms like std::copy are
used to write a single value through a pointer to a base subobject.

The pointer arithmetic on a Base* is valid for N==1, but the copy/move
operation needs to be done using assignment, not a memmove or memcpy of
sizeof(Base) bytes.

Instead of putting a check for N==1 in all of copy, copy_n, move etc.
this adds it to the __copy_move and __copy_move_backward partial
specializations used for trivially copyable types. When N==1 those
partial specializations dispatch to new static member functions of the
partial specializations for non-trivial types, so that a copy/move
assignment is done appropriately for the _IsMove constant.

libstdc++-v3/ChangeLog:

PR libstdc++/108846
* include/bits/stl_algobase.h (__copy_move<false, false, RA>)
Add __assign_one static member function.
(__copy_move<true, false, RA>): Likewise.
(__copy_move<IsMove, true, RA>): Do not use memmove for a single
value.
(__copy_move_backward<IsMove, true, RA>): Likewise.
* testsuite/25_algorithms/copy/108846.cc: New test.
* testsuite/25_algorithms/copy_backward/108846.cc: New test.
* testsuite/25_algorithms/copy_n/108846.cc: New test.
* testsuite/25_algorithms/move/108846.cc: New test.
* testsuite/25_algorithms/move_backward/108846.cc: New test.

(cherry picked from commit 822a11a1e642e0abe92a996e7033a5066905a447)
libstdc++-v3/include/bits/stl_algobase.h
libstdc++-v3/testsuite/25_algorithms/copy/108846.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/copy_backward/108846.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/copy_n/108846.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/move/108846.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/move_backward/108846.cc [new file with mode: 0644]
This page took 0.058279 seconds and 6 git commands to generate.