bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] Define alignof_slot using _Alignof when using C11 or newer
@ 2023-01-14 23:27 Khem Raj
  2023-01-15  0:17 ` Bruno Haible
  0 siblings, 1 reply; 4+ messages in thread
From: Khem Raj @ 2023-01-14 23:27 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Khem Raj

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
alignof_slot to use builtin "_Alignof" to avoid undefined behavior on
when using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 ChangeLog     | 5 +++++
 lib/alignof.h | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index fb467a3c14..71d7f7b7f6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2023-01-15  Khem Raj  <raj.khem@gmail.com>
+
+	* lib/alignof.h (alignof_slot): Use _Alignof when using C11 or newer
+	standard.
+
 2023-01-14  Bruno Haible  <bruno@clisp.org>
 
 	error, verror tests: Fix link error when the package uses libintl.
diff --git a/lib/alignof.h b/lib/alignof.h
index 505ad97aa4..66a9496518 100644
--- a/lib/alignof.h
+++ b/lib/alignof.h
@@ -28,6 +28,8 @@
 #if defined __cplusplus
   template <class type> struct alignof_helper { char __slot1; type __slot2; };
 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
+#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
+# define alignof_slot(type) _Alignof(type)
 #else
 # define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
 #endif
-- 
2.39.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-14 23:27 [PATCH] Define alignof_slot using _Alignof when using C11 or newer Khem Raj
@ 2023-01-15  0:17 ` Bruno Haible
  2023-01-15  1:20   ` Khem Raj
  2023-01-15  1:59   ` Paul Eggert
  0 siblings, 2 replies; 4+ messages in thread
From: Bruno Haible @ 2023-01-15  0:17 UTC (permalink / raw)
  To: bug-gnulib, Khem Raj

Khem Raj wrote:
> [2] https://reviews.llvm.org/D133574

Thanks for the heads-up.

> WG14 N2350 made very clear that it is an UB having type definitions
> within "offsetof" [1].
> [1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm

This document is not normative; it is merely a discussion document.

What's (quasi) normative is

  * n3047.pdf = C23, which says in 7.21.(3)
    "If the specified type defines a new type or if the specified member is
     a bit-field, the behavior is undefined."

  * n2176.pdf = C18, which in 7.19.(3) merely says
    "If the specified member is a bit-field, the behavior is undefined."

So, only C23 and higher require to avoid offsetof with a new type.

Since gnulib/lib/stdalign.h mentions an _Alignof bug in older versions of
GCC and clang and some of these older versions (e.g. GCC 4.8.5) support C11,
it is better to enable the workaround only for C23 and higher, not for
C11 and higher.

> +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)

The '!defined(__cplusplus)' part is redundant here.

Bruno





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15  0:17 ` Bruno Haible
@ 2023-01-15  1:20   ` Khem Raj
  2023-01-15  1:59   ` Paul Eggert
  1 sibling, 0 replies; 4+ messages in thread
From: Khem Raj @ 2023-01-15  1:20 UTC (permalink / raw)
  To: Bruno Haible; +Cc: bug-gnulib

On Sat, Jan 14, 2023 at 4:17 PM Bruno Haible <bruno@clisp.org> wrote:
>
> Khem Raj wrote:
> > [2] https://reviews.llvm.org/D133574
>
> Thanks for the heads-up.
>
> > WG14 N2350 made very clear that it is an UB having type definitions
> > within "offsetof" [1].
> > [1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
>
> This document is not normative; it is merely a discussion document.
>
> What's (quasi) normative is
>
>   * n3047.pdf = C23, which says in 7.21.(3)
>     "If the specified type defines a new type or if the specified member is
>      a bit-field, the behavior is undefined."
>
>   * n2176.pdf = C18, which in 7.19.(3) merely says
>     "If the specified member is a bit-field, the behavior is undefined."
>
> So, only C23 and higher require to avoid offsetof with a new type.
>
> Since gnulib/lib/stdalign.h mentions an _Alignof bug in older versions of
> GCC and clang and some of these older versions (e.g. GCC 4.8.5) support C11,
> it is better to enable the workaround only for C23 and higher, not for
> C11 and higher.
>

I think if its enabled only for c2x and newer, clang will still errors out  for
__STDC_VERSION__= 201710L
I have sent out a patch which replicates the logic from
gnulib/lib/stdalign.h.in which should
address this as well.

> > +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
>
> The '!defined(__cplusplus)' part is redundant here.
>

right. Dropped in v2.

> Bruno
>
>
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15  0:17 ` Bruno Haible
  2023-01-15  1:20   ` Khem Raj
@ 2023-01-15  1:59   ` Paul Eggert
  1 sibling, 0 replies; 4+ messages in thread
From: Paul Eggert @ 2023-01-15  1:59 UTC (permalink / raw)
  To: Bruno Haible, bug-gnulib, Khem Raj

On 2023-01-14 16:17, Bruno Haible wrote:
> What's (quasi) normative is
> 
>    * n3047.pdf = C23, which says in 7.21.(3)
>      "If the specified type defines a new type or if the specified member is
>       a bit-field, the behavior is undefined."

I'm afraid this wording in n3047 is unclear. Ordinarily one would look 
at n3047's section "Type definitions" to interpret the phrase "defines a 
new type", but as that section merely talks about typedefs it doesn't 
seem to be relevant here. I can't find any part of the standard that 
talks about "defining a new type".

All the standardizers wanted here is for the TYPE to not contain a 
top-level comma in 'offsetof (TYPE, MEMBER)' because that would screw up 
macro argument identification.

I hope Clang 16 is not reporting an error for 'offsetof (struct {char a; 
int b;}, b)'; if so, it's gone off the deep end. A nameless struct does 
not "define a new type" for some reasonable definitions of the term 
"define a new type", and since the standard doesn't define that term the 
Clang developers should be cautious about diagnostics in this messy area 
when a lot of code out there uses offsetof in precisely this way.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-15  1:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-14 23:27 [PATCH] Define alignof_slot using _Alignof when using C11 or newer Khem Raj
2023-01-15  0:17 ` Bruno Haible
2023-01-15  1:20   ` Khem Raj
2023-01-15  1:59   ` Paul Eggert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).