Skip to content

Commit e41083f

Browse files
authored
Improve reference handling in encode.c (#9920)
2 parents cacb513 + f195518 commit e41083f

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

src/encode.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
750750
const RawModeID rawmode = findRawModeID(rawmode_name);
751751

752752
if (get_packer(encoder, mode, rawmode) < 0) {
753-
Py_DECREF(encoder);
754753
return NULL;
755754
}
756755

@@ -822,6 +821,7 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
822821
is_var_length = 1;
823822

824823
if (!len) {
824+
Py_DECREF(item);
825825
continue;
826826
}
827827

@@ -844,6 +844,7 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
844844
if (ImagingLibTiffMergeFieldInfo(
845845
&encoder->state, type, key_int, is_var_length
846846
)) {
847+
Py_DECREF(item);
847848
continue;
848849
}
849850
}
@@ -1112,6 +1113,9 @@ get_qtables_arrays(PyObject *qtables, int *qtablesLen) {
11121113
}
11131114

11141115
tables = PySequence_Fast(qtables, "expected a sequence");
1116+
if (!tables) {
1117+
return NULL;
1118+
}
11151119
num_tables = PySequence_Size(qtables);
11161120
if (num_tables < 1 || num_tables > NUM_QUANT_TBLS) {
11171121
PyErr_SetString(
@@ -1138,6 +1142,9 @@ get_qtables_arrays(PyObject *qtables, int *qtablesLen) {
11381142
goto JPEG_QTABLES_ERR;
11391143
}
11401144
table_data = PySequence_Fast(table, "expected a sequence");
1145+
if (!table_data) {
1146+
goto JPEG_QTABLES_ERR;
1147+
}
11411148
for (j = 0; j < DCTSIZE2; j++) {
11421149
qarrays[i * DCTSIZE2 + j] =
11431150
PyLong_AS_LONG(PySequence_Fast_GET_ITEM(table_data, j));
@@ -1232,12 +1239,16 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
12321239

12331240
// Freed in JpegEncode, Case 6
12341241
qarrays = get_qtables_arrays(qtables, &qtablesLen);
1242+
if (!qarrays && PyErr_Occurred()) {
1243+
Py_DECREF(encoder);
1244+
return NULL;
1245+
}
12351246

12361247
if (comment && comment_size > 0) {
12371248
/* malloc check ok, length is from python parsearg */
12381249
char *p = malloc(comment_size); // Freed in JpegEncode, Case 6
12391250
if (!p) {
1240-
return ImagingError_MemoryError();
1251+
goto memory_error;
12411252
}
12421253
memcpy(p, comment, comment_size);
12431254
comment = p;
@@ -1249,10 +1260,7 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
12491260
/* malloc check ok, length is from python parsearg */
12501261
char *p = malloc(extra_size); // Freed in JpegEncode, Case 6
12511262
if (!p) {
1252-
if (comment) {
1253-
free(comment);
1254-
}
1255-
return ImagingError_MemoryError();
1263+
goto memory_error;
12561264
}
12571265
memcpy(p, extra, extra_size);
12581266
extra = p;
@@ -1264,13 +1272,7 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
12641272
/* malloc check ok, length is from python parsearg */
12651273
char *pp = malloc(rawExifLen); // Freed in JpegEncode, Case 6
12661274
if (!pp) {
1267-
if (comment) {
1268-
free(comment);
1269-
}
1270-
if (extra) {
1271-
free(extra);
1272-
}
1273-
return ImagingError_MemoryError();
1275+
goto memory_error;
12741276
}
12751277
memcpy(pp, rawExif, rawExifLen);
12761278
rawExif = pp;
@@ -1303,6 +1305,19 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
13031305
jpeg_encoder_state->rawExifLen = rawExifLen;
13041306

13051307
return (PyObject *)encoder;
1308+
1309+
memory_error:
1310+
Py_DECREF(encoder);
1311+
if (qarrays) {
1312+
free(qarrays);
1313+
}
1314+
if (comment) {
1315+
free(comment);
1316+
}
1317+
if (extra) {
1318+
free(extra);
1319+
}
1320+
return ImagingError_MemoryError();
13061321
}
13071322

13081323
#endif

0 commit comments

Comments
 (0)