ImportC: Fix interaction of aligned and packed structs (#21204)

Previous iteration of this did not properly account for the interaction
of aligned and packed and would even segfault on a null access in such
a case. This version properly handles that interaction by aligning the
struct itself instead of the first member and not forcing the struct
alignment to 1 if it is packed.
This commit is contained in:
drpriver 2025-04-12 15:55:56 -07:00 committed by GitHub
parent 346a772985
commit 6c3860ef32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 76 additions and 62 deletions

View file

@ -41,3 +41,37 @@ struct __attribute__((aligned(4))) D {
__attribute__((aligned(8)));
_Static_assert(_Alignof(struct D)==8, "D");
//
// Interaction of aligned() and packed
//
#include <stddef.h>
struct Spacked {
unsigned a;
unsigned long long b;
} __attribute__((aligned(4), packed));
_Static_assert(_Alignof(struct Spacked) == 4, "Spacked");
_Static_assert(_Alignof(struct Spacked) == 4, "Spacked");
_Static_assert(offsetof(struct Spacked, a) == 0, "Spacked.a");
_Static_assert(offsetof(struct Spacked, b) == sizeof(unsigned), "Spacked.b");
_Static_assert(sizeof(struct Spacked) == sizeof(unsigned) + sizeof(unsigned long long), "sizeof(Spacked)");
struct __attribute__((aligned(4))) Spacked2 {
unsigned a;
unsigned long long b;
} __attribute__((packed));
_Static_assert(_Alignof(struct Spacked2) == 4, "Spacked2");
_Static_assert(offsetof(struct Spacked2, a) == 0, "Spacked2.a");
_Static_assert(offsetof(struct Spacked2, b) == sizeof(unsigned), "Spacked2.b");
_Static_assert(sizeof(struct Spacked2) == sizeof(unsigned) + sizeof(unsigned long long), "sizeof(Spacked2)");
#pragma pack(push)
#pragma pack(1)
struct __attribute__((aligned(4))) Spacked3 {
unsigned a;
unsigned long long b;
};
#pragma pack(pop)
_Static_assert(_Alignof(struct Spacked3) == 4, "Spacked3");
_Static_assert(offsetof(struct Spacked3, a) == 0, "Spacked3.a");
_Static_assert(offsetof(struct Spacked3, b) == sizeof(unsigned), "Spacked3.b");
_Static_assert(sizeof(struct Spacked3) == sizeof(unsigned) + sizeof(unsigned long long), "sizeof(Spacked3)");