From 48d1c3c568c1417b86ed9e23f48c5d4344a2d096 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Wed, 13 Nov 2002 22:15:17 +0000 Subject: [PATCH] re PR libstdc++/8230 (Buggy allocator behaviour) 2002-11-13 Benjamin Kosnik PR libstdc++/8230 * include/bits/vector.tcc (vector::reserve): Throw length_error if requested size is bigger than max_size(). * include/bits/stl_bvector.h (vector::reserve): Same. * testsuite/23_containers/vector_capacity.cc (test02): Add. From-SVN: r59090 --- libstdc++-v3/ChangeLog | 10 +++- libstdc++-v3/include/bits/stl_bvector.h | 4 +- libstdc++-v3/include/bits/vector.tcc | 22 +++---- .../23_containers/vector_capacity.cc | 58 ++++++++++++++++--- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 73ffef2a680..faa6414bd70 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,10 +1,18 @@ +2002-11-13 Benjamin Kosnik + + PR libstdc++/8230 + * include/bits/vector.tcc (vector::reserve): Throw length_error if + requested size is bigger than max_size(). + * include/bits/stl_bvector.h (vector::reserve): Same. + * testsuite/23_containers/vector_capacity.cc (test02): Add. + 2002-11-13 Benjamin Kosnik * config/linker-map.gnu: Export all _S_construct. 2002-11-13 Loren J. Rittle - libstdc++/7445 + PR libstdc++/7445 * src/locale.cc (std::locale::classic()): Weaken locking protocol. 2002-11-13 Jonathan Wakely diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h index 59e7b7f967c..2c97d470582 100644 --- a/libstdc++-v3/include/bits/stl_bvector.h +++ b/libstdc++-v3/include/bits/stl_bvector.h @@ -604,7 +604,9 @@ template } void reserve(size_type __n) { - if (capacity() < __n) { + if (__n > this->max_size()) + __throw_length_error("vector::reserve"); + if (this->capacity() < __n) { _Bit_type * __q = _M_bit_alloc(__n); _M_finish = copy(begin(), end(), iterator(__q, 0)); _M_deallocate(); diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 9b21d0da9fc..4e742dd8672 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -68,16 +68,18 @@ namespace std vector<_Tp,_Alloc>:: reserve(size_type __n) { - if (capacity() < __n) - { - const size_type __old_size = size(); - pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish); - _Destroy(_M_start, _M_finish); - _M_deallocate(_M_start, _M_end_of_storage - _M_start); - _M_start = __tmp; - _M_finish = __tmp + __old_size; - _M_end_of_storage = _M_start + __n; - } + if (__n > this->max_size()) + __throw_length_error("vector::reserve"); + if (this->capacity() < __n) + { + const size_type __old_size = size(); + pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish); + _Destroy(_M_start, _M_finish); + _M_deallocate(_M_start, _M_end_of_storage - _M_start); + _M_start = __tmp; + _M_finish = __tmp + __old_size; + _M_end_of_storage = _M_start + __n; + } } template diff --git a/libstdc++-v3/testsuite/23_containers/vector_capacity.cc b/libstdc++-v3/testsuite/23_containers/vector_capacity.cc index a56f2ef40c4..e73b15a3246 100644 --- a/libstdc++-v3/testsuite/23_containers/vector_capacity.cc +++ b/libstdc++-v3/testsuite/23_containers/vector_capacity.cc @@ -1,7 +1,7 @@ // 1999-05-07 // bkoz -// Copyright (C) 1999 Free Software Foundation, Inc. +// Copyright (C) 1999, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -22,6 +22,7 @@ // 23.2.4.2 vector capacity #include +#include #include template @@ -29,9 +30,8 @@ template struct B { }; -bool test01() +void test01() { - // non POD types bool test = true; std::vector< A > vec01; @@ -51,17 +51,57 @@ bool test01() vec01.resize(sz01); sz02 = vec01.size(); VERIFY( sz01 == sz02 ); +} -#ifdef DEBUG_ASSERT - assert(test); -#endif - - return test; +// libstdc++/8230 +void test02() +{ + bool test = true; + + { + std::vector array; + const std::size_t size = array.max_size(); + try + { + array.reserve(size); + } + catch (const std::length_error& error) + { + test &= false; + } + catch (const std::bad_alloc& error) + { + test &= true; + } + catch (...) + { + test &= false; + } + VERIFY( test ); + } + + { + std::vector array; + const std::size_t size = array.max_size() + 1; + try + { + array.reserve(size); + } + catch (const std::length_error& error) + { + test &= true; + } + catch (...) + { + test &= false; + } + VERIFY( test ); + } } int main() { test01(); - + test02(); return 0; }