From 9b7125cde9de3d69e6c8c75e1740bc2ef7d4a668 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 8 Apr 2016 22:17:45 +0200 Subject: [PATCH] BUG/MEDIUM: dns: fix alignment issue when building DNS queries On some architectures, unaligned access is not authorized. On most architectures, it is just slower. Therefore, we have to use memcpy() when an unaligned access is needed, specifically when writing the qinfo. Also remove the unaligned access when reading answer count when reading the answer. It's likely that this instruction was optimized away by the compiler since it is unneeded. Add a comment to explain why we use 7 as an offset instead of 6. Not an unaligned offset since "resp" is "unsigned char", then promoted to int. --- src/dns.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/dns.c b/src/dns.c index 3906742bc..3b3dfc59e 100644 --- a/src/dns.c +++ b/src/dns.c @@ -619,8 +619,7 @@ int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end, cname = *newip = newip4 = newip6 = NULL; cnamelen = currentip_found = 0; *newip_sin_family = AF_UNSPEC; - ancount = (((struct dns_header *)resp)->ancount); - ancount = *(resp + 7); + ancount = *(resp + 7); /* Assume no more than 256 answers */ /* bypass DNS response header */ reader = resp + sizeof(struct dns_header); @@ -975,7 +974,7 @@ int dns_init_resolvers(void) int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize) { struct dns_header *dns; - struct dns_question *qinfo; + struct dns_question qinfo; char *ptr, *bufend; memset(buf, '\0', bufsize); @@ -1021,9 +1020,9 @@ int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostnam return -1; /* set up query info (type and class) */ - qinfo = (struct dns_question *)ptr; - qinfo->qtype = htons(query_type); - qinfo->qclass = htons(DNS_RCLASS_IN); + qinfo.qtype = htons(query_type); + qinfo.qclass = htons(DNS_RCLASS_IN); + memcpy(ptr, &qinfo, sizeof(qinfo)); ptr += sizeof(struct dns_question);