Skip to content

Commit

Permalink
Allocator::max_size support in basic_memory_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Oct 27, 2020
1 parent 4fe0b11 commit 50a5e4f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,14 @@ void basic_memory_buffer<T, SIZE, Allocator>::grow(size_t size) {
#ifdef FMT_FUZZ
if (size > 5000) throw std::runtime_error("fuzz mode - won't grow that much");
#endif
const size_t max_size = std::allocator_traits<Allocator>::max_size(alloc_);
if (size > max_size) {
throw std::length_error("fmt::basic_memory_buffer::grow");
}
size_t old_capacity = this->capacity();
size_t new_capacity = old_capacity + old_capacity / 2;
if (size > new_capacity) new_capacity = size;
if (new_capacity > max_size) new_capacity = max_size;
if (size > new_capacity) new_capacity = size; // size is never greater max_size.
T* old_data = this->data();
T* new_data =
std::allocator_traits<Allocator>::allocate(alloc_, new_capacity);
Expand Down

0 comments on commit 50a5e4f

Please sign in to comment.