From patchwork Wed Jun 21 07:58:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicola Vetrini X-Patchwork-Id: 13286810 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A29DCEB64D8 for ; Wed, 21 Jun 2023 08:00:51 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.552515.862636 (Exim 4.92) (envelope-from ) id 1qBslU-0004vg-Kf; Wed, 21 Jun 2023 08:00:32 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 552515.862636; Wed, 21 Jun 2023 08:00:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qBslU-0004vZ-Hy; Wed, 21 Jun 2023 08:00:32 +0000 Received: by outflank-mailman (input) for mailman id 552515; Wed, 21 Jun 2023 08:00:31 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qBslT-0004vT-Rq for xen-devel@lists.xenproject.org; Wed, 21 Jun 2023 08:00:31 +0000 Received: from support.bugseng.com (mail.bugseng.com [162.55.131.47]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id b046c6fb-1009-11ee-b236-6b7b168915f2; Wed, 21 Jun 2023 10:00:30 +0200 (CEST) Received: from nico.bugseng.com (unknown [151.47.226.168]) by support.bugseng.com (Postfix) with ESMTPSA id 549BD4EE0737; Wed, 21 Jun 2023 10:00:16 +0200 (CEST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b046c6fb-1009-11ee-b236-6b7b168915f2 From: Nicola Vetrini To: xen-devel@lists.xenproject.org Cc: sstabellini@kernel.org, michal.orzel@amd.com, xenia.ragiadakou@amd.com, ayan.kumar.halder@amd.com, consulting@bugseng.com, jbeulich@suse.com, andrew.cooper3@citrix.com, roger.pau@citrix.com, bertrand.marquis@arm.com, julien@xen.org, Nicola Vetrini , George Dunlap , Wei Liu Subject: [XEN PATCH v2] xen/include: avoid undefined behavior. Date: Wed, 21 Jun 2023 09:58:15 +0200 Message-Id: <9d222cc83013aaa67b45638b27f5975b60aecb37.1687332385.git.nicola.vetrini@bugseng.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Redefine BUILD_BUG_ON_ZERO to fully comply with C99 avoiding undefined behavior 58 ("A structure or union is defined as containing no named members (6.7.2.1)." The chosen ill-formed construct is a negative bitwidth in a bitfield within a struct containing at least one named member, which prevents the UB while keeping the semantics of the construct for any memory layout of the struct (this motivates the "sizeof(unsigned) * 8" in the definition of the macro). Signed-off-by: Nicola Vetrini --- Changes in V2: - Avoid using a VLA as the compile-time assertion - Do not drop _Static_assert --- xen/include/xen/lib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index 67fc7c1d7e..e57d272772 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -51,9 +51,10 @@ e.g. in a structure initializer (or where-ever else comma expressions aren't permitted). */ #define BUILD_BUG_ON_ZERO(cond) \ - sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); }) + (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) - 1U) #else -#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); }) +#define BUILD_BUG_ON_ZERO(cond) \ + (sizeof(struct { unsigned u : (cond) ? -1 : sizeof(unsigned) * 8; }) - sizeof(unsigned)) #define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond)) #endif