From abcb12c998cb2c93d2bbfed39405e228c34449fe Mon Sep 17 00:00:00 2001 From: Michael Torres Date: Sun, 9 Nov 2025 10:25:56 -0800 Subject: [PATCH] Check if ai_list is null before freeing it Passing null to freeaddrinfo is undefined behaviour. While glibc and uClibc both handle it safely, musl does not, so a bad nodename or servname causes a segfault. This change ensures that the ai_list is non-null before trying to free it. In musl and glibc this condition will never be met, but there are some paths for it to be met in uClibc. --- src/modbus-tcp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index 8e5e37cdc..0646b9a2f 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -658,7 +658,9 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) fprintf(stderr, "Error returned by getaddrinfo: %d\n", rc); #endif } - freeaddrinfo(ai_list); + if (ai_list != NULL) { + freeaddrinfo(ai_list); + } errno = ECONNREFUSED; return -1; }