lua-users home
lua-l archive

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


On Mon, Oct 11, 2021 at 07:36:34PM +0200, Pierre Chapuis wrote:
> I would need more tests, including on hardware I no longer have access to,
> but I have had experience with packed structs and unaligned accesses and
> in general you do not want them unless you have a very good reason.
> 
> - Not all ARM CPUs support them. On some 32 bit ARM CPUs that don't
>   you will get SIGBUS on Android.

The semantics of the GCC packed attribute, IIUC, is that if the architecture
doesn't natively support unaligned access, the compiler generates the
necessary code for safe behavior. The caveat is that the compiler can't
generate the necessary code if you access internal members through
unqualified pointers.

This should always work correctly

  struct __attribute__((__packed__)) foo {
     char c;
     int i;
  };

  struct foo *fp = ...;
  printf("%d\n", fp->i);

but not

  int *ip = &fp->i;
  printf("%d\n", *ip);

Compare the much newer scalar_storage_order("endianness") attribute which
also implicitly generates the correct code according to architecture, except
they made it an error to take pointers to internal scalar members,
mitigating the footgun quality of the packed attribute.