diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 42a766a9d98..deb1321957e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2001-11-21 Paolo Carlini + + 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. + 2001-11-19 Phil Edwards * docs/doxygen/Intro.3: More notes. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 84ec0cfc132..d1dd1cb5d99 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -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); diff --git a/libstdc++-v3/testsuite/21_strings/capacity.cc b/libstdc++-v3/testsuite/21_strings/capacity.cc index ed47e4e26f2..8239f1b6488 100644 --- a/libstdc++-v3/testsuite/21_strings/capacity.cc +++ b/libstdc++-v3/testsuite/21_strings/capacity.cc @@ -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; }