RE: [PATCH v2] netlink: align attributes on 64-bits
From: David Laight <hidden>
Date: 2012-12-20 09:46:43
On 12/19/12 at 09:17am, David Laight wrote:quoted
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);
That will be fine on all systems. But if, instead, you have: struct foo buf, *bufp; bufp = nla_data(attr); memcpy(&buf, bufp, sizeof buf); The compiler is allowed to assume that 'bufp' is aligned, so the copy will be done using 64bit accesses. (Basically because all you are allowed to do with 'void *' is cast a point to 'void *', then back to its original type. So when you cast back from 'void *' the pointer can be assumed to be aligned.) This will fault on systems that require strict alignment of 64bit items. David