Use memcpy as data may not be word aligned
authorNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 16 Mar 2019 16:26:49 +0000 (17:26 +0100)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 8 Nov 2022 16:03:05 +0000 (17:03 +0100)
lib/protocols/mqtt.c

index d9e9ef3..d11a809 100644 (file)
@@ -330,7 +330,7 @@ int mqtt_unpack_publish_packet(struct mqtt_publish_pkt* pkt, uint8_t* buf, uint3
                return -EPROTO;
        }
        /* Decode topic string */
-       tmp = *((uint16_t*)(buf + idx));
+       memcpy(&tmp, (buf + idx), 2);
        topic_len = ntohs(tmp);
        pkt->topic.name = (char*)(buf + idx + 2);
        idx += topic_len + 2;
@@ -338,9 +338,9 @@ int mqtt_unpack_publish_packet(struct mqtt_publish_pkt* pkt, uint8_t* buf, uint3
                return -EPROTO;
        }
        /* Decode packet ID */
-       tmp = *((uint16_t*)(buf + idx));
-       pkt->packet_id = ntohs(tmp);
        idx += 2;
+               memcpy(&tmp, (buf + idx), 2);
+               pkt->packet_id = ntohs(tmp);
        /* Get application message */
        pkt->message_size = size - idx;
        if (pkt->message_size != 0) {