From 3cb0b6cf17e2cc6cd62133a0beffa998b1e53c23 Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Sun, 16 Jun 2019 22:57:10 +0300 Subject: [PATCH] MHD_pool_reallocate(): never allocate additional buffer when shrinking --- src/microhttpd/memorypool.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c index 27c949fc..6f74db83 100644 --- a/src/microhttpd/memorypool.c +++ b/src/microhttpd/memorypool.c @@ -321,15 +321,16 @@ MHD_pool_reallocate (struct MemoryPool *pool, if (0 != old_size) { /* Need to save some data */ const size_t old_offset = (uint8_t*)old - pool->memory; + const bool shrinking = (old_size > new_size); /* Try resizing in-place */ + if (shrinking) + { /* Shrinking in-place, zero-out freed part */ + memset ((uint8_t*)old + new_size, 0, old_size - new_size); + } if (pool->pos == ROUND_TO_ALIGN (old_offset + old_size)) { /* "old" block is the last allocated block */ const size_t new_apos = ROUND_TO_ALIGN (old_offset + new_size); - if (old_size > new_size) - { /* Shrinking in-place, zero-out freed part */ - memset ((uint8_t*)old + new_size, 0, old_size - new_size); - } - else + if (!shrinking) { /* Grow in-place, check for enough space. */ if ( (new_apos > pool->end) || (new_apos < pool->pos) ) /* Value wrap */ @@ -339,6 +340,8 @@ MHD_pool_reallocate (struct MemoryPool *pool, pool->pos = new_apos; return old; } + if (shrinking) + return old; /* Resized in-place, freed part remains allocated */ } /* Need to allocate new block */ asize = ROUND_TO_ALIGN (new_size);