This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]

[v3] Fix two bugs in string::_S_create


Hi,

the below fixes two bugs: one *very* long standing, where a string
bigger than max_size() could be constructed; the other introduced by
myself a couple of days ago, leading to a small memory leak in some
circumstances.

Tested x86-linux, committing to mainline.

Paolo.

////////////////
2004-01-30  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.tcc (_Rep::_S_create):
	Never allocate a string bigger than max_size(); always keep
	__capacity and __size in sync to avoid memory leaks at
	deallocation time.
diff -urN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc	2004-01-30 10:06:13.000000000 +0100
+++ libstdc++-v3/include/bits/basic_string.tcc	2004-01-30 14:03:49.000000000 +0100
@@ -520,7 +520,10 @@
 				     - (__size + __malloc_header_size)
 				     % __pagesize);
 	  __capacity += __extra / sizeof(_CharT);
-	  __size += __extra;
+	  // Never allocate a string bigger than _S_max_size.
+	  if (__capacity > _S_max_size)
+	    __capacity = _S_max_size;
+	  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
 	}
       else if (__size > __subpagesize)
 	{
@@ -528,7 +531,7 @@
 				     - (__size + __malloc_header_size)
 				     % __subpagesize);
 	  __capacity += __extra / sizeof(_CharT);
-	  __size += __extra;
+	  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
 	}
 
       // NB: Might throw, but no worries about a leak, mate: _Rep()

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