Skip to content

Commit 5945104

Browse files
committed
Fix missing null dereference checks
1 parent f0b5f56 commit 5945104

5 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/_avif.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,27 @@ _decoder_get_info(AvifDecoderObject *self) {
708708

709709
if (image->xmp.size) {
710710
xmp = PyBytes_FromStringAndSize((const char *)image->xmp.data, image->xmp.size);
711+
if (!xmp) {
712+
return NULL;
713+
}
711714
}
712715

713716
if (image->exif.size) {
714717
exif =
715718
PyBytes_FromStringAndSize((const char *)image->exif.data, image->exif.size);
719+
if (!exif) {
720+
Py_XDECREF(xmp);
721+
return NULL;
722+
}
716723
}
717724

718725
if (image->icc.size) {
719726
icc = PyBytes_FromStringAndSize((const char *)image->icc.data, image->icc.size);
727+
if (!icc) {
728+
Py_XDECREF(xmp);
729+
Py_XDECREF(exif);
730+
return NULL;
731+
}
720732
}
721733

722734
ret = Py_BuildValue(
@@ -799,13 +811,17 @@ _decoder_get_frame(AvifDecoderObject *self, PyObject *args) {
799811

800812
if (rgb.height > PY_SSIZE_T_MAX / rgb.rowBytes) {
801813
PyErr_SetString(PyExc_MemoryError, "Integer overflow in pixel size");
814+
// UNDONE avifRGBImageFreePixels(&rgb); ??
802815
return NULL;
803816
}
804817

805818
size = rgb.rowBytes * rgb.height;
806819

807820
bytes = PyBytes_FromStringAndSize((char *)rgb.pixels, size);
808821
avifRGBImageFreePixels(&rgb);
822+
if (!bytes) {
823+
return NULL;
824+
}
809825

810826
ret = Py_BuildValue(
811827
"SKKK",

src/_imaging.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,9 @@ _split(ImagingObject *self) {
24692469
}
24702470

24712471
list = PyTuple_New(self->image->bands);
2472+
if (!list) {
2473+
return NULL;
2474+
}
24722475
for (i = 0; i < self->image->bands; i++) {
24732476
imaging_object = PyImagingNew(bands[i]);
24742477
if (!imaging_object) {
@@ -3765,6 +3768,9 @@ _ptr_destructor(PyObject *capsule) {
37653768
static PyObject *
37663769
_getattr_ptr(ImagingObject *self, void *closure) {
37673770
PyObject *capsule = PyCapsule_New(self->image, IMAGING_MAGIC, _ptr_destructor);
3771+
if (!capsule) {
3772+
return NULL;
3773+
}
37683774
Py_INCREF(self);
37693775
PyCapsule_SetContext(capsule, self);
37703776
return capsule;

src/_imagingft.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,10 @@ font_render(FontObject *self, PyObject *args) {
941941
return NULL;
942942
}
943943
PyObject *imagePtr = PyObject_GetAttrString(image, "ptr");
944+
if (!imagePtr) {
945+
PyMem_Del(glyph_info);
946+
return NULL;
947+
}
944948
im = (Imaging)PyCapsule_GetPointer(imagePtr, IMAGING_MAGIC);
945949
Py_XDECREF(imagePtr);
946950

src/_imagingmorph.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ match(PyObject *self, PyObject *args) {
143143
}
144144

145145
imgin = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
146+
if (!imgin) {
147+
return NULL;
148+
}
146149

147150
if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) {
148151
PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
@@ -185,6 +188,10 @@ match(PyObject *self, PyObject *args) {
185188
(b6 << 6) | (b7 << 7) | (b8 << 8));
186189
if (lut[lut_idx]) {
187190
PyObject *coordObj = Py_BuildValue("(nn)", col_idx, row_idx);
191+
if (!coordObj) {
192+
Py_XDECREF(ret);
193+
return NULL;
194+
}
188195
PyList_Append(ret, coordObj);
189196
Py_XDECREF(coordObj);
190197
}
@@ -230,6 +237,10 @@ get_on_pixels(PyObject *self, PyObject *args) {
230237
for (col_idx = 0; col_idx < width; col_idx++) {
231238
if (row[col_idx]) {
232239
PyObject *coordObj = Py_BuildValue("(nn)", col_idx, row_idx);
240+
if (!coordObj) {
241+
Py_XDECREF(ret);
242+
return NULL;
243+
}
233244
PyList_Append(ret, coordObj);
234245
Py_XDECREF(coordObj);
235246
}

src/_webp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ _anim_decoder_get_next(PyObject *self) {
503503
bytes = PyBytes_FromStringAndSize(
504504
(char *)buf, decp->info.canvas_width * 4 * decp->info.canvas_height
505505
);
506+
if (!bytes) {
507+
return NULL;
508+
}
506509

507510
ret = Py_BuildValue("Si", bytes, timestamp);
508511

0 commit comments

Comments
 (0)