Skip to content

Commit 851868c

Browse files
authored
Fix refcounts after porting to GetItemRef & better error checking (#126)
2 parents c21a403 + 8854e46 commit 851868c

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

src/_imaging.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,11 @@ _getcolors(ImagingObject *self, PyObject *args) {
22512251
ImagingColorItem *v = &items[i];
22522252
PyObject *item = Py_BuildValue(
22532253
"iN", v->count, getpixel(self->image, self->access, v->x, v->y));
2254+
if (item == NULL) {
2255+
Py_DECREF(out);
2256+
free(items);
2257+
return NULL;
2258+
}
22542259
PyList_SetItem(out, i, item);
22552260
}
22562261
}

src/_imagingft.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,25 +1205,27 @@ font_getvarnames(FontObject *self) {
12051205

12061206
num_namedstyles = master->num_namedstyles;
12071207
list_names = PyList_New(num_namedstyles);
1208+
if (list_names == NULL) {
1209+
FT_Done_MM_Var(library, master);
1210+
return NULL;
1211+
}
12081212

12091213
int *list_names_filled = PyMem_Malloc(num_namedstyles * sizeof(int));
12101214
if (list_names_filled == NULL) {
1215+
Py_DECREF(list_names);
1216+
FT_Done_MM_Var(library, master);
12111217
return PyErr_NoMemory();
12121218
}
12131219

12141220
for (int i = 0; i < num_namedstyles; i++) {
12151221
list_names_filled[i] = 0;
12161222
}
12171223

1218-
if (list_names == NULL) {
1219-
FT_Done_MM_Var(library, master);
1220-
return NULL;
1221-
}
1222-
12231224
name_count = FT_Get_Sfnt_Name_Count(self->face);
12241225
for (i = 0; i < name_count; i++) {
12251226
error = FT_Get_Sfnt_Name(self->face, i, &name);
12261227
if (error) {
1228+
PyMem_Free(list_names_filled);
12271229
Py_DECREF(list_names);
12281230
FT_Done_MM_Var(library, master);
12291231
return geterror(error);
@@ -1236,6 +1238,12 @@ font_getvarnames(FontObject *self) {
12361238

12371239
if (master->namedstyle[j].strid == name.name_id) {
12381240
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+
}
12391247
PyList_SetItem(list_names, j, list_name);
12401248
list_names_filled[j] = 1;
12411249
break;
@@ -1301,9 +1309,15 @@ font_getvaraxes(FontObject *self) {
13011309

13021310
if (name.name_id == axis.strid) {
13031311
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+
}
13041318
PyDict_SetItemString(
13051319
list_axis, "name", axis_name ? axis_name : Py_None);
1306-
Py_XDECREF(axis_name);
1320+
Py_DECREF(axis_name);
13071321
break;
13081322
}
13091323
}
@@ -1357,18 +1371,25 @@ font_setvaraxes(FontObject *self, PyObject *args) {
13571371
return PyErr_NoMemory();
13581372
}
13591373
for (i = 0; i < num_coords; i++) {
1360-
item = PyList_GET_ITEM(axes, i);
1374+
item = PyList_GetItemRef(axes, i);
1375+
if (item == NULL) {
1376+
free(coords);
1377+
return NULL;
1378+
}
1379+
13611380
if (PyFloat_Check(item)) {
13621381
coord = PyFloat_AS_DOUBLE(item);
13631382
} else if (PyLong_Check(item)) {
13641383
coord = (float)PyLong_AS_LONG(item);
13651384
} else if (PyNumber_Check(item)) {
13661385
coord = PyFloat_AsDouble(item);
13671386
} else {
1387+
Py_DECREF(item);
13681388
free(coords);
13691389
PyErr_SetString(PyExc_TypeError, "list must contain numbers");
13701390
return NULL;
13711391
}
1392+
Py_DECREF(item);
13721393
coords[i] = coord * 65536;
13731394
}
13741395

src/encode.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,16 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
673673
TRACE(("tags size: %d\n", (int)tags_size));
674674
for (pos = 0; pos < tags_size; pos++) {
675675
item = PyList_GetItemRef(tags, pos);
676+
if (item == NULL) {
677+
return NULL;
678+
}
679+
676680
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) {
681+
Py_DECREF(item);
677682
PyErr_SetString(PyExc_ValueError, "Invalid tags list");
678683
return NULL;
679684
}
685+
Py_DECREF(item);
680686
}
681687
pos = 0;
682688
}
@@ -705,10 +711,16 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
705711
num_core_tags = sizeof(core_tags) / sizeof(int);
706712
for (pos = 0; pos < tags_size; pos++) {
707713
item = PyList_GetItemRef(tags, pos);
714+
if (item == NULL) {
715+
return NULL;
716+
}
717+
708718
// We already checked that tags is a 2-tuple list.
709-
key = PyTuple_GetItem(item, 0);
719+
key = PyTuple_GET_ITEM(item, 0);
710720
key_int = (int)PyLong_AsLong(key);
711-
value = PyTuple_GetItem(item, 1);
721+
value = PyTuple_GET_ITEM(item, 1);
722+
Py_DECREF(item);
723+
712724
status = 0;
713725
is_core_tag = 0;
714726
is_var_length = 0;

src/path.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include "Python.h"
29+
#include "thirdparty/pythoncapi_compat.h"
2930
#include "libImaging/Imaging.h"
3031

3132
#include <math.h>
@@ -179,14 +180,21 @@ PyPath_Flatten(PyObject *data, double **pxy) {
179180
} \
180181
free(xy); \
181182
return -1; \
183+
} \
184+
if (decref) { \
185+
Py_DECREF(op); \
182186
}
183187

184188
/* Copy table to path array */
185189
if (PyList_Check(data)) {
186190
for (i = 0; i < n; i++) {
187191
double x, y;
188-
PyObject *op = PyList_GET_ITEM(data, i);
189-
assign_item_to_array(op, 0);
192+
PyObject *op = PyList_GetItemRef(data, i);
193+
if (op == NULL) {
194+
free(xy);
195+
return -1;
196+
}
197+
assign_item_to_array(op, 1);
190198
}
191199
} else if (PyTuple_Check(data)) {
192200
for (i = 0; i < n; i++) {
@@ -209,7 +217,6 @@ PyPath_Flatten(PyObject *data, double **pxy) {
209217
}
210218
}
211219
assign_item_to_array(op, 1);
212-
Py_DECREF(op);
213220
}
214221
}
215222

0 commit comments

Comments
 (0)