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

2002-11-15  Benjamin Kosnik  <bkoz@redhat.com>
            Gabriel Dos Reis  <gdr@integrable-solutions.net>

	PR libstdc++/8230
	* include/bits/stl_alloc.h: Use builtin_expect for the most
	obvious limit checks.
	(__default_alloc_template::allocate): Check for null, throw
	bad_alloc.
	* include/bits/vector.tcc: Formatting tweaks.
	* include/bits/stl_vector.h: Same.
	* testsuite/20_util/allocator_members.cc (test02): Add.
	* testsuite/23_containers/vector_capacity.cc (test03): Add.

Co-Authored-By: Gabriel Dos Reis <gdr@integrable-solutions.net>

From-SVN: r59169
This commit is contained in:
Benjamin Kosnik 2002-11-16 17:16:31 +00:00 committed by Benjamin Kosnik
parent 5dab517fa0
commit af5fb6ab3b
6 changed files with 983 additions and 879 deletions

View File

@ -1,3 +1,16 @@
2002-11-15 Benjamin Kosnik <bkoz@redhat.com>
Gabriel Dos Reis <gdr@integrable-solutions.net>
PR libstdc++/8230
* include/bits/stl_alloc.h: Use builtin_expect for the most
obvious limit checks.
(__default_alloc_template::allocate): Check for null, throw
bad_alloc.
* include/bits/vector.tcc: Formatting tweaks.
* include/bits/stl_vector.h: Same.
* testsuite/20_util/allocator_members.cc (test02): Add.
* testsuite/23_containers/vector_capacity.cc (test03): Add.
2002-11-15 Rainer Orth <ro@TechFak.Uni-Bielefeld.DE>
* src/ios.cc [_GLIBCPP_HAVE_UNISTD_H]: Include unistd.h.

View File

