Skip to content

Commit

Permalink
Do not use an aliased pointer in dns_ai_setent
Browse files Browse the repository at this point in the history
Fixes #186
  • Loading branch information
daurnimator committed Jun 18, 2017
1 parent 97cac6d commit 8b5bbf9
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/lib/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -8195,29 +8195,23 @@ void dns_ai_close(struct dns_addrinfo *ai) {


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;
struct sockaddr_in6 sin6;
union { struct sockaddr saddr; struct sockaddr_in sin; struct sockaddr_in6 sin6; } saddr = {0};
const char *cname;
size_t clen;

switch (type) {
case DNS_T_A:
saddr = memset(&sin, '\0', sizeof sin);
saddr.sin.sin_family = AF_INET;
saddr.sin.sin_port = htons(ai->port);

sin.sin_family = AF_INET;
sin.sin_port = htons(ai->port);

memcpy(&sin.sin_addr, any, sizeof sin.sin_addr);
memcpy(&saddr.sin.sin_addr, any, sizeof saddr.sin.sin_addr);

break;
case DNS_T_AAAA:
saddr = memset(&sin6, '\0', sizeof sin6);

sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(ai->port);
saddr.sin6.sin6_family = AF_INET6;
saddr.sin6.sin6_port = htons(ai->port);

memcpy(&sin6.sin6_addr, any, sizeof sin6.sin6_addr);
memcpy(&saddr.sin6.sin6_addr, any, sizeof saddr.sin6.sin6_addr);

break;
default:
Expand All @@ -8232,20 +8226,20 @@ static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_typ
clen = 0;
}

if (!(*ent = malloc(sizeof **ent + dns_sa_len(saddr) + ((ai->hints.ai_flags & AI_CANONNAME)? clen + 1 : 0))))
if (!(*ent = malloc(sizeof **ent + dns_sa_len(&saddr) + ((ai->hints.ai_flags & AI_CANONNAME)? clen + 1 : 0))))
return dns_syerr();

memset(*ent, '\0', sizeof **ent);

(*ent)->ai_family = saddr->sa_family;
(*ent)->ai_family = saddr.saddr.sa_family;
(*ent)->ai_socktype = ai->hints.ai_socktype;
(*ent)->ai_protocol = ai->hints.ai_protocol;

(*ent)->ai_addr = memcpy((unsigned char *)*ent + sizeof **ent, saddr, dns_sa_len(saddr));
(*ent)->ai_addrlen = dns_sa_len(saddr);
(*ent)->ai_addr = memcpy((unsigned char *)*ent + sizeof **ent, &saddr, dns_sa_len(&saddr));
(*ent)->ai_addrlen = dns_sa_len(&saddr);

if (ai->hints.ai_flags & AI_CANONNAME)
(*ent)->ai_canonname = memcpy((unsigned char *)*ent + sizeof **ent + dns_sa_len(saddr), cname, clen + 1);
(*ent)->ai_canonname = memcpy((unsigned char *)*ent + sizeof **ent + dns_sa_len(&saddr), cname, clen + 1);

ai->found++;

Expand Down

0 comments on commit 8b5bbf9

Please sign in to comment.