blob: d48d7265d401d2a573c9aa3f3080a72c6373b158 [file] [log] [blame]
Andrew Geisslerc5535c92023-01-27 16:10:19 -06001From 9e9d94566d39eef3e4606f806aa418bf5534fab9 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sun, 15 Jan 2023 22:04:31 -0800
4Subject: [PATCH 1/2] Define alignof using _Alignof when using C11 or newer
5
6WG14 N2350 made very clear that it is an UB having type definitions
7within "offsetof" [1]. This patch enhances the implementation of macro
8alignof to use builtin "_Alignof" to avoid undefined behavior on
9when using std=c11 or newer
10
11clang 16+ has started to flag this [2]
12
13Fixes build when using -std >= gnu11 and using clang16+
14
15Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it
16may support C11, exclude those compilers too
17
18[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
19[2] https://reviews.llvm.org/D133574
20
21Upstream-Status: Pending
22
23Signed-off-by: Khem Raj <raj.khem@gmail.com>
24---
25 Misc/md5-coreutils.c | 12 +++++++++++-
26 Misc/sha1.c | 12 +++++++++++-
27 Misc/sha256.c | 12 +++++++++++-
28 Misc/sha512.c | 12 +++++++++++-
29 4 files changed, 44 insertions(+), 4 deletions(-)
30
31diff --git a/Misc/md5-coreutils.c b/Misc/md5-coreutils.c
32index d6503e02..2ffb6050 100644
33--- a/Misc/md5-coreutils.c
34+++ b/Misc/md5-coreutils.c
35@@ -154,7 +154,17 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
36 if (len >= 64)
37 {
38 #if !_STRING_ARCH_unaligned
39-# define alignof(type) offsetof (struct { char c; type x; }, x)
40+/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
41+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
42+ clang versions < 8.0.0 have the same bug. */
43+# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
44+ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
45+ && !defined __clang__) \
46+ || (defined __clang__ && __clang_major__ < 8))
47+# define alignof(type) offsetof (struct { char c; type x; }, x)
48+# else
49+# define alignof(type) _Alignof(type)
50+# endif
51 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
52 if (UNALIGNED_P (buffer))
53 while (len > 64)
54diff --git a/Misc/sha1.c b/Misc/sha1.c
55index 18ceb845..a170efe3 100644
56--- a/Misc/sha1.c
57+++ b/Misc/sha1.c
58@@ -149,7 +149,17 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
59 if (len >= 64)
60 {
61 #if !_STRING_ARCH_unaligned
62-# define alignof(type) offsetof (struct { char c; type x; }, x)
63+/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
64+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
65+ clang versions < 8.0.0 have the same bug. */
66+# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
67+ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
68+ && !defined __clang__) \
69+ || (defined __clang__ && __clang_major__ < 8))
70+# define alignof(type) offsetof (struct { char c; type x; }, x)
71+# else
72+# define alignof(type) _Alignof(type)
73+# endif
74 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
75 if (UNALIGNED_P (buffer))
76 while (len > 64)
77diff --git a/Misc/sha256.c b/Misc/sha256.c
78index 68292326..da59e81d 100644
79--- a/Misc/sha256.c
80+++ b/Misc/sha256.c
81@@ -372,7 +372,17 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
82 if (len >= 64)
83 {
84 #if !_STRING_ARCH_unaligned
85-# define alignof(type) offsetof (struct { char c; type x; }, x)
86+/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
87+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
88+ clang versions < 8.0.0 have the same bug. */
89+# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
90+ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
91+ && !defined __clang__) \
92+ || (defined __clang__ && __clang_major__ < 8))
93+# define alignof(type) offsetof (struct { char c; type x; }, x)
94+# else
95+# define alignof(type) _Alignof(type)
96+# endif
97 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
98 if (UNALIGNED_P (buffer))
99 while (len > 64)
100diff --git a/Misc/sha512.c b/Misc/sha512.c
101index db86c659..38e162fc 100644
102--- a/Misc/sha512.c
103+++ b/Misc/sha512.c
104@@ -190,7 +190,17 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
105 if (len >= 128)
106 {
107 #if !_STRING_ARCH_unaligned
108-# define alignof(type) offsetof (struct { char c; type x; }, x)
109+/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
110+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
111+ clang versions < 8.0.0 have the same bug. */
112+# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
113+ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
114+ && !defined __clang__) \
115+ || (defined __clang__ && __clang_major__ < 8))
116+# define alignof(type) offsetof (struct { char c; type x; }, x)
117+# else
118+# define alignof(type) _Alignof(type)
119+# endif
120 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint64_t) != 0)
121 if (UNALIGNED_P (buffer))
122 while (len > 128)
123--
1242.39.0
125