Fix an off-by-one error in basic_string::__grow_by, where it would incorrectly throw length_error (instead of bad_alloc) when attempting to resize the string to 'max_size()'. Add tests for resizing to max_size +/-1
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@194151 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -2181,7 +2181,7 @@ basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_t
|
|||||||
size_type __n_copy, size_type __n_del, size_type __n_add)
|
size_type __n_copy, size_type __n_del, size_type __n_add)
|
||||||
{
|
{
|
||||||
size_type __ms = max_size();
|
size_type __ms = max_size();
|
||||||
if (__delta_cap > __ms - __old_cap - 1)
|
if (__delta_cap > __ms - __old_cap)
|
||||||
this->__throw_length_error();
|
this->__throw_length_error();
|
||||||
pointer __old_p = __get_pointer();
|
pointer __old_p = __get_pointer();
|
||||||
size_type __cap = __old_cap < __ms / 2 - __alignment ?
|
size_type __cap = __old_cap < __ms / 2 - __alignment ?
|
||||||
|
@@ -16,17 +16,47 @@
|
|||||||
|
|
||||||
#include "../min_allocator.h"
|
#include "../min_allocator.h"
|
||||||
|
|
||||||
|
template <class S>
|
||||||
|
void
|
||||||
|
test1(const S& s)
|
||||||
|
{
|
||||||
|
S s2(s);
|
||||||
|
const size_t sz = s2.max_size() - 1;
|
||||||
|
try { s2.resize(sz, 'x'); }
|
||||||
|
catch ( const std::bad_alloc & ) { return ; }
|
||||||
|
assert ( s2.size() == sz );
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class S>
|
||||||
|
void
|
||||||
|
test2(const S& s)
|
||||||
|
{
|
||||||
|
S s2(s);
|
||||||
|
const size_t sz = s2.max_size();
|
||||||
|
try { s2.resize(sz, 'x'); }
|
||||||
|
catch ( const std::bad_alloc & ) { return ; }
|
||||||
|
assert ( s.size() == sz );
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class S>
|
||||||
|
void
|
||||||
|
test3(const S& s)
|
||||||
|
{
|
||||||
|
S s2(s);
|
||||||
|
const size_t sz = s2.max_size() + 1;
|
||||||
|
try { s2.resize(sz, 'x'); }
|
||||||
|
catch ( const std::length_error & ) { return ; }
|
||||||
|
assert ( false );
|
||||||
|
}
|
||||||
|
|
||||||
template <class S>
|
template <class S>
|
||||||
void
|
void
|
||||||
test(const S& s)
|
test(const S& s)
|
||||||
{
|
{
|
||||||
assert(s.max_size() >= s.size());
|
assert(s.max_size() >= s.size());
|
||||||
{
|
test1(s);
|
||||||
S s2;
|
test2(s);
|
||||||
try { s2.resize(s2.max_size() - 1, 'x'); }
|
test3(s);
|
||||||
catch ( const std::bad_alloc & ) { return ; }
|
|
||||||
assert ( false );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
Reference in New Issue
Block a user