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]

[libstdc++ trunk & 3.0] Re: string::resize bug (PR4548)


On Tue, Nov 20, 2001 at 07:45:32PM -0800, Benjamin Kosnik wrote:
> This patch is fine, thanks Paolo. My net access/sources are both a bit of a 
> mess right now. 
> 
> Phil, can you check this in please?

Tested on i686/linux, no regressions, trunk and branch.


2001-11-21  Paolo Carlini  <pcarlini@unitus.it>

	PR libstdc++/4548
	* include/bits/basic_string.tcc (basic_string::reserve):  Never shrink
	below the current size.
	* testsuite/21_strings/capacity.cc (test02):  Add test.


Index: include/bits/basic_string.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/basic_string.tcc,v
retrieving revision 1.8
diff -u -3 -p -r1.8 basic_string.tcc
--- basic_string.tcc	2001/10/31 08:27:19	1.8
+++ basic_string.tcc	2001/11/21 22:02:30
@@ -315,6 +315,9 @@ namespace std
         {
 	  if (__res > this->max_size())
 	    __throw_length_error("basic_string::reserve");
+	  // Make sure we don't shrink below the current size
+	  if (__res < this->size())
+	    __res = this->size();
 	  allocator_type __a = get_allocator();
 	  _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
 	  _M_rep()->_M_dispose(__a);
Index: testsuite/21_strings/capacity.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/21_strings/capacity.cc,v
retrieving revision 1.4
diff -u -3 -p -r1.4 capacity.cc
--- capacity.cc	2001/08/07 03:38:28	1.4
+++ capacity.cc	2001/11/21 22:02:30
@@ -169,9 +169,30 @@ bool test01()
   return test;
 }
 
+// libstdc++/4548
+// http://gcc.gnu.org/ml/libstdc++/2001-11/msg00150.html
+bool test02()
+{
+  bool test = true;
+
+  std::string str01 = "twelve chars";
+  // str01 becomes shared
+  std::string str02 = str01;
+  str01.reserve(1);
+  VERIFY( str01.capacity() == 12 );
+
+#ifdef DEBUG_ASSERT
+  assert(test);
+#endif
+
+  return test;
+}
+
+
 int main()
 {
   test01();
+  test02();
 
   return 0;
 }


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