From a8f0e2801e606cd48832a1d208b9f5a3499c48a8 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Mon, 24 Feb 2025 12:15:01 +0100 Subject: [PATCH] os: replace xallocarray() by calloc() Only key difference that calloc(), in contrast to rellocarray(), is zero-initializing. The overhead is hard to measure on today's machines, and it's safer programming practise to always allocate zero-initialized, so one can't forget to do it explicitly. Cocci rule: @@ expression COUNT; expression LEN; @@ - xallocarray(COUNT,LEN) + calloc(COUNT,LEN) Signed-off-by: Enrico Weigelt, metux IT consult --- os/connection.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/os/connection.c b/os/connection.c index c18822ad3..06447f1d1 100644 --- a/os/connection.c +++ b/os/connection.c @@ -119,6 +119,8 @@ SOFTWARE. #include "probes.h" #include "xdmcp.h" +#define MAX_CONNECTIONS (2^16) + struct ospoll *server_poll; Bool NewOutputPending; /* not yet attempted to write some new output */ @@ -266,7 +268,12 @@ CreateWellKnownSockets(void) LogSetDisplay(); } - ListenTransFds = xallocarray(ListenTransCount, sizeof (int)); + if (ListenTransCount >= MAX_CONNECTIONS) { + FatalError ("Tried to clear too many listening sockets - OOM"); + return; // mostly to keep GCC from complaining about too large alloc + } + + ListenTransFds = calloc(ListenTransCount, sizeof(int)); if (ListenTransFds == NULL) FatalError ("Failed to create listening socket array");