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

Re: [XEN PATCH v4] xen/include: avoid using a compiler extension for BUILD_BUG_ON_ZERO.


  • To: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Fri, 23 Jun 2023 19:16:35 +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=RJdcH9Rc9I1M6THLdWbipQkPRMWtGnku4q28KEcybq8=; b=VytVqe+XKtUqm5Es8Uabx3G9aB2p/D74CEEOdcQSGEhWy/EvTWikZg+8gyq6m/BAiE3+MtKwjjOZLh0ahyYXs8xSpQllV7AtmCTWctLdfJNnMTG7Q6435qsOq0cYvynJDd04ZFo9SHz8iIiohqOhV2FGlft6uKXwv8ugSm52J7lUrw6UHAjGA3eXQXLRfI0p5iw9zVqTX/s94LR6QGMTD7yZEAJ89tkYN4r3FWUmvJuen+Ox3UmTGghC5yVhaZeBlnlGXVMAOKzYvTHz4ygBQjfyRka/Y+G0N/lb/zvRfLl1QihyVx654Xdoa4owCdAetUi4isVj8GV/mD6Czsechg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jg3ws51eWzSq77WSj2SuYRYMCoAcbxMHhAurgZAEPgi+thz9W8siUMPVfzIWDfl9VUHbqkBEFg931P5enRIQoAx7ZoTX43UktJB1rbutSuTGWrfWD2AienJnkBxtrSkhOeqLie7+Jp7//1ra8lUMpJbG4EmcExF4OQsj5A7hT5rVG00Tjn6wdlSDAh22gattRe9xlDWgHKq38wF1zedkekd790/Sw3pKzOkqTn8bm/PDFfICXzg1/ePdAqv7tfB+rJboh+Gx/mt7qltrlV4Fh/5AtqTjCyUm0LIkLeol74D8eF53bZk+MHjtUFEew22GOE+KYMs02PfFvN5r+eEbSg==
  • 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: Fri, 23 Jun 2023 17:16:52 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 23.06.2023 10:59, Nicola Vetrini wrote:
> Redefine BUILD_BUG_ON_ZERO to avoid using a compiler extension
> that gives an acceptable semantics to C99 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.
> 
> The choice of the bitwise AND operation to bring the result to 0
> when cond is false boils down to possibly better portability,
> and the 'U' suffix to make it obvious that this operation results
> in an unsigned value.
> 
> Signed-off-by: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
> ---
> Changes in V2:
> - Avoid using a VLA as the compile-time assertion
> - Do not drop _Static_assert
> Changes in V3:
> - Changed the operation to bring the result to 0 when the
> construct does not lead to a compilation error
> Changes in V4:
> - Switched to a shorter construct for the second definition.

Which sadly renders part of the description inapplicable now (there's
no negative width bitfield anymore, afaics). Could probably be swapped
for "zero" while committing, if some other maintainer wants to ack it
in its present form. I'm not happy to, with the continued use of the
two U suffixes. It may seem minor, but to me it feels like setting a
bad precedent.

Jan

> --- 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 ")"); }) & 
> 0U)
>  #else
> -#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); })
> +#define BUILD_BUG_ON_ZERO(cond) \
> +    (sizeof(struct { unsigned u : !(cond); }) & 0U)
>  #define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
>  #endif
>  




 


Rackspace

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