This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] libstdc++/8466


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.

diff -urN libstdc++-v3-orig/include/std/std_sstream.h libstdc++-v3/include/std/std_sstream.h
--- libstdc++-v3-orig/include/std/std_sstream.h	2002-03-06 22:22:55.000000000 +0100
+++ libstdc++-v3/include/std/std_sstream.h	2002-11-05 23:05:44.000000000 +0100
@@ -105,7 +105,8 @@
       void
       str(const __string_type& __s)
       {
-	_M_string = __s;
+	// Cannot use _M_string = __s, since v3 strings are COW.
+	_M_string.assign(__s.c_str(), __s.size());
 	_M_stringbuf_init(_M_mode);
       }
 
diff -urN libstdc++-v3-orig/testsuite/27_io/stringstream_members.cc libstdc++-v3/testsuite/27_io/stringstream_members.cc
--- libstdc++-v3-orig/testsuite/27_io/stringstream_members.cc	2002-07-26 03:49:36.000000000 +0200
+++ libstdc++-v3/testsuite/27_io/stringstream_members.cc	2002-11-05 22:57:05.000000000 +0100
@@ -177,10 +177,26 @@
   std::stringstream ss2(str3, std::ios_base::in|std::ios_base::out);
 }
 
+// libstdc++/8466
+void test04()
+{
+  bool test = true;
+
+  const char* strlit00 = "orvieto";
+  const std::string str00 = strlit00;
+
+  std::ostringstream oss;
+
+  oss.str(str00);
+  oss << "cortona";
+  VERIFY( str00 == strlit00 );
+}
+
 int main()
 {
   test01();
   test02();
   test03();
+  test04();
   return 0;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]