[Bug libstdc++/122907] [15 Regression] std::copy incorrectly uses memcpy when copying from signed or unsigned char buffer to bool buffer
cvs-commit at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Dec 17 13:20:47 GMT 2025
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122907
--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-15 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:
https://gcc.gnu.org/g:e7664c01527f59b65438fe033b84f89191754300
commit r15-10616-ge7664c01527f59b65438fe033b84f89191754300
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Dec 2 15:26:31 2025 +0000
libstdc++: Do not optimize std::copy to memcpy for bool output [PR122907]
Copying narrow characters to a range of bool using std::copy cannot be
optimized to use std::memcpy. Assignment of an arbitrary integer to a
bool needs to convert all non-zero values to true, so is not a simple
memcpy-like or bit_cast-like operation. We currently get this wrong and
optimize it to memcpy, producing invalid bool values.
By making __memcpyable_integer<bool> false we disable memcpy
optimizations for heterogeneous std::copy and std::move calls where
either the source or destination type is bool. Copies where both types
are bool can still optimize to memcpy, because we don't check
__memcpyable_integer in that case.
This disables the memcpy optimization for bool as the source type,
which isn't actually necessary (the representation of bool in GCC is
0x00 or 0x01 and so copying bool to char is just a bit_cast). We don't
currently have a straightforward way to allow memcpy for bool to char
but disallow the inverse. This seems acceptable as using std::copy with
bool inputs and narrow character outputs is probably not common enough
for this to be an important optimization to do in the library code.
libstdc++-v3/ChangeLog:
PR libstdc++/122907
* include/bits/cpp_type_traits.h (__memcpyable_integer<bool>):
Define as false.
* testsuite/25_algorithms/copy/122907.cc: New test.
Reviewed-by: Tomasz KamiÅski <tkaminsk@redhat.com>
Reviewed-by: Patrick Palka <ppalka@redhat.com>
(cherry picked from commit 7a5ad555965d103e73e606417f4289a4238e82d4)
More information about the Gcc-bugs
mailing list