From patchwork Thu Nov 26 00:23:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 548860 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 81C181402D5 for ; Thu, 26 Nov 2015 11:24:04 +1100 (AEDT) Received: from localhost ([::1]:48401 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1kLx-00084V-SD for incoming@patchwork.ozlabs.org; Wed, 25 Nov 2015 19:24:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58437) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1kLR-00074Q-Mx for qemu-devel@nongnu.org; Wed, 25 Nov 2015 19:23:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1kLQ-0005pL-0H for qemu-devel@nongnu.org; Wed, 25 Nov 2015 19:23:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46144) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1kLP-0005op-RB for qemu-devel@nongnu.org; Wed, 25 Nov 2015 19:23:27 -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 6E570E7061; Thu, 26 Nov 2015 00:23:27 +0000 (UTC) Received: from red.redhat.com ([10.3.113.12]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAQ0NOdR020886; Wed, 25 Nov 2015 19:23:27 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 25 Nov 2015 17:23:01 -0700 Message-Id: <1448497401-27784-5-git-send-email-eblake@redhat.com> In-Reply-To: <1448497401-27784-1-git-send-email-eblake@redhat.com> References: <1448497401-27784-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v6 04/23] qapi: Don't cast Enum* to int* X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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. 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, earlier in "qapi: Simplify visiting of alternate types". Signed-off-by: Eric Blake --- 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 dc2a336..ddfb769 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -172,12 +172,13 @@ out: def gen_visit_enum(name): - # FIXME cast from enum *obj to int * invalidely 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 = *obj; + visit_type_enum(v, &tmp, %(c_name)s_lookup, "%(name)s", name, errp); + *obj = tmp; } ''', c_name=c_name(name), name=name)