This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/34164] New: Inconsistent size used for allocate/deallocate for std::string
- From: "Aaron dot m dot camac at boeing dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 20 Nov 2007 15:15:34 -0000
- Subject: [Bug c++/34164] New: Inconsistent size used for allocate/deallocate for std::string
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Looking at 4.1.1
/data_storage/obj-4.1.1/i686-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc
It looks as though the size is incorrectly figured for the deallocate.
_M_destroy uses sizeof(_Rep_base) where _S_create uses sizeof(_Rep) to
determine the size of the allocation. Based on the header it looks like _Rep is
larger.
I can't say I've had an errror with this, but I was just dumpster diving the
code to figure out a purify error and came across this inconsitency. It doesn't
really do anything when you use the default allocator, since deallocate doesn't
use the size in the default.
template<typename _CharT, typename _Traits, typename _Alloc>
void
basic_string<_CharT, _Traits, _Alloc>::_Rep::
_M_destroy(const _Alloc& __a) throw ()
{
const size_type __size = sizeof(_Rep_base) +
(this->_M_capacity + 1) * sizeof(_CharT);
_Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
}
template<typename _CharT, typename _Traits, typename _Alloc>
typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
basic_string<_CharT, _Traits, _Alloc>::_Rep::
_S_create(size_type __capacity, size_type __old_capacity,
const _Alloc& __alloc)
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 83. String::npos vs. string::max_size()
if (__capacity > _S_max_size)
__throw_length_error(__N("basic_string::_S_create"));
const size_type __pagesize = 4096;
const size_type __malloc_header_size = 4 * sizeof(void*);
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
__capacity = 2 * __old_capacity;
size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
const size_type __adj_size = __size + __malloc_header_size;
if (__adj_size > __pagesize && __capacity > __old_capacity)
{
const size_type __extra = __pagesize - __adj_size % __pagesize;
__capacity += __extra / sizeof(_CharT);
// Never allocate a string bigger than _S_max_size.
if (__capacity > _S_max_size)
__capacity = _S_max_size;
__size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
}
// NB: Might throw, but no worries about a leak, mate: _Rep()
// does not throw.
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
_Rep *__p = new (__place) _Rep;
__p->_M_capacity = __capacity;
return __p;
}
--
Summary: Inconsistent size used for allocate/deallocate for
std::string
Product: gcc
Version: 4.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: Aaron dot m dot camac at boeing dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34164