From 1d4936a3177ae1644a4b3488845aa05b1e265f3e Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Sun, 8 Oct 2023 23:10:42 +0200 Subject: [PATCH 1/2] Avoid possible null pointer dereference --- src/tcldcc.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/tcldcc.c b/src/tcldcc.c index 6a704de87..ddb6e07eb 100644 --- a/src/tcldcc.c +++ b/src/tcldcc.c @@ -1028,7 +1028,7 @@ static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *m struct portmap *pmap = NULL, *pold = NULL; sockname_t name; struct in_addr ipaddr4; - struct addrinfo hint, *ipaddr = NULL; + struct addrinfo hint, *res0 = NULL; #ifdef IPV6 struct in6_addr ipaddr6; #endif @@ -1049,22 +1049,22 @@ static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *m } else { strlcpy(newip, ip, sizeof newip); } - /* Return addrinfo struct ipaddr containing family... */ - error = getaddrinfo(newip, NULL, &hint, &ipaddr); + /* Return addrinfo struct containing family... */ + error = getaddrinfo(newip, NULL, &hint, &res0); if (!error) { - /* Load network address to in(6)_addr struct for later byte comparisons */ - if (ipaddr->ai_family == AF_INET) { + if (!res0) + putlog(LOG_MISC, "*", "setlisten(): getaddrinfo() returned no address for ip %s", newip); + /* Load network address to in(6)_addr struct for later byte comparisons */ + if (res0->ai_family == AF_INET) { inet_pton(AF_INET, newip, &ipaddr4); } #ifdef IPV6 - else if (ipaddr->ai_family == AF_INET6) { + else if (res0->ai_family == AF_INET6) { inet_pton(AF_INET6, newip, &ipaddr6); ipv4 = 0; } #endif - if (ipaddr) /* The behavior of freeadrinfo(NULL) is left unspecified by RFCs - * 2553 and 3493. Avoid to be compatible with all OSes. */ - freeaddrinfo(ipaddr); + freeaddrinfo(res0); } else if (error == EAI_NONAME) /* currently setlisten() handles only ip not hostname */ From 9bf44281a9ddf396b9f59ffd2df0523aa6a3711a Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Mon, 9 Oct 2023 09:30:39 +0200 Subject: [PATCH 2/2] Proper fix --- src/tcldcc.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/tcldcc.c b/src/tcldcc.c index ddb6e07eb..825efbbed 100644 --- a/src/tcldcc.c +++ b/src/tcldcc.c @@ -1052,19 +1052,20 @@ static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *m /* Return addrinfo struct containing family... */ error = getaddrinfo(newip, NULL, &hint, &res0); if (!error) { - if (!res0) - putlog(LOG_MISC, "*", "setlisten(): getaddrinfo() returned no address for ip %s", newip); - /* Load network address to in(6)_addr struct for later byte comparisons */ - if (res0->ai_family == AF_INET) { - inet_pton(AF_INET, newip, &ipaddr4); - } + if (res0) { + /* Load network address to in(6)_addr struct for later byte comparisons */ + if (res0->ai_family == AF_INET) { + inet_pton(AF_INET, newip, &ipaddr4); + } #ifdef IPV6 - else if (res0->ai_family == AF_INET6) { - inet_pton(AF_INET6, newip, &ipaddr6); - ipv4 = 0; - } + else if (res0->ai_family == AF_INET6) { + inet_pton(AF_INET6, newip, &ipaddr6); + ipv4 = 0; + } #endif - freeaddrinfo(res0); + freeaddrinfo(res0); + } else + putlog(LOG_MISC, "*", "setlisten(): getaddrinfo() returned no address for ip %s", newip); } else if (error == EAI_NONAME) /* currently setlisten() handles only ip not hostname */