@ -139,7 +139,8 @@ namespace std
allocate(size_t __n)
{
void* __result = malloc(__n);
if (0 == __result) __result = _S_oom_malloc(__n);
if (__builtin_expect(__result == 0, 0))
__result = _S_oom_malloc(__n);
return __result;
}
@ -152,7 +153,7 @@ namespace std
reallocate(void* __p, size_t /* old_sz */, size_t __new_sz)
{
void* __result = realloc(__p, __new_sz);
if (0 == __result)
if (__builtin_expect(__result == 0, 0))
__result = _S_oom_realloc(__p, __new_sz);
return __result;
}
@ -181,8 +182,8 @@ namespace std
for (;;)
{
__my_malloc_handler = __malloc_alloc_oom_handler;
if (0 == __my_malloc_handler)
std::__throw_bad_alloc();
if (__builtin_expect(__my_malloc_handler == 0, 0))
__throw_bad_alloc();
(*__my_malloc_handler)();
__result = malloc(__n);
if (__result)
@ -202,8 +203,8 @@ namespace std
for (;;)
{
__my_malloc_handler = __malloc_alloc_oom_handler;
if (0 == __my_malloc_handler)
std::__throw_bad_alloc();
if (__builtin_expect(__my_malloc_handler == 0, 0))
__throw_bad_alloc();
(*__my_malloc_handler)();
__result = realloc(__p, __n);
if (__result)
@ -232,7 +233,12 @@ namespace std
public:
static _Tp*
allocate(size_t __n)
{ return 0 == __n ? 0 : (_Tp*) _Alloc::allocate(__n * sizeof (_Tp)); }
{
_Tp* __ret = 0;
if (__n)
__ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
return __ret;
}
static _Tp*
allocate()
@ -293,9 +299,9 @@ namespace std
{
char* __real_p = (char*)__p - (int) _S_extra;
assert(*(size_t*)__real_p == __old_sz);
char* __result = (char*)
_Alloc::reallocate(__real_p, __old_sz + (int) _S_extra,
__new_sz + (int) _S_extra);
char* __result = (char*) _Alloc::reallocate(__real_p,
__old_sz + (int) _S_extra,
__new_sz + (int) _S_extra);
*(size_t*)__result = __new_sz;
return __result + (int) _S_extra;
}
@ -362,7 +368,7 @@ namespace std
static size_t
_S_freelist_index(size_t __bytes)
{ return (((__bytes) + (size_t)_ALIGN-1)/(size_t)_ALIGN - 1); }
{ return (((__bytes) + (size_t)_ALIGN - 1)/(size_t)_ALIGN - 1); }
// Returns an object of size __n, and optionally adds to size __n
// free list.
@ -402,7 +408,7 @@ namespace std
else
__atomic_add(&_S_force_new, -1);
// Trust but verify...
assert (_S_force_new != 0);
assert(_S_force_new != 0);
}
if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0))
@ -416,13 +422,15 @@ namespace std
// unwinding.
_Lock __lock_instance;
_Obj* __restrict__ __result = *__my_free_list;
if (__result == 0)
if (__builtin_expect(__result == 0, 0))
__ret = _S_refill(_S_round_up(__n));
else
{
*__my_free_list = __result -> _M_free_list_link;
__ret = __result;
}
}
if (__builtin_expect(__ret == 0, 0))
__throw_bad_alloc();
}
return __ret;
}
@ -510,7 +518,7 @@ namespace std
*__my_free_list = (_Obj*)_S_start_free;
}
_S_start_free = (char*) __new_alloc::allocate(__bytes_to_get);
if (0 == _S_start_free)
if (_S_start_free == 0)
{
size_t __i;
_Obj* volatile* __my_free_list;
@ -523,7 +531,7 @@ namespace std
{
__my_free_list = _S_free_list + _S_freelist_index(__i);
__p = *__my_free_list;
if (0 != __p)
if (__p != 0)
{
*__my_free_list = __p -> _M_free_list_link;
_S_start_free = (char*)__p;
@ -569,17 +577,17 @@ namespace std
*__my_free_list = __next_obj = (_Obj*)(__chunk + __n);
for (__i = 1; ; __i++)
{
__current_obj = __next_obj;
__current_obj = __next_obj;
__next_obj = (_Obj*)((char*)__next_obj + __n);
if (__nobjs - 1 == __i)
{
__current_obj -> _M_free_list_link = 0;
break;
}
else
__current_obj -> _M_free_list_link = __next_obj;
}
return(__result);
if (__nobjs - 1 == __i)
{
__current_obj -> _M_free_list_link = 0;
break;
}
else
__current_obj -> _M_free_list_link = __next_obj;
}
return __result;
}
@ -600,7 +608,7 @@ namespace std
__copy_sz = __new_sz > __old_sz? __old_sz : __new_sz;
memcpy(__result, __p, __copy_sz);
deallocate(__p, __old_sz);
return(__result);
return __result;
}
#endif
@ -669,13 +677,20 @@ namespace std
const_pointer
address(const_reference __x) const { return &__x; }
// __n is permitted to be 0. The C++ standard says nothing about what
// the return value is when __n == 0.
// NB: __n is permitted to be 0. The C++ standard says nothing
// about what the return value is when __n == 0.
_Tp*
allocate(size_type __n, const void* = 0)
{
return __n != 0
? static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))) : 0;
_Tp* __ret = 0;
if (__n)
{
if (__n <= this->max_size())
__ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
else
__throw_bad_alloc();
}
return __ret;
}
// __p is not permitted to be a null pointer.
@ -719,12 +734,13 @@ namespace std
/**
* @if maint
* Allocator adaptor to turn an "SGI" style allocator (e.g., __alloc,
* __malloc_alloc_template) into a "standard" conforming allocator. Note
* that this adaptor does *not* assume that all objects of the underlying
* alloc class are identical, nor does it assume that all of the underlying
* alloc's member functions are static member functions. Note, also, that
* __allocator<_Tp, __alloc> is essentially the same thing as allocator<_Tp>.
* Allocator adaptor to turn an "SGI" style allocator (e.g.,
* __alloc, __malloc_alloc_template) into a "standard" conforming
* allocator. Note that this adaptor does *not* assume that all
* objects of the underlying alloc class are identical, nor does it
* assume that all of the underlying alloc's member functions are
* static member functions. Note, also, that __allocator<_Tp,
* __alloc> is essentially the same thing as allocator<_Tp>.
* @endif
* (See @link Allocators allocators info @endlink for more.)
*/
@ -732,7 +748,7 @@ namespace std
struct __allocator
{
_Alloc __underlying_alloc;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
@ -761,29 +777,31 @@ namespace std
const_pointer
address(const_reference __x) const { return &__x; }
// __n is permitted to be 0.
_Tp*
allocate(size_type __n, const void* = 0)
{
return __n != 0
? static_cast<_Tp*>(__underlying_alloc.allocate(__n * sizeof(_Tp)))
: 0;
}
// NB: __n is permitted to be 0. The C++ standard says nothing
// about what the return value is when __n == 0.
_Tp*
allocate(size_type __n, const void* = 0)
{
_Tp* __ret = 0;
if (__n)
__ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
return __ret;
}
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type __n)
{ __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); }
size_type
max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
void
construct(pointer __p, const _Tp& __val) { new(__p) _Tp(__val); }
void
destroy(pointer __p) { __p->~_Tp(); }
};
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type __n)
{ __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); }
size_type
max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
void
construct(pointer __p, const _Tp& __val) { new(__p) _Tp(__val); }
void
destroy(pointer __p) { __p->~_Tp(); }
};
template<typename _Alloc>
struct __allocator<void, _Alloc>

