From: Khem Raj <raj.khem@gmail.com>
To: bug-gnulib@gnu.org
Cc: Khem Raj <raj.khem@gmail.com>
Subject: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
Date: Sat, 14 Jan 2023 17:20:07 -0800 [thread overview]
Message-ID: <20230115012007.2175339-1-raj.khem@gmail.com> (raw)
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. Make exceptions for gcc < 4.9 and
clang < 8 where there is bug [3] in _Alignof implementation
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
[3] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
v1->v2:
- Exclude gcc < 4.9 and clang < 8 even though they may have C11 support
ChangeLog | 5 +++++
lib/alignof.h | 10 +++++++++-
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index fb467a3c14..33c0a0bb8a 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 barring GCC < 4.9 and clang < 8.
+
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..962c8f31f4 100644
--- a/lib/alignof.h
+++ b/lib/alignof.h
@@ -28,8 +28,16 @@
#if defined __cplusplus
template <class type> struct alignof_helper { char __slot1; type __slot2; };
# define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
-#else
+/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
+ clang versions < 8.0.0 have the same bug. */
+#elif (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
+ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
+ && !defined __clang__) \
+ || (defined __clang__ && __clang_major__ < 8))
# define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
+#else
+# define alignof_slot(type) _Alignof(type)
#endif
/* alignof_type (TYPE)
--
2.39.0
next reply other threads:[~2023-01-15 1:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-15 1:20 Khem Raj [this message]
2023-01-15 1:51 ` [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer Paul Eggert
2023-01-15 19:52 ` Paul Eggert
2023-01-15 19:56 ` Paul Eggert
2023-01-15 23:05 ` Khem Raj
2023-01-16 17:01 ` Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230115012007.2175339-1-raj.khem@gmail.com \
--to=raj.khem@gmail.com \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).