]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: buffer_create_dynamic_max() - Fix max_size handling
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 11 Feb 2021 00:44:34 +0000 (02:44 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 21 Sep 2021 07:12:53 +0000 (07:12 +0000)
Never allocate buffer larger than its max_size, since it's just wasted
memory. Also clarify that the allocation can actually go up to max_size+1
because of str_c() NUL byte reservation.

src/lib/buffer.c
src/lib/buffer.h

index b08cd27935aab74556775046719e4eff7b65af07..d1d505a525b93ad5faead14bc13614277982912e 100644 (file)
@@ -74,8 +74,15 @@ buffer_check_limits(struct real_buffer *buf, size_t pos, size_t data_size)
                                pool_get_name(buf->pool));
                }
 
-               buffer_alloc(buf, pool_get_exp_grown_size(buf->pool, buf->alloc,
-                                                         new_size + 1));
+               size_t new_alloc_size =
+                       pool_get_exp_grown_size(buf->pool, buf->alloc,
+                                               new_size + 1);
+               if (new_alloc_size > buf->max_size) {
+                       /* limit to max_size, but do include +1 for
+                          str_c() NUL */
+                       new_alloc_size = buf->max_size + 1;
+               }
+               buffer_alloc(buf, new_alloc_size);
        }
 #if 0
        else if (new_size > buf->used && buf->alloced &&
index d915346a204594f67fec086b4f35791e30f2e9b7..902ec88b6a68a673920810dce23a620d645f6d3e 100644 (file)
@@ -35,7 +35,8 @@ void buffer_create_from_const_data(buffer_t *buffer,
    current size it's grown. */
 buffer_t *buffer_create_dynamic(pool_t pool, size_t init_size);
 /* Create a dynamically growing buffer with a maximum size. Writes past the
-   maximum size will i_panic(). */
+   maximum size will i_panic(). Internally allow it to grow max_size+1 so
+   str_c() NUL can be used. */
 buffer_t *buffer_create_dynamic_max(pool_t pool, size_t init_size,
                                    size_t max_size);