diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2019-06-16 22:57:10 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2019-06-17 12:40:01 +0300 |
commit | 3cb0b6cf17e2cc6cd62133a0beffa998b1e53c23 (patch) | |
tree | f56de0af3bc45651cf603b3b2021efb563ba5bd6 /src/microhttpd | |
parent | 03df91fc4cb9120959945cb39a0576acdaee1a3b (diff) | |
download | libmicrohttpd-3cb0b6cf17e2cc6cd62133a0beffa998b1e53c23.tar.gz libmicrohttpd-3cb0b6cf17e2cc6cd62133a0beffa998b1e53c23.zip |
MHD_pool_reallocate(): never allocate additional buffer when shrinking
Diffstat (limited to 'src/microhttpd')
-rw-r--r-- | src/microhttpd/memorypool.c | 13 |
1 files 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, | |||
321 | if (0 != old_size) | 321 | if (0 != old_size) |
322 | { /* Need to save some data */ | 322 | { /* Need to save some data */ |
323 | const size_t old_offset = (uint8_t*)old - pool->memory; | 323 | const size_t old_offset = (uint8_t*)old - pool->memory; |
324 | const bool shrinking = (old_size > new_size); | ||
324 | /* Try resizing in-place */ | 325 | /* Try resizing in-place */ |
326 | if (shrinking) | ||
327 | { /* Shrinking in-place, zero-out freed part */ | ||
328 | memset ((uint8_t*)old + new_size, 0, old_size - new_size); | ||
329 | } | ||
325 | if (pool->pos == ROUND_TO_ALIGN (old_offset + old_size)) | 330 | if (pool->pos == ROUND_TO_ALIGN (old_offset + old_size)) |
326 | { /* "old" block is the last allocated block */ | 331 | { /* "old" block is the last allocated block */ |
327 | const size_t new_apos = ROUND_TO_ALIGN (old_offset + new_size); | 332 | const size_t new_apos = ROUND_TO_ALIGN (old_offset + new_size); |
328 | if (old_size > new_size) | 333 | if (!shrinking) |
329 | { /* Shrinking in-place, zero-out freed part */ | ||
330 | memset ((uint8_t*)old + new_size, 0, old_size - new_size); | ||
331 | } | ||
332 | else | ||
333 | { /* Grow in-place, check for enough space. */ | 334 | { /* Grow in-place, check for enough space. */ |
334 | if ( (new_apos > pool->end) || | 335 | if ( (new_apos > pool->end) || |
335 | (new_apos < pool->pos) ) /* Value wrap */ | 336 | (new_apos < pool->pos) ) /* Value wrap */ |
@@ -339,6 +340,8 @@ MHD_pool_reallocate (struct MemoryPool *pool, | |||
339 | pool->pos = new_apos; | 340 | pool->pos = new_apos; |
340 | return old; | 341 | return old; |
341 | } | 342 | } |
343 | if (shrinking) | ||
344 | return old; /* Resized in-place, freed part remains allocated */ | ||
342 | } | 345 | } |
343 | /* Need to allocate new block */ | 346 | /* Need to allocate new block */ |
344 | asize = ROUND_TO_ALIGN (new_size); | 347 | asize = ROUND_TO_ALIGN (new_size); |