From e87144dcd13cd675d433436b47b6c2af523f660c Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 2 Mar 2025 15:57:14 -0800 Subject: [PATCH] Pacify GCC in pgtkselect malloc alignment This is a better fix for Bug#76414. * src/pgtkselect.c (pgtk_nalloc): New function. (pgtk_get_window_property, lisp_data_to_selection_data): Use it. (cherry picked from commit 76342efe9d758a6ca66cdc0ed197381664a6fbbd) --- src/pgtkselect.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/pgtkselect.c b/src/pgtkselect.c index c05594d7366..7e314df8921 100644 --- a/src/pgtkselect.c +++ b/src/pgtkselect.c @@ -57,6 +57,17 @@ static void lisp_data_to_selection_data (struct pgtk_display_info *, Lisp_Object static Lisp_Object pgtk_get_local_selection (Lisp_Object, Lisp_Object, bool, struct pgtk_display_info *); +/* Allocate an array of NITEMS items, each of positive size ITEM_SIZE. + Make room for an extra byte at the end, as GDK sometimes needs that. */ + +static void * +pgtk_nalloc (ptrdiff_t nitems, ptrdiff_t item_size) +{ + /* To pacify gcc --Wanalyzer-allocation-size, make room for an extra + item at the end instead of just the extra byte GDK sometimes needs. */ + return xnmalloc (nitems + 1, item_size); +} + /* From a Lisp_Object, return a suitable frame for selection operations. OBJECT may be a frame, a terminal object, or nil (which stands for the selected frame--or, if that is not an pgtk @@ -1129,7 +1140,7 @@ pgtk_get_window_property (GdkWindow *window, unsigned char **data_ret, eassert (actual_format == 32); length = length / sizeof (GdkAtom); - xdata = xmalloc (sizeof (GdkAtom) * length + 1); + xdata = pgtk_nalloc (length, sizeof (GdkAtom)); memcpy (xdata, data, 1 + length * sizeof (GdkAtom)); g_free (data); @@ -1145,10 +1156,7 @@ pgtk_get_window_property (GdkWindow *window, unsigned char **data_ret, element_size = pgtk_size_for_format (actual_format); length = length / element_size; - - /* Add an extra byte on the end. GDK guarantees that it is - NULL. */ - xdata = xmalloc (1 + element_size * length); + xdata = pgtk_nalloc (length, element_size); memcpy (xdata, data, 1 + element_size * length); if (actual_format == 32 && LONG_WIDTH > 32) @@ -1437,7 +1445,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo, } else if (SYMBOLP (obj)) { - void *data = xmalloc (sizeof (GdkAtom) + 1); + void *data = pgtk_nalloc (1, sizeof (GdkAtom)); GdkAtom *x_atom_ptr = data; cs->data = data; cs->format = 32; @@ -1448,7 +1456,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo, } else if (RANGED_FIXNUMP (SHRT_MIN, obj, SHRT_MAX)) { - void *data = xmalloc (sizeof (short) + 1); + void *data = pgtk_nalloc (1, sizeof (short)); short *short_ptr = data; cs->data = data; cs->format = 16; @@ -1463,7 +1471,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo, || (CONSP (XCDR (obj)) && FIXNUMP (XCAR (XCDR (obj))))))) { - void *data = xmalloc (sizeof (unsigned long) + 1); + void *data = pgtk_nalloc (1, sizeof (unsigned long)); unsigned long *x_long_ptr = data; cs->data = data; cs->format = 32; -- 2.39.5