File diff suppressed because it is too large Load Diff

View File

@ -63,7 +63,7 @@
namespace std
{
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
void
vector<_Tp,_Alloc>::
reserve(size_type __n)
@ -82,7 +82,7 @@ namespace std
}
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
typename vector<_Tp,_Alloc>::iterator
vector<_Tp,_Alloc>::
insert(iterator __position, const value_type& __x)
@ -98,7 +98,7 @@ namespace std
return begin() + __n;
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
typename vector<_Tp,_Alloc>::iterator
vector<_Tp,_Alloc>::
erase(iterator __position)
@ -110,7 +110,7 @@ namespace std
return __position;
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
typename vector<_Tp,_Alloc>::iterator
vector<_Tp,_Alloc>::
erase(iterator __first, iterator __last)
@ -121,7 +121,7 @@ namespace std
return __first;
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
vector<_Tp,_Alloc>&
vector<_Tp,_Alloc>::
operator=(const vector<_Tp,_Alloc>& __x)
@ -152,7 +152,7 @@ namespace std
return *this;
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
void
vector<_Tp,_Alloc>::
_M_fill_assign(size_t __n, const value_type& __val)
@ -171,7 +171,7 @@ namespace std
erase(fill_n(begin(), __n, __val), end());
}
template <typename _Tp, typename _Alloc> template <typename _InputIter>
template<typename _Tp, typename _Alloc> template<typename _InputIter>
void
vector<_Tp,_Alloc>::
_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag)
@ -185,7 +185,7 @@ namespace std
insert(end(), __first, __last);
}
template <typename _Tp, typename _Alloc> template <typename _ForwardIter>
template<typename _Tp, typename _Alloc> template<typename _ForwardIter>
void
vector<_Tp,_Alloc>::
_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
@ -216,7 +216,7 @@ namespace std
}
}
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
void
vector<_Tp,_Alloc>::
_M_insert_aux(iterator __position, const _Tp& __x)
@ -259,7 +259,7 @@ namespace std
}
#ifdef _GLIBCPP_DEPRECATED
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
void
vector<_Tp,_Alloc>::
_M_insert_aux(iterator __position)
@ -302,63 +302,64 @@ namespace std
}
#endif
template <typename _Tp, typename _Alloc>
template<typename _Tp, typename _Alloc>
void
vector<_Tp,_Alloc>::
_M_fill_insert(iterator __position, size_type __n, const value_type& __x)
{
if (__n != 0)
{
if (size_type(_M_end_of_storage - _M_finish) >= __n) {
value_type __x_copy = __x;
const size_type __elems_after = end() - __position;
iterator __old_finish(_M_finish);
if (__elems_after > __n)
{
uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
_M_finish += __n;
copy_backward(__position, __old_finish - __n, __old_finish);
fill(__position, __position + __n, __x_copy);
}
else
{
uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
_M_finish += __n - __elems_after;
uninitialized_copy(__position, __old_finish, _M_finish);
_M_finish += __elems_after;
fill(__position, __old_finish, __x_copy);
}
}
if (size_type(_M_end_of_storage - _M_finish) >= __n)
{
value_type __x_copy = __x;
const size_type __elems_after = end() - __position;
iterator __old_finish(_M_finish);
if (__elems_after > __n)
{
uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
_M_finish += __n;
copy_backward(__position, __old_finish - __n, __old_finish);
fill(__position, __position + __n, __x_copy);
}
else
{
uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
_M_finish += __n - __elems_after;
uninitialized_copy(__position, __old_finish, _M_finish);
_M_finish += __elems_after;
fill(__position, __old_finish, __x_copy);
}
}
else
{
const size_type __old_size = size();
const size_type __len = __old_size + max(__old_size, __n);
iterator __new_start(_M_allocate(__len));
iterator __new_finish(__new_start);
try
{
__new_finish = uninitialized_copy(begin(), __position,
__new_start);
__new_finish = uninitialized_fill_n(__new_finish, __n, __x);
__new_finish
= uninitialized_copy(__position, end(), __new_finish);
}
catch(...)
{
_Destroy(__new_start,__new_finish);
_M_deallocate(__new_start.base(),__len);
__throw_exception_again;
}
_Destroy(_M_start, _M_finish);
_M_deallocate(_M_start, _M_end_of_storage - _M_start);
_M_start = __new_start.base();
_M_finish = __new_finish.base();
_M_end_of_storage = __new_start.base() + __len;
}
{
const size_type __old_size = size();
const size_type __len = __old_size + max(__old_size, __n);
iterator __new_start(_M_allocate(__len));
iterator __new_finish(__new_start);
try
{
__new_finish = uninitialized_copy(begin(), __position,
__new_start);
__new_finish = uninitialized_fill_n(__new_finish, __n, __x);
__new_finish = uninitialized_copy(__position, end(),
__new_finish);
}
catch(...)
{
_Destroy(__new_start,__new_finish);
_M_deallocate(__new_start.base(),__len);
__throw_exception_again;
}
_Destroy(_M_start, _M_finish);
_M_deallocate(_M_start, _M_end_of_storage - _M_start);
_M_start = __new_start.base();
_M_finish = __new_finish.base();
_M_end_of_storage = __new_start.base() + __len;
}
}
}
template <typename _Tp, typename _Alloc> template <typename _InputIterator>
template<typename _Tp, typename _Alloc> template<typename _InputIterator>
void
vector<_Tp,_Alloc>::
_M_range_insert(iterator __pos,
@ -372,12 +373,11 @@ namespace std
}
}
template <typename _Tp, typename _Alloc> template <typename _ForwardIterator>
template<typename _Tp, typename _Alloc> template<typename _ForwardIterator>
void
vector<_Tp,_Alloc>::
_M_range_insert(iterator __position,
_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag)
_M_range_insert(iterator __position,_ForwardIterator __first,
_ForwardIterator __last, forward_iterator_tag)
{
if (__first != __last)
{

View File

@ -1,6 +1,6 @@
// 2001-06-14 Benjamin Kosnik <bkoz@redhat.com>
// Copyright (C) 2001 Free Software Foundation, Inc.
// Copyright (C) 2001, 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
@ -21,6 +21,7 @@
// 20.4.1.1 allocator members
#include <memory>
#include <stdexcept>
#include <cstdlib>
#include <testsuite_hooks.h>
@ -42,7 +43,7 @@ void operator delete(void *v) throw()
return std::free(v);
}
int main(void)
void test01()
{
bool test = true;
std::allocator<gnu> obj;
@ -55,6 +56,34 @@ int main(void)
obj.deallocate(pobj, 256);
VERIFY( check_delete );
}
// libstdc++/8230
void test02()
{
bool test = true;
try
{
std::allocator<int> alloc;
const std::allocator<int>::size_type n = alloc.max_size();
int* p = alloc.allocate(n + 1);
p[n] = 2002;
}
catch(const std::bad_alloc& e)
{
// Allowed.
test = true;
}
catch(...)
{
test = false;
}
VERIFY( test );
}
int main()
{
test01();
test02();
return 0;
}

View File

@ -99,9 +99,30 @@ void test02()
}
}
void test03()
{
bool test = true;
std::vector<int> v;
try
{
v.resize(v.max_size());
v[v.max_size() - 1] = 2002;
}
catch (const std::bad_alloc& error)
{
test = true;
}
catch (...)
{
test = false;
}
VERIFY( test );
}
int main()
{
test01();
test02();
test03();
return 0;
}