diff options
author | Paolo Carlini <pcarlini@unitus.it> | 2001-11-21 23:04:53 +0100 |
---|---|---|
committer | Phil Edwards <pme@gcc.gnu.org> | 2001-11-21 22:04:53 +0000 |
commit | dcc61724f1453581d64ded7c641dec62e4e3c318 (patch) | |
tree | 179e44e7ecf94dbb7f432620753976d7cd7b18da | |
parent | ae1139f97d3a90768228333a245f5ef4ed0f8326 (diff) | |
download | gcc-dcc61724f1453581d64ded7c641dec62e4e3c318.zip gcc-dcc61724f1453581d64ded7c641dec62e4e3c318.tar.gz gcc-dcc61724f1453581d64ded7c641dec62e4e3c318.tar.bz2 |
re PR libstdc++/4548 (When reserving a string to become smaller, program crashes)
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.
From-SVN: r47246
-rw-r--r-- | libstdc++-v3/ChangeLog | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/basic_string.tcc | 3 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/21_strings/capacity.cc | 21 |
3 files changed, 31 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 42a766a..deb1321 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +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. + 2001-11-19 Phil Edwards <pme@gcc.gnu.org> * 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 84ec0cf..d1dd1cb 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 ed47e4e..8239f1b 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; } |