From 8b5bbf9dbe6c8262f3f29a8e15dec7d07993655d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 18 Jun 2017 22:26:05 +1000 Subject: [PATCH] Do not use an aliased pointer in dns_ai_setent Fixes #186 --- src/lib/dns.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/lib/dns.c b/src/lib/dns.c index 15ff335..a6bb6a6 100644 --- a/src/lib/dns.c +++ b/src/lib/dns.c @@ -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: @@ -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++;