From: Vincent Bernat <vinc...@bernat.im>

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 3906742bce36..3b3dfc59e065 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);
 
-- 
2.8.0.rc3


Reply via email to