Thread (45 messages) flat view 45 messages, 5 authors, 2012-12-20

Re: [PATCH v2] netlink: align attributes on 64-bits

From: Thomas Graf <tgraf@suug.ch>
Date: 2012-12-19 17:20:16

On 12/19/12 at 09:17am, David Laight wrote:
You can't use memcpy() to copy a pointer to a misaligned
structure into an aligned buffer. The compiler assumes
the pointer is aligned and will use instructions that
depend on the alignment.
I am not sure I understand this correctly. Are you saying
that the following does not work on i386?

struct foo {
  uint32_t a;
  uint64_t b;
};

struct foo buf;

memcpy(&buf, nla_data(attr), nla_len(attr));
printf([...], buf.b);

I think:
1) Alignment is only needed on systems that have 'strict alignment'
   requirements (maybe disable for testing?)
Right, what about mixed 32bit/64bit environments?
2) Alignment is only needed for parameters whose size is a
   multiple of the alignment (a structure containing a
   field that needs 8 byte alignment will always be a multiple
   of 8 bytes long).
Good point. I'll fix this in the next iteration of the patch.
3) You need to add NA_HDR_LEN to the write pointer before
   determining the size of the pad.
Right, I'm doing this in the patch I proposed. Or are you referring
to something else?
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help