From patchwork Tue Jan 31 20:19:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 722230 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 3vCd2K0mTWz9s65 for ; Wed, 1 Feb 2017 07:21:29 +1100 (AEDT) Received: from localhost ([::1]:40995 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYeve-0005Ov-FV for incoming@patchwork.ozlabs.org; Tue, 31 Jan 2017 15:21:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54150) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYetg-0003x5-0t for qemu-devel@nongnu.org; Tue, 31 Jan 2017 15:19:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cYetb-0004IC-4b for qemu-devel@nongnu.org; Tue, 31 Jan 2017 15:19:24 -0500 Received: from mail.kernel.org ([198.145.29.136]:52486) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cYeta-0004I6-VA for qemu-devel@nongnu.org; Tue, 31 Jan 2017 15:19:19 -0500 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4E6C620211; Tue, 31 Jan 2017 20:19:17 +0000 (UTC) Received: from redhat.com (pool-96-237-166-50.bstnma.fios.verizon.net [96.237.166.50]) (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 96A71201CE; Tue, 31 Jan 2017 20:19:15 +0000 (UTC) Date: Tue, 31 Jan 2017 22:19:13 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1485893872-26524-8-git-send-email-mst@redhat.com> References: <1485893872-26524-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1485893872-26524-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 v5 07/22] ARRAY_SIZE: check that argument is an array 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 , Sergey Fedorov , Markus Armbruster , Paolo Bonzini Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" It's a familiar pattern: some code uses ARRAY_SIZE, then refactoring changes the argument from an array to a pointer to a dynamically allocated buffer. Code keeps compiling but any ARRAY_SIZE calls now return the size of the pointer divided by element size. Let's add build time checks to ARRAY_SIZE before we allow more of these in the code-base. Signed-off-by: Michael S. Tsirkin Reviewed-by: Markus Armbruster Reviewed-by: Eric Blake --- include/qemu/osdep.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 689f253..56c9e22 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -198,8 +198,15 @@ extern int daemon(int, int); #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) #endif +/* + * &(x)[0] is always a pointer - if it's same type as x then the argument is a + * pointer, not an array. + */ +#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \ + typeof(&(x)[0]))) #ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \ + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x))) #endif int qemu_daemon(int nochdir, int noclose);