Skip to content

Commit

Permalink
ping: properly iterate addrinfo (#153)
Browse files Browse the repository at this point in the history
to avoid a memory leak introduced by #149, as per 5644db7#r37979275
and http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
  • Loading branch information
nuno-silva authored Apr 1, 2020
1 parent cc34685 commit 63c4834
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 63c4834

Please sign in to comment.