[PATCH] Tighten length error check in string::replace

Paolo Carlini pcarlini@unitus.it
Wed Apr 24 13:55:00 GMT 2002


Hi,

yesterday I noticed that the length error check in a string::replace method is not sufficiently tight, since it considers __n1 as is, not folded to the final value. I'd like to commit the following straightforward fix to trunk and, after release, branch too.

Ok?

Ciao, Paolo.

//////////////

2002-04-24  Paolo Carlini  <pcarlini@unitus.it>

	* include/bits/basic_string.h (replace(__pos, __n1, __s, __n2):
	Tighten __throw_length_error check.

--- basic_string.h.~1.23.~	2002-04-16 04:29:20.000000000 +0200
+++ basic_string.h	2002-04-23 23:55:33.000000000 +0200
@@ -642,10 +642,10 @@
 	const size_type __size = this->size();
  	if (__pos > __size)
 	  __throw_out_of_range("basic_string::replace");
-	if (__size - __n1 > this->max_size() - __n2)
-	  __throw_length_error("basic_string::replace");
 	const bool __testn1 = __n1 < __size - __pos;
 	const size_type __foldn1 = __testn1 ? __n1 : __size - __pos;
+	if (__size - __foldn1 > this->max_size() - __n2)
+	  __throw_length_error("basic_string::replace");
 	if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
 	    || less<const _CharT*>()(_M_data() + __size, __s))
 	  return _M_replace_safe(_M_ibegin() + __pos,




More information about the Libstdc++ mailing list