Skip to content

Commit 7a5ad55

Browse files
committed
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 <[email protected]> Reviewed-by: Patrick Palka <[email protected]>
1 parent e0fa6ea commit 7a5ad55

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

libstdc++-v3/include/bits/cpp_type_traits.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ __INT_N(__int128)
518518
struct __memcpyable_integer<volatile _Tp>
519519
{ enum { __width = 0 }; };
520520

521+
// Assigning an integer to bool needs to convert all non-zero values to true
522+
// so it is not a memcpyable integer.
523+
// __memcpyable<bool*, bool*> is still true though.
524+
template<>
525+
struct __memcpyable_integer<bool>
526+
{ enum { __width = 0 }; };
527+
521528
// Specializations for __intNN types with padding bits.
522529
#if defined __GLIBCXX_TYPE_INT_N_0 && __GLIBCXX_BITSIZE_INT_N_0 % __CHAR_BIT__
523530
__extension__
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// { dg-do run }
2+
3+
// Bug libstdc++/122907
4+
// std::copy incorrectly uses memcpy when copying from signed or unsigned char
5+
// buffer to bool buffer
6+
7+
#include <algorithm>
8+
#include <testsuite_hooks.h>
9+
10+
template<typename T>
11+
__attribute__((noinline,noipa))
12+
void
13+
test_pr122907(T (&buf)[4])
14+
{
15+
unsigned char uc[4];
16+
bool bool_buf[4];
17+
std::copy(buf, buf+1, bool_buf);
18+
std::copy(bool_buf, bool_buf+1, uc);
19+
VERIFY(uc[0] == bool(buf[0]));
20+
std::copy(buf, buf+4, bool_buf);
21+
std::copy(bool_buf, bool_buf+4, uc);
22+
VERIFY(uc[0] == bool(buf[0]));
23+
VERIFY(uc[1] == bool(buf[1]));
24+
VERIFY(uc[2] == bool(buf[2]));
25+
VERIFY(uc[3] == bool(buf[3]));
26+
}
27+
28+
template<typename T>
29+
void
30+
test_pr122907()
31+
{
32+
T buf[4] = { (T)3, (T)2, (T)1, (T)0 };
33+
test_pr122907(buf);
34+
}
35+
36+
int main()
37+
{
38+
test_pr122907<char>();
39+
test_pr122907<signed char>();
40+
test_pr122907<unsigned char>();
41+
test_pr122907<bool>();
42+
test_pr122907<int>();
43+
}

0 commit comments

Comments
 (0)