https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122907

            Bug ID: 122907
           Summary: std::copy incorrectly uses memcpy when copying from
                    signed or unsigned char buffer to bool buffer
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: complex_octonion at protonmail dot com
  Target Milestone: ---

Created attachment 62936
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62936&action=edit
Example program demonstrating the bug

When using std::copy<unsigned char*, bool*> the actual values in the
destination buffer doesn't correspond to a copy assignment of each element.

The problem occurs both in gcc 15.1 and gcc 15.2 (see output from gcc -v below
for detailed version information), but not in any gcc 14 version. The problem
also occurs using "x86_64 gcc (trunk)" (gcc version 16.0.0 20251129
(experimental)) on the godbolt site. The problem was likely introduced by
commit 308d19c11e119b2c5abf67778dd0ac8a370e5df7 "libstdc++: Enable memcpy
optimizations for distinct integral types [PR93059]".

The attached example program demonstrates the problem by showing the actual
memory contents after the copy compared to when explicitly copying each
element. It also shows that the incorrect std::copy in turn causes incorrect
results in a function taking the bool buffer that was copied into. The program
can be compiled and run with the following command "g++ -O0 -Wall -Wextra -o
/tmp/a.out std_copy_bug.cpp && /tmp/a.out".

Output from gcc -v:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --with-linker-hash-style=gnu
--with-system-zlib --enable-__cxa_atexit --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.2.1 20251112 (GCC)

Reply via email to