From e1619707f63e043f526a1be29afa4c97377be274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves=20R=C5=B1tschl=C3=A9?= Date: Wed, 13 Aug 2025 12:24:15 +0200 Subject: [PATCH] check allocation success --- processes.c | 21 ++++++++++++++++++++- udp-listener.c | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/processes.c b/processes.c index a7e2b6d..22a6442 100644 --- a/processes.c +++ b/processes.c @@ -162,6 +162,24 @@ void remember_child_data(struct loop_info* fd_info, } } +static int max_forking_connections(void) +{ + int max_cnx = 0; + for (int i = 0; i < cfg.protocols_len; i++) { + if (cfg.protocols[i].fork) + max_cnx += cfg.protocols[i].max_connections; + } + return max_cnx; +} + +static int next_power_of_two(int n) +{ + n |= (n >> 16); + n |= (n >> 4); + n |= (n >> 2); + n |= (n >> 1); + return n + 1; +} void loop_init(struct loop_info* loop) { @@ -173,7 +191,8 @@ void loop_init(struct loop_info* loop) udp_init(loop); tcp_init(); - loop->pid2proto = hash_init(32, pid_make_key, pid_cmp); + int max_forks = next_power_of_two(max_forking_connections()); + loop->pid2proto = hash_init(2 * max_forks, pid_make_key, pid_cmp); CHECK_ALLOC(loop->pid2proto, "hash_init"); } diff --git a/udp-listener.c b/udp-listener.c index 1f30702..87930d9 100644 --- a/udp-listener.c +++ b/udp-listener.c @@ -124,6 +124,7 @@ static void udp_sanity_check(void) void udp_init(struct loop_info* fd_info) { fd_info->hash_sources = hash_init(cfg.udp_max_connections, &hash_make_key, &cnx_cmp); + CHECK_ALLOC(fd_info->hash_sources, "hash_init"); udp_protocol_list_init(); udp_sanity_check();