Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
vlm committed Mar 25, 2017
1 parent 0f8add9 commit 393b41a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -7416,6 +7416,10 @@ void dns_ai_close(struct dns_addrinfo *ai) {
} /* dns_ai_close() */


void dns_ai_nextent_free(struct addrinfo *ent) {
free(ent);
}

static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_type type, struct dns_addrinfo *ai) {
struct sockaddr *saddr;
struct sockaddr_in sin;
Expand Down
2 changes: 2 additions & 0 deletions dns/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ void dns_ai_close(struct dns_addrinfo *);

int dns_ai_nextent(struct addrinfo **, struct dns_addrinfo *);

void dns_ai_nextent_free(struct addrinfo *);

size_t dns_ai_print(void *, size_t, struct addrinfo *, struct dns_addrinfo *);

time_t dns_ai_elapsed(struct dns_addrinfo *);
Expand Down
32 changes: 25 additions & 7 deletions ipaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,33 +284,47 @@ int ipaddr_remote(struct ipaddr *addr, const char *name, int port, int mode,
int err = errno;
fdclean(fd);
errno = err;
if(dill_slow(rc < 0)) return -1;
if(dill_slow(rc < 0)) {
dns_ai_close(ai);
return -1;
}
continue;
}
if(rc == ENOENT || (rc >= DNS_EBASE && rc <= DNS_ELAST))
break;
if(!ipv4 && it && it->ai_family == AF_INET)
if(!ipv4 && it && it->ai_family == AF_INET) {
ipv4 = it;
if(!ipv6 && it && it->ai_family == AF_INET6)
it = NULL;
}
if(!ipv6 && it && it->ai_family == AF_INET6) {
ipv6 = it;
it = NULL;
}
dns_ai_nextent_free(it); /* Ended up useless */
if(ipv4 && ipv6)
break;
}
switch(mode) {
case IPADDR_IPV4:
dns_ai_nextent_free(ipv6);
ipv6 = NULL;
break;
case IPADDR_IPV6:
dns_ai_nextent_free(ipv4);
ipv4 = NULL;
break;
case 0:
case IPADDR_PREF_IPV4:
if(ipv4)
ipv6 = NULL;
if(ipv4) {
dns_ai_nextent_free(ipv6);
ipv6 = NULL;
}
break;
case IPADDR_PREF_IPV6:
if(ipv6)
ipv4 = NULL;
if(ipv6) {
dns_ai_nextent_free(ipv4);
ipv4 = NULL;
}
break;
default:
dill_assert(0);
Expand All @@ -319,13 +333,17 @@ int ipaddr_remote(struct ipaddr *addr, const char *name, int port, int mode,
struct sockaddr_in *inaddr = (struct sockaddr_in*)addr;
memcpy(inaddr, ipv4->ai_addr, sizeof (struct sockaddr_in));
inaddr->sin_port = htons(port);
dns_ai_nextent_free(ipv4);
dns_ai_nextent_free(ipv6);
dns_ai_close(ai);
return 0;
}
if(ipv6) {
struct sockaddr_in6 *inaddr = (struct sockaddr_in6*)addr;
memcpy(inaddr, ipv6->ai_addr, sizeof (struct sockaddr_in6));
inaddr->sin6_port = htons(port);
dns_ai_nextent_free(ipv4);
dns_ai_nextent_free(ipv6);
dns_ai_close(ai);
return 0;
}
Expand Down

0 comments on commit 393b41a

Please sign in to comment.