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).

Reply via email to