From 63c48343be343dfac09c9cf76d45017ccefb89b0 Mon Sep 17 00:00:00 2001 From: Nuno Silva Date: Wed, 1 Apr 2020 17:39:51 +0100 Subject: [PATCH] ping: properly iterate addrinfo (#153) to avoid a memory leak introduced by #149, as per https://github.com/Cacti/spine/commit/5644db77320ce87075cdec95250def87461fc354#r37979275 and http://man7.org/linux/man-pages/man3/getaddrinfo.3.html --- ping.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ping.c b/ping.c index 7e236732..81aaad1b 100644 --- a/ping.c +++ b/ping.c @@ -854,7 +854,7 @@ int ping_tcp(host_t *host, ping_t *ping) { * \return 1 - IPv4, 2 - IPv6, 0 - Unknown */ int get_address_type(host_t *host) { - struct addrinfo hints, *res; + struct addrinfo hints, *res, *res_list; char addrstr[255]; void *ptr; int addr_found = FALSE; @@ -866,15 +866,15 @@ int get_address_type(host_t *host) { hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; int error; - if ((error = getaddrinfo(host->hostname, NULL, &hints, &res)) != 0) { + if ((error = getaddrinfo(host->hostname, NULL, &hints, &res_list)) != 0) { SPINE_LOG(("WARNING: Unable to determine address info for %s (%s)", host->hostname, gai_strerror(error))); - if (res != NULL) { - freeaddrinfo(res); + if (res_list != NULL) { + freeaddrinfo(res_list); } return SPINE_NONE; } - while (res) { + for (res = res_list; res != NULL; res = res->ai_next) { inet_ntop(res->ai_family, res->ai_addr->sa_data, addrstr, 100); switch(res->ai_family) { @@ -893,15 +893,13 @@ int get_address_type(host_t *host) { SPINE_LOG_HIGH(("Device[%d] IPv%d address %s (%s)\n", host->id, res->ai_family == PF_INET6 ? 6:4, addrstr, res->ai_canonname)); if (res->ai_family != PF_INET6) { - freeaddrinfo(res); + freeaddrinfo(res_list); return SPINE_IPV4; } - - res = res->ai_next; } - freeaddrinfo(res); + freeaddrinfo(res_list); if (addr_found) { return SPINE_IPV6;