Skip to contentSkip to conversation

packed struct sizeof wrong

DISCUSSION Johannes Berg 1 participant 1 message May 28, 2010 Sparse checker

lore
Share & tools
lore
#0 lore raw

Hi all,

I ran across this in some of my kernel code, and isolated it into a few test cases, below. sparse should not report errors on this file, but does regardless, somehow the structs appear larger to it than they are.

johannes

This is suitable for dropping into the validation directory:
--- begin file ---

struct A {
	unsigned char a, b, c, d;
} __attribute__((packed));
struct B {
	struct A A;
	unsigned char e, f;
	unsigned short g;
	unsigned char h;
} __attribute__((packed));
struct C {
	unsigned char a, b, c, d;
	unsigned char e, f;
	unsigned short g;
	unsigned char h;
} __attribute__((packed));
struct D {
	unsigned char h;
} __attribute__((packed));
struct E {
	unsigned short g;
	unsigned char h;
} __attribute__((packed));

#define BUILD_BUG_ON(expr) ((void)(sizeof(struct { int:-!!(expr); })))

static inline void foo(void)

{
	BUILD_BUG_ON(sizeof(struct A) != 4);
	BUILD_BUG_ON(sizeof(struct B) != 9);
	BUILD_BUG_ON(sizeof(struct C) != 9);
	BUILD_BUG_ON(sizeof(struct D) != 1);
	BUILD_BUG_ON(sizeof(struct E) != 3);
}

/*
* check-name: packed struct sizeof
*/