From ade10463917e5296b17d839bae7fb3e077313dc2 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Tue, 30 May 2023 08:40:56 +0000 Subject: [PATCH] stream: Allocate enough for tls upgrade --- include/stream.h | 4 ++-- src/stream-gnutls.c | 3 +++ src/stream-tcp.c | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/stream.h b/include/stream.h index 8eaf8bf..68c7e71 100644 --- a/include/stream.h +++ b/include/stream.h @@ -23,6 +23,8 @@ #include #include +#define STREAM_ALLOC_SIZE 4096 + enum stream_state { STREAM_STATE_NORMAL = 0, STREAM_STATE_CLOSED, @@ -68,8 +70,6 @@ struct stream_impl { void (*exec_and_send)(struct stream*, stream_exec_fn, void* userdata); }; -// TODO: Move some of these struct members into their respective implementation -// classes. struct stream { struct stream_impl *impl; diff --git a/src/stream-gnutls.c b/src/stream-gnutls.c index 416233d..2b7afb1 100644 --- a/src/stream-gnutls.c +++ b/src/stream-gnutls.c @@ -40,6 +40,9 @@ struct stream_gnutls { gnutls_session_t session; }; +static_assert(sizeof(struct stream_gnutls) <= STREAM_ALLOC_SIZE, + "struct stream_gnutls has grown too large, increase STREAM_ALLOC_SIZE"); + static int stream__try_tls_accept(struct stream* self); static int stream_gnutls_close(struct stream* base) diff --git a/src/stream-tcp.c b/src/stream-tcp.c index 03a3d9b..eb94c1d 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -32,6 +32,9 @@ #include "stream-common.h" #include "sys/queue.h" +static_assert(sizeof(struct stream) <= STREAM_ALLOC_SIZE, + "struct stream has grown too large, increase STREAM_ALLOC_SIZE"); + static int stream_tcp_close(struct stream* self) { if (self->state == STREAM_STATE_CLOSED) @@ -255,7 +258,7 @@ static struct stream_impl impl = { struct stream* stream_new(int fd, stream_event_fn on_event, void* userdata) { - struct stream* self = calloc(1, sizeof(*self)); + struct stream* self = calloc(1, STREAM_ALLOC_SIZE); if (!self) return NULL;