bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH] Define alignof_slot using _Alignof when using C11 or newer


From: Khem Raj
Subject: [PATCH] Define alignof_slot using _Alignof when using C11 or newer
Date: Sat, 14 Jan 2023 15:27:44 -0800

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]