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