[virt-tools-list] [osinfo PATCH] Mark the PrimaryVolumeDescriptor struct as packed
Daniel P. Berrange
berrange at redhat.com
Fri Jan 6 14:03:42 UTC 2012
On Fri, Jan 06, 2012 at 06:52:04AM -0700, Eric Blake wrote:
> On 01/06/2012 05:21 AM, Christophe Fergeau wrote:
> > This tells gcc it shouldn't add some padding between the struct
> > members. which is important because we rely on this structure size
> > being exactly 2048. One of its members is guint8 ignored2[246]; (only
> > a 2 byte multiple) so all the fields of the struct won't be aligned
> > on a 8 byte boundary, so gcc could do some unexpected things on
> > some arch.
> > ---
> > osinfo/osinfo_media.c | 2 +-
> > 1 files changed, 1 insertions(+), 1 deletions(-)
> >
> > diff --git a/osinfo/osinfo_media.c b/osinfo/osinfo_media.c
> > index 2366993..2a80205 100644
> > --- a/osinfo/osinfo_media.c
> > +++ b/osinfo/osinfo_media.c
> > @@ -44,7 +44,7 @@ struct _PrimaryVolumeDescriptor {
> > guint8 ignored2[246];
> > gchar publisher[MAX_PUBLISHER]; /* Publisher ID */
> > guint8 ignored3[1602];
> > -};
> > +} __attribute__ ((packed));;
>
> When a struct must be an exact size, it's probably worth adding a
> compile-time verification, so that you get a compilation failure if any
> compiler ever picks the wrong size. If you used gnulib, it would be
> fairly trivial to use "verify.h" for this purpose, but even without
> gnulib, you can do:
>
> char dummy[sizeof(struct _PrimaryVolumeDescriptor) == 2048 ? 1 : -1];
The gnulib verify.h header is pretty much self-contained & license
compatible, so we could just copy it across to our code without
any pain.
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
More information about the virt-tools-list
mailing list