re PR libstdc++/8230 (Buggy allocator behaviour)

2002-11-13  Benjamin Kosnik  <bkoz@redhat.com>

	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<bool>::reserve): Same.
	* testsuite/23_containers/vector_capacity.cc (test02): Add.

From-SVN: r59090
This commit is contained in:
Benjamin Kosnik 2002-11-13 22:15:17 +00:00
parent 1a083c0e90
commit 48d1c3c568
4 changed files with 73 additions and 21 deletions

View File

@ -1,10 +1,18 @@
2002-11-13 Benjamin Kosnik <bkoz@redhat.com>
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<bool>::reserve): Same.
* testsuite/23_containers/vector_capacity.cc (test02): Add.
2002-11-13 Benjamin Kosnik <bkoz@redhat.com> 2002-11-13 Benjamin Kosnik <bkoz@redhat.com>
* config/linker-map.gnu: Export all _S_construct. * config/linker-map.gnu: Export all _S_construct.
2002-11-13 Loren J. Rittle <ljrittle@acm.org> 2002-11-13 Loren J. Rittle <ljrittle@acm.org>
libstdc++/7445 PR libstdc++/7445
* src/locale.cc (std::locale::classic()): Weaken locking protocol. * src/locale.cc (std::locale::classic()): Weaken locking protocol.
2002-11-13 Jonathan Wakely <redi@gcc.gnu.org> 2002-11-13 Jonathan Wakely <redi@gcc.gnu.org>

View File

@ -604,7 +604,9 @@ template <typename _Alloc>
} }
void reserve(size_type __n) { 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); _Bit_type * __q = _M_bit_alloc(__n);
_M_finish = copy(begin(), end(), iterator(__q, 0)); _M_finish = copy(begin(), end(), iterator(__q, 0));
_M_deallocate(); _M_deallocate();

View File

@ -68,16 +68,18 @@ namespace std
vector<_Tp,_Alloc>:: vector<_Tp,_Alloc>::
reserve(size_type __n) reserve(size_type __n)
{ {
if (capacity() < __n) if (__n > this->max_size())
{ __throw_length_error("vector::reserve");
const size_type __old_size = size(); if (this->capacity() < __n)
pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish); {
_Destroy(_M_start, _M_finish); const size_type __old_size = size();
_M_deallocate(_M_start, _M_end_of_storage - _M_start); pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish);
_M_start = __tmp; _Destroy(_M_start, _M_finish);
_M_finish = __tmp + __old_size; _M_deallocate(_M_start, _M_end_of_storage - _M_start);
_M_end_of_storage = _M_start + __n; _M_start = __tmp;
} _M_finish = __tmp + __old_size;
_M_end_of_storage = _M_start + __n;
}
} }
template <typename _Tp, typename _Alloc> template <typename _Tp, typename _Alloc>

View File

@ -1,7 +1,7 @@
// 1999-05-07 // 1999-05-07
// bkoz // 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 // 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 // software; you can redistribute it and/or modify it under the
@ -22,6 +22,7 @@
// 23.2.4.2 vector capacity // 23.2.4.2 vector capacity
#include <vector> #include <vector>
#include <stdexcept>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
template<typename T> template<typename T>
@ -29,9 +30,8 @@ template<typename T>
struct B { }; struct B { };
bool test01() void test01()
{ {
// non POD types // non POD types
bool test = true; bool test = true;
std::vector< A<B> > vec01; std::vector< A<B> > vec01;
@ -51,17 +51,57 @@ bool test01()
vec01.resize(sz01); vec01.resize(sz01);
sz02 = vec01.size(); sz02 = vec01.size();
VERIFY( sz01 == sz02 ); VERIFY( sz01 == sz02 );
}
#ifdef DEBUG_ASSERT // libstdc++/8230
assert(test); void test02()
#endif {
bool test = true;
return test; {
std::vector<int> 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<int> 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() int main()
{ {
test01(); test01();
test02();
return 0; return 0;
} }