aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2025-01-26 22:15:49 -0800
committerPaul Eggert2025-01-26 23:05:53 -0800
commitff65cc9944dc0b37986d512ee8b9817c6913db36 (patch)
treee8ab859edf18216c1cbc41df371a3a4ee8d7747b /src
parentc0d83876f18cb21abae299d3b4a2a2bd7f117252 (diff)
downloademacs-ff65cc9944dc0b37986d512ee8b9817c6913db36.tar.gz
emacs-ff65cc9944dc0b37986d512ee8b9817c6913db36.zip
Pacify GCC in pgtkselect malloc alignment
* src/pgtkselect.c (pgtk_size_for_format): Remove. (pgtk_get_window_property): Third arg is now void **, not unsigned char **. Omit last arg. All callers changed. Simplify allocation to pacify GCC 14.2.1 complaint about malloc size mismatching size of array: there’s no need for an extra byte when converting from ldata to idata. (selection_data_to_lisp_data): Third arg is now void const *, not unsigned char const *. All callers changed. Omit unnecessary casts. Make some locals more local.
Diffstat (limited to 'src')
-rw-r--r--src/pgtkselect.c131
1 files changed, 39 insertions, 92 deletions
diff --git a/src/pgtkselect.c b/src/pgtkselect.c
index c05594d7366..7e6c457d15b 100644
--- a/src/pgtkselect.c
+++ b/src/pgtkselect.c
@@ -50,7 +50,7 @@ static Lisp_Object pgtk_get_window_property_as_lisp_data (struct pgtk_display_in
50 GdkWindow *, GdkAtom, 50 GdkWindow *, GdkAtom,
51 Lisp_Object, GdkAtom, bool); 51 Lisp_Object, GdkAtom, bool);
52static Lisp_Object selection_data_to_lisp_data (struct pgtk_display_info *, 52static Lisp_Object selection_data_to_lisp_data (struct pgtk_display_info *,
53 const unsigned char *, 53 void const *,
54 ptrdiff_t, GdkAtom, int); 54 ptrdiff_t, GdkAtom, int);
55static void lisp_data_to_selection_data (struct pgtk_display_info *, Lisp_Object, 55static void lisp_data_to_selection_data (struct pgtk_display_info *, Lisp_Object,
56 struct selection_data *); 56 struct selection_data *);
@@ -148,7 +148,7 @@ pgtk_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
148 guint32 timestamp = gtk_get_current_event_time (); 148 guint32 timestamp = gtk_get_current_event_time ();
149 GdkAtom selection_atom = symbol_to_gdk_atom (selection_name); 149 GdkAtom selection_atom = symbol_to_gdk_atom (selection_name);
150 Lisp_Object targets; 150 Lisp_Object targets;
151 ptrdiff_t i, ntargets; 151 ptrdiff_t ntargets;
152 GtkTargetEntry *gtargets; 152 GtkTargetEntry *gtargets;
153 153
154 if (timestamp == GDK_CURRENT_TIME) 154 if (timestamp == GDK_CURRENT_TIME)
@@ -207,7 +207,7 @@ pgtk_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
207 gtargets = xzalloc (sizeof *gtargets * ASIZE (targets)); 207 gtargets = xzalloc (sizeof *gtargets * ASIZE (targets));
208 ntargets = 0; 208 ntargets = 0;
209 209
210 for (i = 0; i < ASIZE (targets); ++i) 210 for (ptrdiff_t i = 0; i < ASIZE (targets); i++)
211 { 211 {
212 if (SYMBOLP (AREF (targets, i))) 212 if (SYMBOLP (AREF (targets, i)))
213 gtargets[ntargets++].target 213 gtargets[ntargets++].target
@@ -1072,38 +1072,17 @@ pgtk_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_typ
1072 1072
1073/* Subroutines of pgtk_get_window_property_as_lisp_data */ 1073/* Subroutines of pgtk_get_window_property_as_lisp_data */
1074 1074
1075static ptrdiff_t
1076pgtk_size_for_format (gint format)
1077{
1078 switch (format)
1079 {
1080 case 8:
1081 return sizeof (unsigned char);
1082 case 16:
1083 return sizeof (unsigned short);
1084 case 32:
1085 return sizeof (unsigned long);
1086
1087 default:
1088 emacs_abort ();
1089 }
1090}
1091
1092/* Use xfree, not g_free, to free the data obtained with this function. */ 1075/* Use xfree, not g_free, to free the data obtained with this function. */
1093 1076
1094static void 1077static void
1095pgtk_get_window_property (GdkWindow *window, unsigned char **data_ret, 1078pgtk_get_window_property (GdkWindow *window, void **data_ret,
1096 ptrdiff_t *bytes_ret, GdkAtom *actual_type_ret, 1079 ptrdiff_t *bytes_ret, GdkAtom *actual_type_ret,
1097 int *actual_format_ret, unsigned long *actual_size_ret) 1080 int *actual_format_ret)
1098{ 1081{
1099 gint length, actual_format; 1082 gint length, actual_format;
1100 unsigned char *data; 1083 unsigned char *data;
1101 ptrdiff_t element_size;
1102 void *xdata; 1084 void *xdata;
1103 GdkAtom actual_type; 1085 GdkAtom actual_type;
1104 unsigned long i;
1105 unsigned int *idata;
1106 unsigned long *ldata;
1107 1086
1108 data = NULL; 1087 data = NULL;
1109 1088
@@ -1117,63 +1096,39 @@ pgtk_get_window_property (GdkWindow *window, unsigned char **data_ret,
1117 *actual_type_ret = GDK_NONE; 1096 *actual_type_ret = GDK_NONE;
1118 *bytes_ret = 0; 1097 *bytes_ret = 0;
1119 *actual_format_ret = 8; 1098 *actual_format_ret = 8;
1120 *actual_size_ret = 0;
1121 1099
1122 return; 1100 return;
1123 } 1101 }
1124 1102
1125 if (actual_type == GDK_SELECTION_TYPE_ATOM 1103 if (! (actual_type == GDK_SELECTION_TYPE_ATOM
1126 || actual_type == gdk_atom_intern_static_string ("ATOM_PAIR")) 1104 || actual_type == gdk_atom_intern_static_string ("ATOM_PAIR")))
1127 { 1105 actual_type = GDK_NONE;
1128 /* GDK should not allow anything else. */
1129 eassert (actual_format == 32);
1130
1131 length = length / sizeof (GdkAtom);
1132 xdata = xmalloc (sizeof (GdkAtom) * length + 1);
1133 memcpy (xdata, data, 1 + length * sizeof (GdkAtom));
1134
1135 g_free (data);
1136 1106
1137 *data_ret = xdata; 1107 if (ULONG_WIDTH > 32 && actual_format == 32)
1138 *actual_type_ret = actual_type;
1139 *bytes_ret = length * sizeof (GdkAtom);
1140 *actual_format_ret = 32;
1141 *actual_size_ret = length;
1142
1143 return;
1144 }
1145
1146 element_size = pgtk_size_for_format (actual_format);
1147 length = length / element_size;
1148
1149 /* Add an extra byte on the end. GDK guarantees that it is
1150 NULL. */
1151 xdata = xmalloc (1 + element_size * length);
1152 memcpy (xdata, data, 1 + element_size * length);
1153
1154 if (actual_format == 32 && LONG_WIDTH > 32)
1155 { 1108 {
1156 ldata = (typeof (ldata)) data; 1109 unsigned long int *ldata = (unsigned long int *) data;
1157 idata = xdata; 1110 gint n = length / sizeof *ldata;
1158 1111 unsigned int *idata;
1159 for (i = 0; i < length; ++i) 1112 length = n * sizeof *idata;
1113 idata = xdata = xmalloc (length);
1114 for (gint i = 0; i < n; i++)
1160 idata[i] = ldata[i]; 1115 idata[i] = ldata[i];
1161
1162 /* There is always enough space in idata. */
1163 idata[length] = 0;
1164 *bytes_ret = sizeof *idata * length;
1165 } 1116 }
1166 else 1117 else
1167 /* I think GDK itself prevents element_size from exceeding the 1118 {
1168 length at which this computation fails. */ 1119 /* Add an extra byte on the end. GDK guarantees that it is
1169 *bytes_ret = element_size * length; 1120 NULL. */
1121 xdata = xmalloc (length + 1);
1122 memcpy (xdata, data, length + 1);
1123 }
1124
1125 *bytes_ret = length;
1170 1126
1171 /* Now free the original `data' allocated by GDK. */ 1127 /* Now free the original `data' allocated by GDK. */
1172 g_free (data); 1128 g_free (data);
1173 1129
1174 *data_ret = xdata; 1130 *data_ret = xdata;
1175 *actual_type_ret = GDK_NONE; 1131 *actual_type_ret = GDK_NONE;
1176 *actual_size_ret = length;
1177 *actual_format_ret = actual_format; 1132 *actual_format_ret = actual_format;
1178 *actual_type_ret = actual_type; 1133 *actual_type_ret = actual_type;
1179} 1134}
@@ -1186,15 +1141,13 @@ pgtk_get_window_property_as_lisp_data (struct pgtk_display_info *dpyinfo,
1186{ 1141{
1187 GdkAtom actual_type; 1142 GdkAtom actual_type;
1188 int actual_format; 1143 int actual_format;
1189 unsigned long actual_size; 1144 void *data;
1190 unsigned char *data = 0;
1191 ptrdiff_t bytes = 0; 1145 ptrdiff_t bytes = 0;
1192 Lisp_Object val; 1146 Lisp_Object val;
1193 GdkDisplay *display = dpyinfo->display; 1147 GdkDisplay *display = dpyinfo->display;
1194 1148
1195 pgtk_get_window_property (window, &data, &bytes, 1149 pgtk_get_window_property (window, &data, &bytes,
1196 &actual_type, &actual_format, 1150 &actual_type, &actual_format);
1197 &actual_size);
1198 1151
1199 if (!data) 1152 if (!data)
1200 { 1153 {
@@ -1261,7 +1214,7 @@ pgtk_get_window_property_as_lisp_data (struct pgtk_display_info *dpyinfo,
1261 1214
1262static Lisp_Object 1215static Lisp_Object
1263selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo, 1216selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1264 const unsigned char *data, 1217 void const *data,
1265 ptrdiff_t size, GdkAtom type, int format) 1218 ptrdiff_t size, GdkAtom type, int format)
1266{ 1219{
1267 if (type == gdk_atom_intern_static_string ("NULL")) 1220 if (type == gdk_atom_intern_static_string ("NULL"))
@@ -1271,7 +1224,7 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1271 { 1224 {
1272 Lisp_Object str, lispy_type; 1225 Lisp_Object str, lispy_type;
1273 1226
1274 str = make_unibyte_string ((char *) data, size); 1227 str = make_unibyte_string (data, size);
1275 /* Indicate that this string is from foreign selection by a text 1228 /* Indicate that this string is from foreign selection by a text
1276 property `foreign-selection' so that the caller of 1229 property `foreign-selection' so that the caller of
1277 x-get-selection-internal (usually x-get-selection) can know 1230 x-get-selection-internal (usually x-get-selection) can know
@@ -1294,8 +1247,7 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1294 /* Treat ATOM_PAIR type similar to list of atoms. */ 1247 /* Treat ATOM_PAIR type similar to list of atoms. */
1295 || type == gdk_atom_intern_static_string ("ATOM_PAIR"))) 1248 || type == gdk_atom_intern_static_string ("ATOM_PAIR")))
1296 { 1249 {
1297 ptrdiff_t i; 1250 GdkAtom const *idata = data;
1298 GdkAtom *idata = (GdkAtom *) data;
1299 1251
1300 if (size == sizeof (GdkAtom)) 1252 if (size == sizeof (GdkAtom))
1301 return gdk_atom_to_symbol (idata[0]); 1253 return gdk_atom_to_symbol (idata[0]);
@@ -1303,7 +1255,7 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1303 { 1255 {
1304 Lisp_Object v = make_nil_vector (size / sizeof (GdkAtom)); 1256 Lisp_Object v = make_nil_vector (size / sizeof (GdkAtom));
1305 1257
1306 for (i = 0; i < size / sizeof (GdkAtom); i++) 1258 for (ptrdiff_t i = 0; i < size / sizeof (GdkAtom); i++)
1307 ASET (v, i, gdk_atom_to_symbol (idata[i])); 1259 ASET (v, i, gdk_atom_to_symbol (idata[i]));
1308 return v; 1260 return v;
1309 } 1261 }
@@ -1335,12 +1287,11 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1335 */ 1287 */
1336 else if (format == 16) 1288 else if (format == 16)
1337 { 1289 {
1338 ptrdiff_t i;
1339 Lisp_Object v = make_uninit_vector (size / 2); 1290 Lisp_Object v = make_uninit_vector (size / 2);
1340 1291
1341 if (type == GDK_SELECTION_TYPE_INTEGER) 1292 if (type == GDK_SELECTION_TYPE_INTEGER)
1342 { 1293 {
1343 for (i = 0; i < size / 2; i++) 1294 for (ptrdiff_t i = 0; i < size / 2; i++)
1344 { 1295 {
1345 short j = ((short *) data) [i]; 1296 short j = ((short *) data) [i];
1346 ASET (v, i, make_fixnum (j)); 1297 ASET (v, i, make_fixnum (j));
@@ -1348,7 +1299,7 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1348 } 1299 }
1349 else 1300 else
1350 { 1301 {
1351 for (i = 0; i < size / 2; i++) 1302 for (ptrdiff_t i = 0; i < size / 2; i++)
1352 { 1303 {
1353 unsigned short j = ((unsigned short *) data) [i]; 1304 unsigned short j = ((unsigned short *) data) [i];
1354 ASET (v, i, make_fixnum (j)); 1305 ASET (v, i, make_fixnum (j));
@@ -1358,12 +1309,11 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1358 } 1309 }
1359 else 1310 else
1360 { 1311 {
1361 ptrdiff_t i;
1362 Lisp_Object v = make_nil_vector (size / sizeof (gint)); 1312 Lisp_Object v = make_nil_vector (size / sizeof (gint));
1363 1313
1364 if (type == GDK_SELECTION_TYPE_INTEGER) 1314 if (type == GDK_SELECTION_TYPE_INTEGER)
1365 { 1315 {
1366 for (i = 0; i < size / sizeof (gint); i++) 1316 for (ptrdiff_t i = 0; i < size / sizeof (gint); i++)
1367 { 1317 {
1368 int j = ((gint *) data) [i]; 1318 int j = ((gint *) data) [i];
1369 ASET (v, i, INT_TO_INTEGER (j)); 1319 ASET (v, i, INT_TO_INTEGER (j));
@@ -1371,7 +1321,7 @@ selection_data_to_lisp_data (struct pgtk_display_info *dpyinfo,
1371 } 1321 }
1372 else 1322 else
1373 { 1323 {
1374 for (i = 0; i < size / sizeof (gint); i++) 1324 for (ptrdiff_t i = 0; i < size / sizeof (gint); i++)
1375 { 1325 {
1376 unsigned int j = ((unsigned int *) data) [i]; 1326 unsigned int j = ((unsigned int *) data) [i];
1377 ASET (v, i, INT_TO_INTEGER (j)); 1327 ASET (v, i, INT_TO_INTEGER (j));
@@ -1478,7 +1428,6 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo,
1478 a set of 16 or 32 bit INTEGERs; 1428 a set of 16 or 32 bit INTEGERs;
1479 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...] 1429 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1480 */ 1430 */
1481 ptrdiff_t i;
1482 ptrdiff_t size = ASIZE (obj); 1431 ptrdiff_t size = ASIZE (obj);
1483 1432
1484 if (SYMBOLP (AREF (obj, 0))) 1433 if (SYMBOLP (AREF (obj, 0)))
@@ -1487,7 +1436,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo,
1487 void *data; 1436 void *data;
1488 GdkAtom *x_atoms; 1437 GdkAtom *x_atoms;
1489 if (NILP (type)) type = QATOM; 1438 if (NILP (type)) type = QATOM;
1490 for (i = 0; i < size; i++) 1439 for (ptrdiff_t i = 0; i < size; i++)
1491 if (!SYMBOLP (AREF (obj, i))) 1440 if (!SYMBOLP (AREF (obj, i)))
1492 signal_error ("All elements of selection vector must have same type", obj); 1441 signal_error ("All elements of selection vector must have same type", obj);
1493 1442
@@ -1495,7 +1444,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo,
1495 x_atoms = data; 1444 x_atoms = data;
1496 cs->format = 32; 1445 cs->format = 32;
1497 cs->size = size; 1446 cs->size = size;
1498 for (i = 0; i < size; i++) 1447 for (ptrdiff_t i = 0; i < size; i++)
1499 x_atoms[i] = symbol_to_gdk_atom (AREF (obj, i)); 1448 x_atoms[i] = symbol_to_gdk_atom (AREF (obj, i));
1500 } 1449 }
1501 else 1450 else
@@ -1507,7 +1456,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo,
1507 unsigned long *x_atoms; 1456 unsigned long *x_atoms;
1508 short *shorts; 1457 short *shorts;
1509 if (NILP (type)) type = QINTEGER; 1458 if (NILP (type)) type = QINTEGER;
1510 for (i = 0; i < size; i++) 1459 for (ptrdiff_t i = 0; i < size; i++)
1511 { 1460 {
1512 if (! RANGED_FIXNUMP (SHRT_MIN, AREF (obj, i), SHRT_MAX)) 1461 if (! RANGED_FIXNUMP (SHRT_MIN, AREF (obj, i), SHRT_MAX))
1513 { 1462 {
@@ -1524,7 +1473,7 @@ lisp_data_to_selection_data (struct pgtk_display_info *dpyinfo,
1524 shorts = data; 1473 shorts = data;
1525 cs->format = format; 1474 cs->format = format;
1526 cs->size = size; 1475 cs->size = size;
1527 for (i = 0; i < size; i++) 1476 for (ptrdiff_t i = 0; i < size; i++)
1528 { 1477 {
1529 if (format == 32) 1478 if (format == 32)
1530 x_atoms[i] = cons_to_gdk_long (AREF (obj, i)); 1479 x_atoms[i] = cons_to_gdk_long (AREF (obj, i));
@@ -1560,13 +1509,11 @@ clean_local_selection_data (Lisp_Object obj)
1560 } 1509 }
1561 if (VECTORP (obj)) 1510 if (VECTORP (obj))
1562 { 1511 {
1563 ptrdiff_t i;
1564 ptrdiff_t size = ASIZE (obj); 1512 ptrdiff_t size = ASIZE (obj);
1565 Lisp_Object copy;
1566 if (size == 1) 1513 if (size == 1)
1567 return clean_local_selection_data (AREF (obj, 0)); 1514 return clean_local_selection_data (AREF (obj, 0));
1568 copy = make_nil_vector (size); 1515 Lisp_Object copy = make_nil_vector (size);
1569 for (i = 0; i < size; i++) 1516 for (ptrdiff_t i = 0; i < size; i++)
1570 ASET (copy, i, clean_local_selection_data (AREF (obj, i))); 1517 ASET (copy, i, clean_local_selection_data (AREF (obj, i)));
1571 return copy; 1518 return copy;
1572 } 1519 }