Received: from eggs.gnu.org ([2001:4830:134:3::10]:55005) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLxBL-0004H7-Vt for qemu-devel@nongnu.org; Wed, 20 Jan 2016 13:08:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLxBI-0006Bf-IT for qemu-devel@nongnu.org; Wed, 20 Jan 2016 13:08:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50016) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLxBI-0006Bb-Di for qemu-devel@nongnu.org; Wed, 20 Jan 2016 13:08:32 -0500 From: Markus Armbruster References: <1453219845-30939-1-git-send-email-eblake@redhat.com> <1453219845-30939-13-git-send-email-eblake@redhat.com> Date: Wed, 20 Jan 2016 19:08:29 +0100 In-Reply-To: <1453219845-30939-13-git-send-email-eblake@redhat.com> (Eric Blake's message of "Tue, 19 Jan 2016 09:10:20 -0700") Message-ID: <87lh7km8de.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v9 12/37] qapi: Don't cast Enum* to int* List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: marcandre.lureau@redhat.com, qemu-devel@nongnu.org, Michael Roth Eric Blake writes: > C compilers are allowed to represent enums as a smaller type > than int, if all enum values fit in the smaller type. There > are even compiler flags that force the use of this smaller > representation, and using them changes the ABI of a binary. Suggest "although using them". > Therefore, our generated code for visit_type_ENUM() (for all > qapi enums) was wrong for casting Enum* to int* when calling > visit_type_enum(). > > It appears that no one has been doing this for qemu, because > if they had, we are potentially dereferencing beyond bounds > or even risking a SIGBUS on platforms where unaligned pointer > dereferencing is fatal. Better is to avoid the practice > entirely, and just use the correct types. > > This matches the fix for alternate qapi types, done earlier in > commit 0426d53 "qapi: Simplify visiting of alternate types", > with generated code changing as: > > | void visit_type_GuestDiskBusType(Visitor *v, GuestDiskBusType *obj, con= st char *name, Error **errp) > | { > |- visit_type_enum(v, (int *)obj, GuestDiskBusType_lookup, "GuestDiskB= usType", name, errp); > |+ int tmp =3D *obj; > |+ visit_type_enum(v, &tmp, GuestDiskBusType_lookup, "GuestDiskBusType= ", name, errp); > |+ *obj =3D tmp; > | } Long lines. Do we have an example with a shorter enum name handy? > Signed-off-by: Eric Blake > Reviewed-by: Marc-Andr=C3=A9 Lureau > > --- > v9: mention earlier commit id, enhance commit message > v8: no change > v7: rebase on typo fix > v6: new patch > --- > scripts/qapi-visit.py | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py > index 4a4f67d..6bd188b 100644 > --- a/scripts/qapi-visit.py > +++ b/scripts/qapi-visit.py > @@ -178,12 +178,13 @@ out: > > > def gen_visit_enum(name): > - # FIXME cast from enum *obj to int * invalidly assumes enum is int > return mcgen(''' > > void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name= , Error **errp) > { > - visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, = errp); > + int tmp =3D *obj; > + visit_type_enum(v, &tmp, %(c_name)s_lookup, "%(name)s", name, errp); > + *obj =3D tmp; > } > ''', > c_name=3Dc_name(name), name=3Dname) Same pattern in qapi-visit-core.c, except we name the variable @value there. Your choice.