diff options
author | Ondrej Zajicek <santiago@crfreenet.org> | 2023-05-31 18:32:53 +0200 |
---|---|---|
committer | Ondrej Zajicek <santiago@crfreenet.org> | 2023-05-31 18:42:14 +0200 |
commit | 0799fc99abb523432bc3f903f6a32eafbe37d043 (patch) | |
tree | e16fa09c12e30c8bb9f8dac79c3f6ae20f019a49 | |
parent | e8be7a7080be2ffd800ead5377b06c7a535b564a (diff) |
BMP: Fix bug in buffer resize
The buffer code in bmp_buffer_grow(), reuse the MRT buffer handling code.
Based on comments by Michal Zagorski <mzagorsk@akamai.com>, Thanks!
-rw-r--r-- | proto/bmp/buffer.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/proto/bmp/buffer.c b/proto/bmp/buffer.c index 9e62e468..be9dd698 100644 --- a/proto/bmp/buffer.c +++ b/proto/bmp/buffer.c @@ -25,22 +25,32 @@ bmp_buffer_free(buffer *buf) buf->start = buf->pos = buf->end = NULL; } +/** + * @brief bmp_buffer_grow + * @param buf - buffer to grow + * @param n - required amount of available space + * Resize buffer in a way that there is at least @n bytes of available space. + */ static void bmp_buffer_grow(buffer *buf, const size_t n) { - const size_t pos = bmp_buffer_pos(buf); - buf->start = mb_realloc(buf->start, n); + size_t pos = bmp_buffer_pos(buf); + size_t size = bmp_buffer_size(buf); + size_t req = pos + n; + + while (size < req) + size = size * 3 / 2; + + buf->start = mb_realloc(buf->start, size); buf->pos = buf->start + pos; - buf->end = buf->start + n; + buf->end = buf->start + size; } void bmp_buffer_need(buffer *buf, const size_t n) { if (bmp_buffer_avail(buf) < n) - { bmp_buffer_grow(buf, n); - } } void |