Skip to content

Commit 396b0a2

Browse files
authored
Check PyCapsule_GetPointer and PyBytes_FromStringAndSize return values (#9508)
2 parents 7672b19 + 20a9401 commit 396b0a2

9 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/_imaging.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ void
254254
ReleaseArrowSchemaPyCapsule(PyObject *capsule) {
255255
struct ArrowSchema *schema =
256256
(struct ArrowSchema *)PyCapsule_GetPointer(capsule, "arrow_schema");
257+
if (!schema) {
258+
return;
259+
}
257260
if (schema->release != NULL) {
258261
schema->release(schema);
259262
}
@@ -276,6 +279,9 @@ void
276279
ReleaseArrowArrayPyCapsule(PyObject *capsule) {
277280
struct ArrowArray *array =
278281
(struct ArrowArray *)PyCapsule_GetPointer(capsule, "arrow_array");
282+
if (!array) {
283+
return;
284+
}
279285
if (array->release != NULL) {
280286
array->release(array);
281287
}

src/_imagingcms.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,13 @@ cms_transform_apply(CmsTransformObject *self, PyObject *args) {
558558
}
559559

560560
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
561+
if (!im) {
562+
return NULL;
563+
}
561564
imOut = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
565+
if (!imOut) {
566+
return NULL;
567+
}
562568

563569
return Py_BuildValue("i", pyCMSdoTransform(im, imOut, self->transform));
564570
}

src/_imagingft.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ font_render(FontObject *self, PyObject *args) {
948948
}
949949
im = (Imaging)PyCapsule_GetPointer(imagePtr, IMAGING_MAGIC);
950950
Py_DECREF(imagePtr);
951+
if (!im) {
952+
PyMem_Del(glyph_info);
953+
Py_DECREF(image);
954+
return NULL;
955+
}
951956

952957
x_offset = round(x_offset - stroke_width);
953958
y_offset = round(y_offset - stroke_width);

src/_imagingmath.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,17 @@ _unop(PyObject *self, PyObject *args) {
187187
}
188188

189189
unop = (void *)PyCapsule_GetPointer(op, MATH_FUNC_UNOP_MAGIC);
190+
if (!unop) {
191+
return NULL;
192+
}
190193
out = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
194+
if (!out) {
195+
return NULL;
196+
}
191197
im1 = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
198+
if (!im1) {
199+
return NULL;
200+
}
192201

193202
unop(out, im1);
194203

@@ -219,9 +228,21 @@ _binop(PyObject *self, PyObject *args) {
219228
}
220229

221230
binop = (void *)PyCapsule_GetPointer(op, MATH_FUNC_BINOP_MAGIC);
231+
if (!binop) {
232+
return NULL;
233+
}
222234
out = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
235+
if (!out) {
236+
return NULL;
237+
}
223238
im1 = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
239+
if (!im1) {
240+
return NULL;
241+
}
224242
im2 = (Imaging)PyCapsule_GetPointer(i2, IMAGING_MAGIC);
243+
if (!im2) {
244+
return NULL;
245+
}
225246

226247
binop(out, im1, im2);
227248

src/_imagingmorph.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ apply(PyObject *self, PyObject *args) {
5353
}
5454

5555
imgin = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
56+
if (!imgin) {
57+
return NULL;
58+
}
5659
imgout = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
60+
if (!imgout) {
61+
return NULL;
62+
}
5763
width = imgin->xsize;
5864
height = imgin->ysize;
5965

@@ -223,6 +229,9 @@ get_on_pixels(PyObject *self, PyObject *args) {
223229
}
224230

225231
img = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
232+
if (!img) {
233+
return NULL;
234+
}
226235
rows = img->image8;
227236
width = img->xsize;
228237
height = img->ysize;

src/_webp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ _anim_encoder_add(PyObject *self, PyObject *args) {
262262
}
263263

264264
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
265+
if (!im) {
266+
return NULL;
267+
}
265268

266269
// Setup config for this frame
267270
if (!WebPConfigInit(&config)) {
@@ -610,6 +613,9 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
610613
}
611614

612615
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
616+
if (!im) {
617+
return NULL;
618+
}
613619

614620
// Setup config for this frame
615621
if (!WebPConfigInit(&config)) {

src/display.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ PyImaging_GrabClipboardWin32(PyObject *self, PyObject *args) {
480480
GlobalUnlock(handle);
481481
CloseClipboard();
482482

483+
if (!result) {
484+
return NULL;
485+
}
483486
return Py_BuildValue("zN", format_names[format], result);
484487
}
485488

src/libImaging/Storage.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,15 @@ ImagingNewArrow(
677677
Imaging im;
678678
struct ArrowSchema *schema =
679679
(struct ArrowSchema *)PyCapsule_GetPointer(schema_capsule, "arrow_schema");
680+
if (!schema) {
681+
return NULL;
682+
}
680683

681684
struct ArrowArray *external_array =
682685
(struct ArrowArray *)PyCapsule_GetPointer(array_capsule, "arrow_array");
686+
if (!external_array) {
687+
return NULL;
688+
}
683689

684690
if (xsize < 0 || ysize < 0) {
685691
return (Imaging)ImagingError_ValueError("bad image size");

src/libImaging/codec_fd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ _imaging_write_pyFd(PyObject *fd, char *src, Py_ssize_t bytes) {
4141
PyObject *byteObj;
4242

4343
byteObj = PyBytes_FromStringAndSize(src, bytes);
44+
if (!byteObj) {
45+
return -1;
46+
}
4447
result = PyObject_CallMethod(fd, "write", "O", byteObj);
4548

4649
Py_DECREF(byteObj);

0 commit comments

Comments
 (0)