On 08/12/2017 10:38 AM, Philippe Mathieu-Daudé wrote: > Hi Eric, > > On 08/12/2017 08:41 AM, Eric Blake wrote: >> Here's an idea: Instead of using struct ip6 { ... } QEMU_PACKED, use >> >> struct ip6 { >> ... >> }; >> QEMU_BUG_ON(sizeof(struct ip6) != 32); >> >> so that the compiler explicitly chokes if it introduced any padding, and >> then we know all our accesses are naturally aligned which shuts up the >> warning. > > Following your suggestion I tried: > > #define QEMU_BUG_STRUCT_UNALIGNED(s) \ > QEMU_BUILD_BUG_ON(sizeof(s) != sizeof(s QEMU_PACKED))
I don't think gcc likes that (you can't just add attribute packed in arbitrary places, but only where the type is declared). > > QEMU_BUG_STRUCT_UNALIGNED(struct ip6); > > but this gives: > > qemu/slirp/ip6.h:157:1: warning: 'packed' attribute ignored when parsing > type [-Wignored-attributes] > QEMU_BUG_STRUCT_UNALIGNED(struct ip6); > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Indeed, there's the evidence that your attempt at a shortcut didn't work. My proposal was to put in a hard-coded number, rather than try to make the compiler compute two variants of the type and check that they occupy the same space (although I agree with your idea that if you CAN get the compiler to do that, it's nicer than hand-computing the number to compare against). -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature