libxcb: Fixed c2x standard undefined behaviour
PATCH 1/6 libxcb: Fixed c2x standard undefined behaviour
Signed-off-by Khem Raj <[email protected]>
--- ...-use-_Alignof-to-avoid-UB-in-ALIGNOF.patch | 45 +++++++++++++++++++ meta/recipes-graphics/xorg-lib/libxcb_1.15.bb | 3 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 meta/recipes-graphics/xorg-lib/libxcb/0001-use-_Alignof-to-avoid-UB-in-ALIGNOF.patch
meta/recipes-graphics/xorg-lib/libxcb/0001-use-_Alignof-to-avoid-UB-in-ALIGNOF.patch new file +45 -0
@@ -0,0 +1,45 @@ +From d55b6b1fa87700f3eae3a29522972d2e7be7d53e Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 10:11:35 -0800 +Subject: [PATCH] use _Alignof to avoid UB in ALIGNOF + +WG14 N2350 clearly says that it is an UB having type definitions +within "offsetof" [1]. Clang 16+ has started diagnosing it [2]. +This patch changes the implementation of macro +"ALIGNOF" to builtin "_Alignof" to avoid undefined behavior. + +_Alignof() return the ABI required minimum alignment. + +[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm +[2] https://reviews.llvm.org/D133574 + +Upstream-Status: Submitted [https://gitlab.freedesktop.org/xorg/lib/libxcb/-/merge_requests/42] +Signed-off-by: Khem Raj <[email protected]> +--- + src/c_client.py | 3 +-- + 1 file changed, 1 insertion(+), 2 deletions(-) + +diff --git a/src/c_client.py b/src/c_client.py +index fd256f0..4e48f13 100644 +--- a/src/c_client.py ++++ b/src/c_client.py +@@ -288,7 +288,6 @@ def c_open(self): + _c('#include "%s.h"', _ns.header) + + _c('') +- _c('#define ALIGNOF(type) offsetof(struct { char dummy; type member; }, member)') + + if _ns.is_ext: + for (n, h) in self.direct_imports: +@@ -1266,7 +1265,7 @@ def _c_serialize_helper_fields(context, self, + count += 1 + + code_lines.append( +- '%s xcb_align_to = ALIGNOF(%s);' ++ '%s xcb_align_to = _Alignof(%s);' + % (space, + 'char' + if field.c_field_type == 'void' or field.type.is_switch +-- +2.39.0 +
meta/recipes-graphics/xorg-lib/libxcb_1.15.bb +2 -1
@@ -9,7 +9,8 @@ SECTION = "x11/libs" LICENSE = "MIT" LIC_FILES_CHKSUM = "file://COPYING;md5=d763b081cb10c223435b01e00dc0aba7" -SRC_URI = "http://xcb.freedesktop.org/dist/libxcb-${PV}.tar.xz" +SRC_URI = "http://xcb.freedesktop.org/dist/libxcb-${PV}.tar.xz \ + file://0001-use-_Alignof-to-avoid-UB-in-ALIGNOF.patch" SRC_URI[sha256sum] = "cc38744f817cf6814c847e2df37fcb8997357d72fa4bcbc228ae0fe47219a059"
Signature · 1 line
2.39.0
PATCH 2/6 glib-2.0: Switch to using C11 std
Signed-off-by Khem Raj <[email protected]>
--- ...0001-Switch-from-C99-to-C11-standard.patch | 40 +++++++++++++++++ ...efine-check_alignof-for-std-c11-and-.patch | 44 +++++++++++++++++++ meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb | 2 + 3 files changed, 86 insertions(+) create mode 100644 meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch create mode 100644 meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch
meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch new file +40 -0
@@ -0,0 +1,40 @@ +From 59f4bdaedccb14802810e2f28646cc039a92f3d3 Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 12:15:29 -0800 +Subject: [PATCH] Switch from C99 to C11 standard + +We need to start using _Alignof consistently as the fallback +implementation which uses types within offsetof is UB as per WG14 N2350 +[1], moreover newer compilers like clang 16+ have started to error on +such use. + +Fixes errors like below with clang 16+ + +| ../glib-2.74.4/glib/ghash.c:299:18: error: 'struct (unnamed at ../glib-2.74.4/glib/ghash.c:299:1)' cannot be defined in '__builtin_offsetof' +| G_STATIC_ASSERT (G_ALIGNOF (GHashTableIter) >= G_ALIGNOF (RealIter)); + +[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm +[2] https://reviews.llvm.org/D133574 + +Upstream-Status: Submitted [https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3202] +Signed-off-by: Khem Raj <[email protected]> +--- + meson.build | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/meson.build b/meson.build +index 8d3500ad7..87662fa47 100644 +--- a/meson.build ++++ b/meson.build +@@ -5,7 +5,7 @@ project('glib', 'c', + default_options : [ + 'buildtype=debugoptimized', + 'warning_level=3', +- 'c_std=gnu99' ++ 'c_std=gnu11' + ] + ) + +-- +2.39.0 +
meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch new file +44 -0
44 lines not on this page · raw message
meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb +2 -0
@@ -19,6 +19,8 @@ SRC_URI = "${GNOME_MIRROR}/glib/${SHRT_VER}/glib-${PV}.tar.xz \ file://cpp-null.patch \ file://cpp-null2.patch \ file://fix-errno.patch \ + file://0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch \ + file://0001-Switch-from-C99-to-C11-standard.patch \ " SRC_URI:append:class-native = " file://relocate-modules.patch"
Signature · 1 line
2.39.0
Re: [PATCH 2/6] glib-2.0: Switch to using C11 std
upstream glib-2,0 has not accepted these patches so lets hold on to them for now.
Quoted 122 lines · Khem Raj
Signed-off-by Khem Raj <[email protected]>--- ...0001-Switch-from-C99-to-C11-standard.patch | 40 +++++++++++++++++ ...efine-check_alignof-for-std-c11-and-.patch | 44 +++++++++++++++++++ meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb | 2 + 3 files changed, 86 insertions(+) create mode 100644 meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch create mode 100644 meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patchmeta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch new file +40
-0@@ -0,0 +1,40 @@ +From 59f4bdaedccb14802810e2f28646cc039a92f3d3 Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 12:15:29 -0800 +Subject: [PATCH] Switch from C99 to C11 standard + +We need to start using _Alignof consistently as the fallback +implementation which uses types within offsetof is UB as per WG14 N2350 +[1], moreover newer compilers like clang 16+ have started to error on +such use. + +Fixes errors like below with clang 16+ + +| ../glib-2.74.4/glib/ghash.c:299:18: error: 'struct (unnamed at ../glib-2.74.4/glib/ghash.c:299:1)' cannot be defined in '__builtin_offsetof' +| G_STATIC_ASSERT (G_ALIGNOF (GHashTableIter) >= G_ALIGNOF (RealIter)); + +[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm +[2] https://reviews.llvm.org/D133574 + +Upstream-Status: Submitted [https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3202] +Signed-off-by: Khem Raj <[email protected]> +--- + meson.build | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/meson.build b/meson.build +index 8d3500ad7..87662fa47 100644 +--- a/meson.build ++++ b/meson.build +@@ -5,7 +5,7 @@ project('glib', 'c', + default_options : [ + 'buildtype=debugoptimized', + 'warning_level=3', +- 'c_std=gnu99' ++ 'c_std=gnu11' + ] + ) + +-- +2.39.0 +meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch new file +44
-0@@ -0,0 +1,44 @@ +From 3a86bfadd67983267e5d89b1a7e3a637041e599b Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 10:54:28 -0800 +Subject: [PATCH] tests/macros.c: Define check_alignof for std=c11 and newer + +WG14 N2350 made very clear that it is an UB having type definitions +within "offsetof" [1]. This patch changes the implementation of macro +check_alignof to builtin "_Alignof" to avoid undefined behavior. + +clang 16+ has started to diagnose 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 + +Upstream-Status: Submitted [https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3201] +Signed-off-by: Khem Raj <[email protected]> +--- + glib/tests/macros.c | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/glib/tests/macros.c b/glib/tests/macros.c +index efe632b..17b1646 100644 +--- a/glib/tests/macros.c ++++ b/glib/tests/macros.c +@@ -42,9 +42,13 @@ test_assert_static (void) + static void + test_alignof_fallback (void) + { ++#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus) ++#define check_alignof(type) \ ++ g_assert_cmpint (G_ALIGNOF (type), ==, _Alignof (type)) ++#else + #define check_alignof(type) \ + g_assert_cmpint (G_ALIGNOF (type), ==, G_STRUCT_OFFSET (struct { char a; type b; }, b)) +- ++#endif + check_alignof (char); + check_alignof (int); + check_alignof (float); +-- +2.39.0 +meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb +2
-1@@ -19,6 +19,8 @@ SRC_URI = "${GNOME_MIRROR}/glib/${SHRT_VER}/glib-${PV}.tar.xz \ file://cpp-null.patch \ file://cpp-null2.patch \ file://fix-errno.patch \ + file://0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch \ + file://0001-Switch-from-C99-to-C11-standard.patch \ " SRC_URI:append:class-native = " file://relocate-modules.patch" --2.39.0
PATCH 3/6 dbus: Use Alignof when using C11 or newer standard
In reply to Khem RajFixes an UB found with with clang
Signed-off-by Khem Raj <[email protected]>
--- ...GNOF-using-_Alignof-when-using-C11-o.patch | 42 +++++++++++++++++++ meta/recipes-core/dbus/dbus_1.14.4.bb | 1 + 2 files changed, 43 insertions(+) create mode 100644 meta/recipes-core/dbus/dbus/0001-Define-_DBUS_ALIGNOF-using-_Alignof-when-using-C11-o.patch
meta/recipes-core/dbus/dbus/0001-Define-_DBUS_ALIGNOF-using-_Alignof-when-using-C11-o.patch new file +42 -0
@@ -0,0 +1,42 @@ +From c3e2c873ffa8e89b5f83dccc4e4e35dfcea633cb Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 12:39:43 -0800 +Subject: [PATCH] Define _DBUS_ALIGNOF using _Alignof when using C11 or newer + +WG14 N2350 made very clear that it is an UB having type definitions +within "offsetof" [1]. This patch changes the implementation of macro +_DBUS_ALIGNOF to builtin "_Alignof" to avoid undefined behavior. + +clang 16+ has started to diagnose 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 + +Upstream-Status: Submitted [https://gitlab.freedesktop.org/dbus/dbus/-/merge_requests/389] +Signed-off-by: Khem Raj <[email protected]> +--- + dbus/dbus-internals.h | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h +index cc98c92a..2387752d 100644 +--- a/dbus/dbus-internals.h ++++ b/dbus/dbus-internals.h +@@ -201,8 +201,12 @@ void _dbus_real_assert_not_reached (const char *explanation, + ((intptr_t) ((unsigned char*) &((struct_type*) 0)->member)) + #endif + ++#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus) ++#define _DBUS_ALIGNOF(type) _Alignof(type) ++#else + #define _DBUS_ALIGNOF(type) \ + (_DBUS_STRUCT_OFFSET (struct { char _1; type _2; }, _2)) ++#endif + + #if defined(DBUS_DISABLE_CHECKS) || defined(DBUS_DISABLE_ASSERT) + /* this is an assert and not an error, but in the typical --disable-checks case (you're trying +-- +2.39.0 +
meta/recipes-core/dbus/dbus_1.14.4.bb +1 -0
@@ -13,6 +13,7 @@ LIC_FILES_CHKSUM = "file://COPYING;md5=6423dcd74d7be9715b0db247fd889da3 \ SRC_URI = "https://dbus.freedesktop.org/releases/dbus/dbus-${PV}.tar.xz \ file://run-ptest \ file://tmpdir.patch \ + file://0001-Define-_DBUS_ALIGNOF-using-_Alignof-when-using-C11-o.patch \ file://dbus-1.init \ "
Signature · 1 line
2.39.0
PATCH 4/6 m4: Define alignof_slot using _Alignof when using C11 or newer
In reply to Khem RajSigned-off-by Khem Raj <[email protected]>
--- meta/recipes-devtools/m4/m4-1.4.19.inc | 1 + ...lot-using-_Alignof-when-using-C11-or.patch | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 meta/recipes-devtools/m4/m4/0001-Define-alignof_slot-using-_Alignof-when-using-C11-or.patch
meta/recipes-devtools/m4/m4-1.4.19.inc +1 -0
@@ -9,6 +9,7 @@ inherit autotools texinfo ptest gettext SRC_URI = "${GNU_MIRROR}/m4/m4-${PV}.tar.gz \ file://ac_config_links.patch \ file://0001-sigsegv-Fix-build-on-ppc-musl.patch \ + file://0001-Define-alignof_slot-using-_Alignof-when-using-C11-or.patch \ " SRC_URI:append:class-target = " file://run-ptest \ file://serial-tests-config.patch \
meta/recipes-devtools/m4/m4/0001-Define-alignof_slot-using-_Alignof-when-using-C11-or.patch new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH 5/6 opkg: Define alignof using _Alignof when using C11 or newer
In reply to Khem RajSigned-off-by Khem Raj <[email protected]>
--- ...sing-_Alignof-when-using-C11-or-newe.patch | 51 +++++++++++++++++++ meta/recipes-devtools/opkg/opkg_0.6.1.bb | 1 + 2 files changed, 52 insertions(+) create mode 100644 meta/recipes-devtools/opkg/opkg/0001-Define-alignof-using-_Alignof-when-using-C11-or-newe.patch
meta/recipes-devtools/opkg/opkg/0001-Define-alignof-using-_Alignof-when-using-C11-or-newe.patch new file +51 -0
@@ -0,0 +1,51 @@ +From 4089affd371e6d62dd8c1e57b344f8cc329005ea Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sat, 14 Jan 2023 23:11:08 -0800 +Subject: [PATCH] Define alignof using _Alignof when using C11 or newer + +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+ + +Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it +may support C11, exclude those compilers too + +[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm +[2] https://reviews.llvm.org/D133574 + +Upstream-Status: Submitted [https://groups.google.com/g/opkg-devel/c/gjcQPZgT_jI] +Signed-off-by: Khem Raj <[email protected]> +--- + libopkg/md5.c | 10 ++++++++++ + 1 file changed, 10 insertions(+) + +diff --git a/libopkg/md5.c b/libopkg/md5.c +index 981b9b8..ccb645e 100644 +--- a/libopkg/md5.c ++++ b/libopkg/md5.c +@@ -237,7 +237,17 @@ void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx) + /* Process available complete blocks. */ + if (len >= 64) { + #if !_STRING_ARCH_unaligned ++/* 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. */ ++#if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ ++ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ ++ && !defined __clang__) \ ++ || (defined __clang__ && __clang_major__ < 8)) + #define alignof(type) offsetof (struct { char c; type x; }, x) ++#else ++#define alignof(type) _Alignof(type) ++#endif + #define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) + if (UNALIGNED_P(buffer)) + while (len > 64) { +-- +2.39.0 +
meta/recipes-devtools/opkg/opkg_0.6.1.bb +1 -0
@@ -16,6 +16,7 @@ SRC_URI = "http://downloads.yoctoproject.org/releases/${BPN}/${BPN}-${PV}.tar.gz file://opkg.conf \ file://0001-opkg_conf-create-opkg.lock-in-run-instead-of-var-run.patch \ file://0002-opkg-key-remove-no-options-flag-from-gpg-calls.patch \ + file://0001-Define-alignof-using-_Alignof-when-using-C11-or-newe.patch \ file://run-ptest \ "
Signature · 1 line
2.39.0
PATCH 6/6 cpio: Fix gnulib alignof implementation on clang
In reply to Khem RajBackport needed patches
Signed-off-by Khem Raj <[email protected]>
--- .../0001-Use-__alignof__-with-clang.patch | 42 +++++++++++++++++++ meta/recipes-extended/cpio/cpio_2.13.bb | 1 + 2 files changed, 43 insertions(+) create mode 100644 meta/recipes-extended/cpio/cpio-2.13/0001-Use-__alignof__-with-clang.patch
meta/recipes-extended/cpio/cpio-2.13/0001-Use-__alignof__-with-clang.patch new file +42 -0
@@ -0,0 +1,42 @@ +From 6e169bb5fadb73b4dd300b28e9cae1e1772855e8 Mon Sep 17 00:00:00 2001 +From: Khem Raj <[email protected]> +Date: Sun, 15 Jan 2023 02:05:23 -0800 +Subject: [PATCH] Use __alignof__ with clang. + +For clang versions < 8, use the same +workaround as for GCC versions < 4.9. + +Backport couple of gnulib patches squashed + +[1] https://git.savannah.gnu.org/cgit/gnulib.git/commit/lib/stdalign.in.h?id=57caf9541d98314102a55dd1cd0b8dc2be747471 +[2] https://git.savannah.gnu.org/cgit/gnulib.git/commit/lib/stdalign.in.h?id=2af490a4c4ce85567296e27be6a9528964ca851e + +Upstream-Status: Backport [https://git.savannah.gnu.org/cgit/gnulib.git/commit/lib/stdalign.in.h?id=57caf9541d98314102a55dd1cd0b8dc2be747471] + +Signed-off-by: Khem Raj <[email protected]> +--- + gnu/stdalign.in.h | 7 +++++-- + 1 file changed, 5 insertions(+), 2 deletions(-) + +diff --git a/gnu/stdalign.in.h b/gnu/stdalign.in.h +index 5f56eee..e6e6a65 100644 +--- a/gnu/stdalign.in.h ++++ b/gnu/stdalign.in.h +@@ -53,9 +53,12 @@ + #undef _Alignof + + /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023 +- <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. */ ++ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. ++ clang versions < 8.0.0 have the same bug. */ + #if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ +- || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9))) ++ || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ ++ && !defined __clang__) \ ++ || (defined __clang__ && __clang_major__ < 8)) + # ifdef __cplusplus + # if 201103 <= __cplusplus + # define _Alignof(type) alignof (type) +-- +2.39.0 +
meta/recipes-extended/cpio/cpio_2.13.bb +1 -0
@@ -11,6 +11,7 @@ SRC_URI = "${GNU_MIRROR}/cpio/cpio-${PV}.tar.gz \ file://0002-src-global.c-Remove-superfluous-declaration-of-progr.patch \ file://0001-obstack-Fix-a-clang-warning.patch \ file://CVE-2021-38185.patch \ + file://0001-Use-__alignof__-with-clang.patch \ " SRC_URI[md5sum] = "389c5452d667c23b5eceb206f5000810"
Signature · 1 line
2.39.0