Skip to content

fix alignment of fields within mqoi_desc_t - #5

Open
clhuang wants to merge 2 commits into
shraiwi:masterfrom
clhuang:master
Open

fix alignment of fields within mqoi_desc_t#5
clhuang wants to merge 2 commits into
shraiwi:masterfrom
clhuang:master

Conversation

@clhuang

@clhuang clhuang commented May 14, 2024

Copy link
Copy Markdown

Attempting to use the minimal code example on an RP2040 fails (the board crashes) while attempting to run mqoi_desc_verify.
I narrowed the crash down to the line https://github.com/shraiwi/mini-qoi/blob/master/src/mini_qoi.c#L34, which casts a potentially unaligned uint8_t* to a uint32_t*, which is undefined behavior. Padding the struct so the width/height fields are 4-byte aligned fixes the issue.

@GorgonMeducer

GorgonMeducer commented Dec 3, 2025

Copy link
Copy Markdown

It is better to use the following method:

typedef struct {
    uint8_t head;

    uint8_t magic[4]; __attribute__((aligned(4)));

    uint8_t width[4] __attribute__((aligned(4)));  // big-endian width

    uint8_t height[4] __attribute__((aligned(4))); // big-endian height

    uint8_t channels;
    uint8_t colorspace;
} mqoi_desc_t;

Then the compiler will apply 4-byte alignment inside the mqoi_desc_t and add padding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants