diff mbox series

[2/6] glib-2.0: Switch to using C11 std

Message ID 20230115185153.2277534-2-raj.khem@gmail.com
State New
Headers show
Series [1/6] libxcb: Fixed c2x standard undefined behaviour | expand

Commit Message

Khem Raj Jan. 15, 2023, 6:51 p.m. UTC
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 ...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

Comments

Khem Raj Jan. 16, 2023, 9:06 p.m. UTC | #1
upstream glib-2,0 has not accepted these patches so lets hold on to
them for now.

On Sun, Jan 15, 2023 at 10:51 AM Khem Raj <raj.khem@gmail.com> wrote:
>
> Signed-off-by: Khem Raj <raj.khem@gmail.com>
> ---
>  ...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
>
> diff --git a/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch b/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch
> new file mode 100644
> index 0000000000..4ddf49a9df
> --- /dev/null
> +++ b/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch
> @@ -0,0 +1,40 @@
> +From 59f4bdaedccb14802810e2f28646cc039a92f3d3 Mon Sep 17 00:00:00 2001
> +From: Khem Raj <raj.khem@gmail.com>
> +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 <raj.khem@gmail.com>
> +---
> + 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
> +
> diff --git a/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch b/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch
> new file mode 100644
> index 0000000000..c6f2ee1948
> --- /dev/null
> +++ b/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch
> @@ -0,0 +1,44 @@
> +From 3a86bfadd67983267e5d89b1a7e3a637041e599b Mon Sep 17 00:00:00 2001
> +From: Khem Raj <raj.khem@gmail.com>
> +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 <raj.khem@gmail.com>
> +---
> + 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
> +
> diff --git a/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb b/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
> index e5279e946c..de3f55e865 100644
> --- a/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
> +++ b/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
> @@ -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
>
diff mbox series

Patch

diff --git a/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch b/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch
new file mode 100644
index 0000000000..4ddf49a9df
--- /dev/null
+++ b/meta/recipes-core/glib-2.0/glib-2.0/0001-Switch-from-C99-to-C11-standard.patch
@@ -0,0 +1,40 @@ 
+From 59f4bdaedccb14802810e2f28646cc039a92f3d3 Mon Sep 17 00:00:00 2001
+From: Khem Raj <raj.khem@gmail.com>
+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 <raj.khem@gmail.com>
+---
+ 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
+
diff --git a/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch b/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch
new file mode 100644
index 0000000000..c6f2ee1948
--- /dev/null
+++ b/meta/recipes-core/glib-2.0/glib-2.0/0001-tests-macros.c-Define-check_alignof-for-std-c11-and-.patch
@@ -0,0 +1,44 @@ 
+From 3a86bfadd67983267e5d89b1a7e3a637041e599b Mon Sep 17 00:00:00 2001
+From: Khem Raj <raj.khem@gmail.com>
+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 <raj.khem@gmail.com>
+---
+ 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
+
diff --git a/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb b/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
index e5279e946c..de3f55e865 100644
--- a/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
+++ b/meta/recipes-core/glib-2.0/glib-2.0_2.74.4.bb
@@ -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"