]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ssl_sock: avoid iterating realloc(+1) on stored context
authorWilly Tarreau <w@1wt.eu>
Mon, 21 Aug 2023 06:12:12 +0000 (08:12 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 30 Aug 2023 09:43:06 +0000 (11:43 +0200)
The SSL context storage in servers is per-thread, and the contents are
allocated for a length that is determined from the session. It turns out
that placing some traces there revealed that the realloc() that is called
to grow the area can be called multiple times in a row even for just
health checks, to grow the area by just one or two bytes. Given that
malloc() allocates in multiples of 8 or 16 anyway, let's round the
allocated size up to the nearest multiple of 8 to avoid this unneeded
operation.

src/ssl_sock.c

index 9cc8166a595ea546d0e36cfe6a08c553165ef03a..5c4f584ba18c1fdf75fe231ab9affd1ee0bedd11 100644 (file)
@@ -4279,6 +4279,7 @@ static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
                if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
                        ptr = s->ssl_ctx.reused_sess[tid].ptr;
                } else {
+                       len = (len + 7) & -8; /* round to the nearest 8 bytes */
                        ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len);
                        if (!ptr)
                                free(s->ssl_ctx.reused_sess[tid].ptr);