[PATCH] libstdc++/8466

Paolo Carlini pcarlini@unitus.it
Tue Nov 5 14:34:00 GMT 2002


Hi,

the bug is due to the Copy On Write nature of v3 string: in
varios members of basic_stringbuf and basic_(i,o)stringstream
a string is simply considered as a buffer of chars.
Therefore

      void
      str(const __string_type& __s)
      {
        _M_string = __s;
        _M_stringbuf_init(_M_mode);
      }

which produces an _M_string having the Rep shared with __s
is very dangerous, since, as soon as the buffer (the Rep)
is directly modified, the original __s ends up being clobbered!

I have fixed this with a "low-level assignment" of __s
to _M_string:

      void
      str(const __string_type& __s)
      {   
        _M_string.assign(__s.c_str(), __s.size());
        _M_stringbuf_init(_M_mode);
      }

Tested x86-linux.

Ok?

Ciao, Paolo.

//////////

2002-11-05  Paolo Carlini  <pcarlini@unitus.it>

        PR libstdc++/8466
        * include/std/std_sstream.h
        (basic_stringbuf::str(const __string_type&)):
        Cannot use simple assignment since the COW-nature of v3
        basic_string is not taken into account in basic_stringbuf.
        * testsuite/27_io/stringstream_members.cc: Add test04 from PR.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_8466
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20021105/9fdb64de/attachment.ksh>


More information about the Libstdc++ mailing list