|
20 | 20 |
|
21 | 21 | #define PY_SSIZE_T_CLEAN |
22 | 22 | #include "Python.h" |
| 23 | +#include "thirdparty/pythoncapi_compat.h" |
23 | 24 | #include "libImaging/Imaging.h" |
24 | 25 |
|
25 | 26 | #include <ft2build.h> |
@@ -1209,30 +1210,49 @@ font_getvarnames(FontObject *self) { |
1209 | 1210 | return NULL; |
1210 | 1211 | } |
1211 | 1212 |
|
| 1213 | + int *list_names_filled = PyMem_Malloc(num_namedstyles * sizeof(int)); |
| 1214 | + if (list_names_filled == NULL) { |
| 1215 | + Py_DECREF(list_names); |
| 1216 | + FT_Done_MM_Var(library, master); |
| 1217 | + return PyErr_NoMemory(); |
| 1218 | + } |
| 1219 | + |
| 1220 | + for (int i = 0; i < num_namedstyles; i++) { |
| 1221 | + list_names_filled[i] = 0; |
| 1222 | + } |
| 1223 | + |
1212 | 1224 | name_count = FT_Get_Sfnt_Name_Count(self->face); |
1213 | 1225 | for (i = 0; i < name_count; i++) { |
1214 | 1226 | error = FT_Get_Sfnt_Name(self->face, i, &name); |
1215 | 1227 | if (error) { |
| 1228 | + PyMem_Free(list_names_filled); |
1216 | 1229 | Py_DECREF(list_names); |
1217 | 1230 | FT_Done_MM_Var(library, master); |
1218 | 1231 | return geterror(error); |
1219 | 1232 | } |
1220 | 1233 |
|
1221 | 1234 | for (j = 0; j < num_namedstyles; j++) { |
1222 | | - if (PyList_GetItem(list_names, j) != NULL) { |
| 1235 | + if (list_names_filled[j]) { |
1223 | 1236 | continue; |
1224 | 1237 | } |
1225 | 1238 |
|
1226 | 1239 | if (master->namedstyle[j].strid == name.name_id) { |
1227 | 1240 | list_name = Py_BuildValue("y#", name.string, name.string_len); |
| 1241 | + if (list_name == NULL) { |
| 1242 | + PyMem_Free(list_names_filled); |
| 1243 | + Py_DECREF(list_names); |
| 1244 | + FT_Done_MM_Var(library, master); |
| 1245 | + return NULL; |
| 1246 | + } |
1228 | 1247 | PyList_SetItem(list_names, j, list_name); |
| 1248 | + list_names_filled[j] = 1; |
1229 | 1249 | break; |
1230 | 1250 | } |
1231 | 1251 | } |
1232 | 1252 | } |
1233 | 1253 |
|
| 1254 | + PyMem_Free(list_names_filled); |
1234 | 1255 | FT_Done_MM_Var(library, master); |
1235 | | - |
1236 | 1256 | return list_names; |
1237 | 1257 | } |
1238 | 1258 |
|
@@ -1289,9 +1309,15 @@ font_getvaraxes(FontObject *self) { |
1289 | 1309 |
|
1290 | 1310 | if (name.name_id == axis.strid) { |
1291 | 1311 | axis_name = Py_BuildValue("y#", name.string, name.string_len); |
| 1312 | + if (axis_name == NULL) { |
| 1313 | + Py_DECREF(list_axis); |
| 1314 | + Py_DECREF(list_axes); |
| 1315 | + FT_Done_MM_Var(library, master); |
| 1316 | + return NULL; |
| 1317 | + } |
1292 | 1318 | PyDict_SetItemString( |
1293 | 1319 | list_axis, "name", axis_name ? axis_name : Py_None); |
1294 | | - Py_XDECREF(axis_name); |
| 1320 | + Py_DECREF(axis_name); |
1295 | 1321 | break; |
1296 | 1322 | } |
1297 | 1323 | } |
@@ -1345,18 +1371,25 @@ font_setvaraxes(FontObject *self, PyObject *args) { |
1345 | 1371 | return PyErr_NoMemory(); |
1346 | 1372 | } |
1347 | 1373 | for (i = 0; i < num_coords; i++) { |
1348 | | - item = PyList_GET_ITEM(axes, i); |
| 1374 | + item = PyList_GetItemRef(axes, i); |
| 1375 | + if (item == NULL) { |
| 1376 | + free(coords); |
| 1377 | + return NULL; |
| 1378 | + } |
| 1379 | + |
1349 | 1380 | if (PyFloat_Check(item)) { |
1350 | 1381 | coord = PyFloat_AS_DOUBLE(item); |
1351 | 1382 | } else if (PyLong_Check(item)) { |
1352 | 1383 | coord = (float)PyLong_AS_LONG(item); |
1353 | 1384 | } else if (PyNumber_Check(item)) { |
1354 | 1385 | coord = PyFloat_AsDouble(item); |
1355 | 1386 | } else { |
| 1387 | + Py_DECREF(item); |
1356 | 1388 | free(coords); |
1357 | 1389 | PyErr_SetString(PyExc_TypeError, "list must contain numbers"); |
1358 | 1390 | return NULL; |
1359 | 1391 | } |
| 1392 | + Py_DECREF(item); |
1360 | 1393 | coords[i] = coord * 65536; |
1361 | 1394 | } |
1362 | 1395 |
|
@@ -1576,5 +1609,9 @@ PyInit__imagingft(void) { |
1576 | 1609 | return NULL; |
1577 | 1610 | } |
1578 | 1611 |
|
| 1612 | +#ifdef Py_GIL_DISABLED |
| 1613 | + PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); |
| 1614 | +#endif |
| 1615 | + |
1579 | 1616 | return m; |
1580 | 1617 | } |
0 commit comments