From 8b17c9155fdac05273867ef5aab10d08b482bbee Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Mon, 19 Jun 2023 18:25:05 +0300 Subject: [PATCH] try_grow_read_buffer(): better handling of edge cases Used application specified grow size instead of MHD default size. When free space is becoming low first try to increase free space to exact increment value before switching to "low memory" mode. --- src/microhttpd/connection.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index db309bb8..d6c4ac1e 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -1768,6 +1768,7 @@ try_grow_read_buffer (struct MHD_Connection *connection, { size_t new_size; size_t avail_size; + const size_t def_grow_size = connection->daemon->pool_increment; void *rb; avail_size = MHD_pool_get_free (connection->pool); @@ -1780,14 +1781,23 @@ try_grow_read_buffer (struct MHD_Connection *connection, size_t grow_size; grow_size = avail_size / 8; - if (MHD_BUF_INC_SIZE > grow_size) + if (def_grow_size > grow_size) { /* Shortage of space */ - if (! required) - return false; /* Grow is not mandatory, leave some space in pool */ + const size_t left_free = + connection->read_buffer_size - connection->read_buffer_offset; + mhd_assert (connection->read_buffer_size >= \ + connection->read_buffer_offset); + if ((def_grow_size <= grow_size + left_free) + && (left_free < def_grow_size)) + grow_size = def_grow_size - left_free; /* Use precise 'def_grow_size' for new free space */ + else if (! required) + return false; /* Grow is not mandatory, leave some space in pool */ else { /* Shortage of space, but grow is mandatory */ - static const size_t small_inc = MHD_BUF_INC_SIZE / 8; + const size_t small_inc = + ((MHD_BUF_INC_SIZE > def_grow_size) ? + def_grow_size : MHD_BUF_INC_SIZE) / 8; if (small_inc < avail_size) grow_size = small_inc; else @@ -1821,6 +1831,7 @@ try_grow_read_buffer (struct MHD_Connection *connection, mhd_assert (0); return false; } + mhd_assert (connection->read_buffer == rb); connection->read_buffer = rb; mhd_assert (NULL != connection->read_buffer); connection->read_buffer_size = new_size;