From patchwork Thu Dec 1 03:58:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 85945 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp525398qgi; Wed, 30 Nov 2016 20:07:04 -0800 (PST) X-Received: by 10.237.35.140 with SMTP id j12mr31934491qtc.5.1480565224219; Wed, 30 Nov 2016 20:07:04 -0800 (PST) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 138si39426484qkf.261.2016.11.30.20.07.04 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 30 Nov 2016 20:07:04 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org Received: from localhost ([::1]:47987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cCIeF-00048H-Pi for patch@linaro.org; Wed, 30 Nov 2016 23:07:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55611) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cCIWR-0007Eo-DY for qemu-devel@nongnu.org; Wed, 30 Nov 2016 22:59:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cCIWO-0004QX-3S for qemu-devel@nongnu.org; Wed, 30 Nov 2016 22:58:59 -0500 Received: from mail.kernel.org ([198.145.29.136]:58530) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cCIWN-0004QJ-Q9 for qemu-devel@nongnu.org; Wed, 30 Nov 2016 22:58:56 -0500 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0378C20263; Thu, 1 Dec 2016 03:58:54 +0000 (UTC) Received: from redhat.com (pool-173-76-102-160.bstnma.fios.verizon.net [173.76.102.160]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 629522028D; Thu, 1 Dec 2016 03:58:52 +0000 (UTC) Date: Thu, 1 Dec 2016 05:58:51 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1480564455-23933-5-git-send-email-mst@redhat.com> References: <1480564455-23933-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1480564455-23933-1-git-send-email-mst@redhat.com> X-Mailer: git-send-email 2.8.0.287.g0deeb61 X-Mutt-Fcc: =sent X-Virus-Scanned: ClamAV using ClamSMTP X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 198.145.29.136 Subject: [Qemu-devel] [PULL 4/5] loader: fix undefined behavior in rom_order_compare() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Peter Crosthwaite , Alistair Francis , Stefan Hajnoczi , Paolo Bonzini , Laszlo Ersek Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Laszlo Ersek According to ISO C99 / N1256 (referenced in HACKING): > 6.5.8 Relational operators > > 4 For the purposes of these operators, a pointer to an object that is > not an element of an array behaves the same as a pointer to the first > element of an array of length one with the type of the object as its > element type. > > 5 When two pointers are compared, the result depends on the relative > locations in the address space of the objects pointed to. If two > pointers to object or incomplete types both point to the same object, > or both point one past the last element of the same array object, they > compare equal. If the objects pointed to are members of the same > aggregate object, pointers to structure members declared later compare > greater than pointers to members declared earlier in the structure, > and pointers to array elements with larger subscript values compare > greater than pointers to elements of the same array with lower > subscript values. All pointers to members of the same union object > compare equal. If the expression /P/ points to an element of an array > object and the expression /Q/ points to the last element of the same > array object, the pointer expression /Q+1/ compares greater than /P/. > In all other cases, the behavior is undefined. Our AddressSpace objects are allocated generally individually, and kept in the "address_spaces" linked list, so we mustn't compare their addresses with relops. Convert the pointers subjected to the relop in rom_order_compare() to "uintptr_t": > 7.18.1.4 Integer types capable of holding object pointers > > 1 [...] > > The following type designates an unsigned integer type with the > property that any valid pointer to void can be converted to this type, > then converted back to pointer to void, and the result will compare > equal to the original pointer: > > /uintptr_t/ > > These types are optional. Cc: "Michael S. Tsirkin" Cc: Alistair Francis Cc: Paolo Bonzini Cc: Peter Maydell Cc: qemu-devel@nongnu.org Fixes: 3e76099aacb4dae0d37ebf95305369e03d1491e6 Signed-off-by: Laszlo Ersek Reviewed-by: Alistair Francis Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/core/loader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- MST diff --git a/hw/core/loader.c b/hw/core/loader.c index c0d645a..4574249 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -818,7 +818,7 @@ static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms); static inline bool rom_order_compare(Rom *rom, Rom *item) { - return (rom->as > item->as) || + return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) || (rom->as == item->as && rom->addr >= item->addr); }