From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34201) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aT6cW-0007rD-34 for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aT6cU-00030t-VD for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:11 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50556) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aT6cU-00030g-Ok for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:10 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 65B908F296 for ; Tue, 9 Feb 2016 11:38:10 +0000 (UTC) From: Markus Armbruster Date: Tue, 9 Feb 2016 12:37:52 +0100 Message-Id: <1455017883-25867-21-git-send-email-armbru@redhat.com> In-Reply-To: <1455017883-25867-1-git-send-email-armbru@redhat.com> References: <1455017883-25867-1-git-send-email-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 20/31] qapi: Don't cast Enum* to int* List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org From: Eric Blake 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, although using them changes the ABI of a binary. 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 using compiler ABI switches 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. But it is still better 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_QType(Visitor *v, QType *obj, const char *name, Error *= *errp) | { |- visit_type_enum(v, (int *)obj, QType_lookup, "QType", name, errp); |+ int value =3D *obj; |+ visit_type_enum(v, &value, QType_lookup, "QType", name, errp); |+ *obj =3D value; | } Signed-off-by: Eric Blake Reviewed-by: Marc-Andr=C3=A9 Lureau Message-Id: <1454075341-13658-17-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- 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 f98bb5f..ba75667 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -178,12 +178,13 @@ out: =20 =20 def gen_visit_enum(name): - # FIXME cast from enum *obj to int * invalidly assumes enum is int return mcgen(''' =20 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 value =3D *obj; + visit_type_enum(v, &value, %(c_name)s_lookup, "%(name)s", name, errp= ); + *obj =3D value; } ''', c_name=3Dc_name(name), name=3Dname) --=20 2.4.3