From 9bec6fb916f65cec99e981a6733984a8a17f30d3 Mon Sep 17 00:00:00 2001 From: Michael Evertz Date: Mon, 12 Sep 2016 14:37:53 +0200 Subject: Fix misaligned memory access This changes fixes misaligned memory access. Without the patch I get several "Misaligend access trap for user program 'llmnrd'" messages on my platform if the system has odd hostname length. --- llmnr.c | 6 ++++-- pkt.h | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/llmnr.c b/llmnr.c index 3090845..9db381e 100644 --- a/llmnr.c +++ b/llmnr.c @@ -124,8 +124,10 @@ static void llmnr_respond(unsigned int ifindex, const struct llmnr_hdr *hdr, if ((query_len - name_len - 2) < (sizeof(qtype) + sizeof(qclass))) return; - qtype = ntohs(*((uint16_t *)query_name_end)); - qclass = ntohs(*((uint16_t *)query_name_end + 1)); + memcpy(&qtype, query_name_end, sizeof(qtype)); + qtype = ntohs(qtype); + memcpy(&qclass, query_name_end + sizeof(qtype), sizeof(qclass)); + qclass = ntohs(qclass); /* Only IN queries supported */ if (qclass != LLMNR_QCLASS_IN) diff --git a/pkt.h b/pkt.h index e829b8c..bb2b717 100644 --- a/pkt.h +++ b/pkt.h @@ -100,8 +100,8 @@ static inline uint8_t *pkt_put(struct pkt *p, size_t len) #define DEFINE_PKT_PUT(__bitwidth) \ static inline void pkt_put_u##__bitwidth(struct pkt *p, uint##__bitwidth##_t val) \ { \ - uint##__bitwidth##_t *data = (uint##__bitwidth##_t *)pkt_put(p, sizeof(val)); \ - *data = val; \ + uint8_t *data = pkt_put(p, sizeof(val)); \ + memcpy(data, &val, sizeof(uint##__bitwidth##_t)); \ } DEFINE_PKT_PUT(8) -- cgit v1.2.3-54-g00ecf