Packed/padded structs and pointer arithmetic

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am seeing things like this quite often in kernel code.

p = (struct partition *) (data + 0x1be);

But isn't it the case that structures by default can have padding for 
alignment, which can even be different between architectures? So for 
example...

struct foo {
	short int quux;
	short int quuux;
} ;

...could be actually eight bytes long, and doing this...

static unsigned char bar[] = {0x00, 0x55, 0xAA, 0xFF};
static struct *foo = (struct foo*)bar;
static short int quux = foo->quux;
static short int quuux = foo->quuux;

...might end up blowing past the end of the array?

(Not to mention the fact that the C standard indicates that you can use 
the binary representation of an object by converting it to void* or char* 
but that you cannot go the other way because it is not guaranteed for all 
architectures that array elements are packed, even though in practice we 
always assume that they are.)

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux