LiamBindle / MQTT-C

A portable MQTT C client for embedded systems and PCs alike.
https://liambindle.ca/MQTT-C
MIT License
766 stars 269 forks source link

Memory alignment in Keil ARM Compiler (thumb instruction set) #183

Open andersonrs66 opened 1 year ago

andersonrs66 commented 1 year ago

I am using MQTT-C in a bare-metal project on Keil MDK 5 and I encountered an issue with memory alignment. I noticed that the returned structure members from mqtt_mq_get were shifted by 1 or more bits. Then leading to messages with the wrong payload size and some other random errors.

The problem stopped when I added the packed attribute to the struct mqtt_queued_message.

Before:

struct mqtt_queued_message {

After:

struct __attribute__((packed)) mqtt_queued_message {

I don't know if it's the best solution but it fixed my problem, so I thought I should report it.