[dart/vm]: fix bug in TextBuffer capacity

Rationale:
Ensuring capacity computation was off by one (due to trailing '\0')
if the initial buffer size was zero. Granted, this can only happen
after a Steal(), but since API did not exclude reuse after this
operation, it is better to just handle this than crash.

https://github.com/dart-lang/sdk/issues/36818

Change-Id: I4e404c4361da03e24364280fb01a308fbf7ace6c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/100991
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Aart Bik <ajcbik@google.com>
This commit is contained in:
Aart Bik 2019-05-01 22:26:41 +00:00 committed by commit-bot@chromium.org
parent 1ea3e418b5
commit aceefaf846

View file

@ -135,7 +135,7 @@ void TextBuffer::AddEscapedString(const char* s) {
void TextBuffer::EnsureCapacity(intptr_t len) { void TextBuffer::EnsureCapacity(intptr_t len) {
intptr_t remaining = buf_size_ - msg_len_; intptr_t remaining = buf_size_ - msg_len_;
if (remaining <= len) { if (remaining <= len) {
intptr_t new_size = buf_size_ + Utils::Maximum(buf_size_, len); intptr_t new_size = buf_size_ + Utils::Maximum(buf_size_, len + 1);
char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size));
if (new_buf == NULL) { if (new_buf == NULL) {
OUT_OF_MEMORY(); OUT_OF_MEMORY();