File 5097.patch of Package glib2
From 9061ab2543e0486edeb78c3cc0ef5369cc5e40a1 Mon Sep 17 00:00:00 2001
From: Philip Withnall <pwithnall@gnome.org>
Date: Fri, 13 Mar 2026 10:14:04 +0000
Subject: [PATCH] gslice: Factor out a size helper macro and stop using MAX in
a public header
While we _should_ be able to use `MAX()` in a public header, it appears
that at least NetworkManager undefines it, causing itself compilation
failures.
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/5063/diffs#note_2705330
Take the opportunity to clarify the size logic by factoring it out to an
internal (private) helper macro, adding a comment, and open-coding the
arithmetic rather than using `MAX()`.
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
---
glib/gslice.h | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/glib/gslice.h b/glib/gslice.h
index 3c33c84c7b..142535b712 100644
--- a/glib/gslice.h
+++ b/glib/gslice.h
@@ -45,21 +45,29 @@ GLIB_AVAILABLE_IN_ALL
void g_slice_free_chain_with_offset (gsize block_size,
gpointer mem_chain,
gsize next_offset);
-#define g_slice_new(type) ((type*) g_slice_alloc (MAX (sizeof (type), 1)))
+
+#ifndef __GI_SCANNER__
+/* Private helper to give the allocation size for a struct — we want to
+ * guarantee non-empty allocations, so it returns a minimum of 1B. This is
+ * necessary because some compilers do allow empty (zero size) structs. */
+#define g_slice_alloc_size(type) (sizeof (type) > 0 ? sizeof (type) : 1)
+#endif
+
+#define g_slice_new(type) ((type*) g_slice_alloc (g_slice_alloc_size (type)))
/* Allow the compiler to inline memset(). Since the size is a constant, this
* can significantly improve performance. */
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
# define g_slice_new0(type) \
(type *) (G_GNUC_EXTENSION ({ \
- gsize __s = MAX (sizeof (type), 1); \
+ gsize __s = g_slice_alloc_size (type); \
gpointer __p; \
__p = g_slice_alloc (__s); \
memset (__p, 0, __s); \
__p; \
}))
#else
-# define g_slice_new0(type) ((type*) g_slice_alloc0 (MAX (sizeof (type), 1)))
+# define g_slice_new0(type) ((type*) g_slice_alloc0 (g_slice_alloc_size (type)))
#endif
/* MemoryBlockType *
@@ -76,7 +84,7 @@ void g_slice_free_chain_with_offset (gsize block_size,
/* we go through extra hoops to ensure type safety */
#define g_slice_dup(type, mem) \
- (1 ? (type*) g_slice_copy (MAX (sizeof (type), 1), (mem)) \
+ (1 ? (type*) g_slice_copy (g_slice_alloc_size (type), (mem)) \
: ((void) ((type*) 0 == (mem)), (type*) 0))
#define g_slice_free(type, mem) \
G_STMT_START { \
--
GitLab