From: Richard Russon Date: Fri, 24 May 2019 22:24:04 +0000 (+0100) Subject: test: improve test_mutt_buffer_increase_size() X-Git-Tag: 2019-10-25~188^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6cfb0641dc9384ccb92e967f4d88f6269a98407e;p=neomutt test: improve test_mutt_buffer_increase_size() --- diff --git a/mutt/buffer.c b/mutt/buffer.c index 5b0fd4ecc..7c50471c4 100644 --- a/mutt/buffer.c +++ b/mutt/buffer.c @@ -347,10 +347,14 @@ void mutt_buffer_increase_size(struct Buffer *buf, size_t new_size) if (!buf) return; + if (!buf->dptr) + buf->dptr = buf->data; + if (new_size <= buf->dsize) return; - size_t offset = buf->dptr - buf->data; + size_t offset = (buf->dptr && buf->data) ? buf->dptr - buf->data : 0; + buf->dsize = new_size; mutt_mem_realloc(&buf->data, buf->dsize); buf->dptr = buf->data + offset; diff --git a/test/buffer/mutt_buffer_increase_size.c b/test/buffer/mutt_buffer_increase_size.c index 561b16610..8bea75381 100644 --- a/test/buffer/mutt_buffer_increase_size.c +++ b/test/buffer/mutt_buffer_increase_size.c @@ -33,4 +33,28 @@ void test_mutt_buffer_increase_size(void) mutt_buffer_increase_size(NULL, 10); TEST_CHECK_(1, "mutt_buffer_increase_size(NULL, 10)"); } + + { + struct Buffer *buf = mutt_buffer_new(); + mutt_buffer_increase_size(buf, 10); + TEST_CHECK_(1, "mutt_buffer_increase_size(buf, 10)"); + mutt_buffer_free(&buf); + } + + { + const int orig_size = 64; + static int sizes[] = { 0, 32, 64, 128 }; + + for (size_t i = 0; i < mutt_array_size(sizes); i++) + { + struct Buffer *buf = mutt_buffer_alloc(orig_size); + TEST_CASE_("%d", sizes[i]); + mutt_buffer_increase_size(buf, sizes[i]); + TEST_CHECK(buf->dsize == MAX(orig_size, sizes[i])); + mutt_buffer_free(&buf); + } + + } + + }