public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/99868] New: std::string is not copied correctly
@ 2021-04-01 13:45 apyszczuk at gmail dot com
  2021-04-01 18:15 ` [Bug libstdc++/99868] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: apyszczuk at gmail dot com @ 2021-04-01 13:45 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug libstdc++/99868] std::string is not copied correctly
  2021-04-01 13:45 [Bug libstdc++/99868] New: std::string is not copied correctly apyszczuk at gmail dot com
@ 2021-04-01 18:15 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-01 18:15 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Artur from comment #0)
> 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 ()));

This is undefined behaviour.


You are using the old std::string implementation, which matches the rules in
the C++03 standard. That standard is very clear about the requirements for
data():

"Requires: The program shall not alter any of the values stored in the
character array."

The character sequence owned by dn is shared with str, so changing one changes
the other. But you can't change it in a correct program, only in a program with
undefined behaviour.



> As I saw, GCC 8.3 does not have the same issue: https://ideone.com/EuNi6U

The oldest supported release is GCC 8.4, nothing older than that is supported
or maintained.

The behaviour is the same in all releases, but you need to use the gcc4
std::string implementation by specifying -D_GLIBCXX_USE_CXX11_ABI=0 (which is
implied when using the GCC from devtoolset).

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-01 18:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 13:45 [Bug libstdc++/99868] New: std::string is not copied correctly apyszczuk at gmail dot com
2021-04-01 18:15 ` [Bug libstdc++/99868] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).