This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
libstdc++/8230
- From: Benjamin Kosnik <bkoz at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 13 Nov 2002 16:15:09 -0600
- Subject: libstdc++/8230
tested x86/linux
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.
Index: include/bits/stl_bvector.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_bvector.h,v
retrieving revision 1.17
diff -c -p -r1.17 stl_bvector.h
*** include/bits/stl_bvector.h 10 Sep 2002 23:19:10 -0000 1.17
--- include/bits/stl_bvector.h 13 Nov 2002 22:13:30 -0000
*************** template <typename _Alloc>
*** 604,610 ****
}
void reserve(size_type __n) {
! if (capacity() < __n) {
_Bit_type * __q = _M_bit_alloc(__n);
_M_finish = copy(begin(), end(), iterator(__q, 0));
_M_deallocate();
--- 604,612 ----
}
void reserve(size_type __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();
Index: include/bits/vector.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/vector.tcc,v
retrieving revision 1.2
diff -c -p -r1.2 vector.tcc
*** include/bits/vector.tcc 9 Aug 2002 16:51:15 -0000 1.2
--- include/bits/vector.tcc 13 Nov 2002 22:13:32 -0000
*************** namespace std
*** 68,83 ****
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;
! }
}
template <typename _Tp, typename _Alloc>
--- 68,85 ----
vector<_Tp,_Alloc>::
reserve(size_type __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 <typename _Tp, typename _Alloc>
Index: testsuite/23_containers/vector_capacity.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/23_containers/vector_capacity.cc,v
retrieving revision 1.3
diff -c -p -r1.3 vector_capacity.cc
*** testsuite/23_containers/vector_capacity.cc 7 Aug 2001 03:38:30 -0000 1.3
--- testsuite/23_containers/vector_capacity.cc 13 Nov 2002 22:13:32 -0000
***************
*** 1,7 ****
// 1999-05-07
// bkoz
! // Copyright (C) 1999 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
--- 1,7 ----
// 1999-05-07
// bkoz
! // 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,27 ****
--- 22,28 ----
// 23.2.4.2 vector capacity
#include <vector>
+ #include <stdexcept>
#include <testsuite_hooks.h>
template<typename T>
*************** template<typename T>
*** 29,37 ****
struct B { };
! bool test01()
{
-
// non POD types
bool test = true;
std::vector< A<B> > vec01;
--- 30,37 ----
struct B { };
! void test01()
{
// non POD types
bool test = true;
std::vector< A<B> > vec01;
*************** bool test01()
*** 51,67 ****
vec01.resize(sz01);
sz02 = vec01.size();
VERIFY( sz01 == sz02 );
! #ifdef DEBUG_ASSERT
! assert(test);
! #endif
!
! return test;
}
int main()
{
test01();
!
return 0;
}
--- 51,107 ----
vec01.resize(sz01);
sz02 = vec01.size();
VERIFY( sz01 == sz02 );
+ }
! // libstdc++/8230
! void test02()
! {
! bool test = true;
!
! {
! 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()
{
test01();
! test02();
return 0;
}