Skip to contentSkip to conversation

Packed struct size issue

DISCUSSION John Levon 2 participants 3 messages Jul 17, 2019 – Jul 18, 2019

lore
Share & tools
lore

John Levon

author
#0 lore raw

Andy Fiddaman discovered:

$ cat ./test.c
#include <stdio.h>
#include <stdint.h>
struct t1 {
        uint8_t                 critical_warning;
        uint16_t                temperature;
        uint8_t                 available_spare;
} __packed;

_Static_assert(sizeof(struct t1) == 4, "bad size for t1");

struct t2 {
        uint8_t                 critical_warning;
        uint16_t                temperature;
        uint8_t                 available_spare;
};

_Static_assert(sizeof(struct t2) == 6, "bad size for t2");

$ ./smatch ./test.c
./test.c:10:34: error: static assertion failed: "bad size for t1"

Looks like we lost this commit:
https://github.com/illumos/smatch/commit/0c60237e4096b7576f0aec21a7545026c7041611#diff-0cd054d23ed9e02274eaa540fc7e6536

??

This works:

https://github.com/citrus-it/illumos-omnios/commit/88e29e1705a616695ec6f905c3cab1f600e12659?diff=unified

regards
john

#1 lore raw
Quoted 7 lines · John Levon

Andy Fiddaman discovered:

$ cat ./test.c
#include <stdio.h>
#include <stdint.h>
Quoted 8 lines
struct t1 {
        uint8_t                 critical_warning;
        uint16_t                temperature;
        uint8_t                 available_spare;
} __packed;

_Static_assert(sizeof(struct t1) == 4, "bad size for t1");

I've added Sparse mailing list to the CC.

It looks like the packed attribute is broken on upstream Sparse (from April) as well and I would prefer if they fixed it and I pulled in their fix. It's probably better to use __attribute__((packed)) instead of the __packed macro in the test code.

regards,
dan carpenter

#2 lore raw
In reply to John Levon

[ resending to the correct Sparse email address ].

Quoted 7 lines · John Levon

Andy Fiddaman discovered:

$ cat ./test.c
#include <stdio.h>
#include <stdint.h>
Quoted 8 lines
struct t1 {
        uint8_t                 critical_warning;
        uint16_t                temperature;
        uint8_t                 available_spare;
} __packed;

_Static_assert(sizeof(struct t1) == 4, "bad size for t1");

I've added Sparse mailing list to the CC.

It looks like the packed attribute is broken on upstream Sparse (from April) as well and I would prefer if they fixed it and I pulled in their fix. It's probably better to use __attribute__((packed)) instead of the __packed macro in the test code.

regards,
dan carpenter