https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99868
Bug ID: 99868 Summary: std::string is not copied correctly Product: gcc Version: 7.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: apyszczuk at gmail dot com Target Milestone: --- Created attachment 50495 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50495&action=edit save-temps This code does not correctly copy the string *str*. #include <iostream> #include <string> void mod (char* data) { data[0] = 'c'; } int main () { const std::string str {"8:0.0"}; std::string dn {str}; mod (const_cast<char*> (dn.data ())); std::cout << "dn = " << dn << "\n"; std::cout << "str = " << str << "\n"; return 0; } *dn* should be "c:0.0" and *str* should be untouched but in fact both have the same value. If I change: std::string dn {str}; to: std::string dn {str.c_str ()}; then *str* is "8:0.0" as it should be. VERSION: -- Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.2.1-20170829/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 7.2.1 20170829 (Red Hat 7.2.1-1) (GCC) -- COMPILATION: g++ -std=c++17 main.cpp -o main && ./main (in 11 and 14 is the same) As I saw, GCC 8.3 does not have the same issue: https://ideone.com/EuNi6U