[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [XEN PATCH v2] xen/include: avoid undefined behavior.


  • To: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Wed, 21 Jun 2023 10:48:39 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=6cAMGeDsrb9bQblAXcAKJAqLCzbkEboAE1EDV4YvyD4=; b=aYl2JnJ6ucF63ZetHsH571sh6pi3VbM5U1M+FP/fYT7C8ZRFC7v4EV+xCP7Qx7rw+TZwQAoC2spyCF2SQYmROm1NydavbzkRGgVDfaoIYOnvSgq8TWhcLVO8DiPGllUdHMh4JHdDUCHmyVRB+Iht59xE/cjEUwH5C3vXYQMXhqiVVtEd2FKgoQQ6/AjkZbHyHjO9Fb2wuunsGEnUcdbLE7B1ydBX3IgbEmgZiUgIgabYjy7lYmMiZ/qBIyrz20Xu2r1ZNydjXEhHd4LiToLHB9g/On7EKYZ4vKb6Wd4SdD50ohBz0iFCouSolkDNkFrSqcWTi36Uf1grzNuyzHTQ3w==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JHbcDqrRP31/rPXHpM4xrTy7P9A2dGWn2EMPGGRNUE3vdITChSq5gxfXvc8L3i8qEjVEVdn+Kqs2CFSlU/egtOky00SG7Fu+XgW8eaAs/Gtw+UvhI+7fs81rur0PeBeZoQzHDeUbmwqZVpeQrQxGxjfSaVJA1SmjTmv6pZIVheSiemO95DYXY3b9MLoTVTv8McL4pGYDUlhNBk+SzJRbRPlUJfPygwGVqTc3QyAEklASS3uDWgUT4ItuQGoBTMaMzJeQX1zOLFJKXjcUW60P5qHfc6hzk9MucvyZUJn6rl7oMAvWtiDr29scGjVruRVm36xWTWTrUQQQe3v6bA1X9g==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: sstabellini@xxxxxxxxxx, michal.orzel@xxxxxxx, xenia.ragiadakou@xxxxxxx, ayan.kumar.halder@xxxxxxx, consulting@xxxxxxxxxxx, andrew.cooper3@xxxxxxxxxx, roger.pau@xxxxxxxxxx, bertrand.marquis@xxxxxxx, julien@xxxxxxx, George Dunlap <george.dunlap@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Wed, 21 Jun 2023 08:49:21 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 21.06.2023 09:58, Nicola Vetrini wrote:
> 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)."

Here and in the title I'm not happy about you referencing undefined
behavior. What we do here is use a well-known compiler extension (and I'm
sure you're aware we do so elsewhere, where it's actually going to remain
as is from all I can tell right now).

> --- 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)

To be compatible with whatever odd ABIs new ports may have, maybe better to
AND or multiply with 0? (I also don't think a U suffix is warranted here.)

>  #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))

What's wrong with just giving the bitfield a name:

#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int _:-!!(cond); })

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.