[Base] Add sanity checks in StringBuffer.
This commit is contained in:
parent
60681eeb23
commit
96067fb293
|
@ -12,13 +12,15 @@
|
|||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
|
||||
#include "xenia/base/assert.h"
|
||||
#include "xenia/base/math.h"
|
||||
|
||||
namespace xe {
|
||||
|
||||
StringBuffer::StringBuffer(size_t initial_capacity) {
|
||||
buffer_capacity_ = std::max(initial_capacity, static_cast<size_t>(16 * 1024));
|
||||
buffer_ = reinterpret_cast<char*>(malloc(buffer_capacity_));
|
||||
buffer_ = reinterpret_cast<char*>(std::malloc(buffer_capacity_));
|
||||
assert_not_null(buffer_);
|
||||
buffer_[0] = 0;
|
||||
}
|
||||
|
||||
|
@ -40,7 +42,9 @@ void StringBuffer::Grow(size_t additional_length) {
|
|||
size_t new_capacity =
|
||||
std::max(xe::round_up(buffer_offset_ + additional_length, 16 * 1024),
|
||||
old_capacity * 2);
|
||||
buffer_ = reinterpret_cast<char*>(realloc(buffer_, new_capacity));
|
||||
auto new_buffer = std::realloc(buffer_, new_capacity);
|
||||
assert_not_null(new_buffer);
|
||||
buffer_ = reinterpret_cast<char*>(new_buffer);
|
||||
buffer_capacity_ = new_capacity;
|
||||
}
|
||||
|
||||
|
@ -57,7 +61,11 @@ void StringBuffer::Append(const std::string_view value) {
|
|||
}
|
||||
|
||||
void StringBuffer::AppendVarargs(const char* format, va_list args) {
|
||||
int length = vsnprintf(nullptr, 0, format, args);
|
||||
int result = vsnprintf(nullptr, 0, format, args);
|
||||
if (result <= 0) {
|
||||
return;
|
||||
}
|
||||
auto length = static_cast<size_t>(result);
|
||||
Grow(length + 1);
|
||||
vsnprintf(buffer_ + buffer_offset_, buffer_capacity_, format, args);
|
||||
buffer_offset_ += length;
|
||||
|
|
Loading…
Reference in New Issue