Skip to content

Commit

Permalink
Delete encoder object on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 27, 2024
1 parent ea22f87 commit 853d8c6
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ PyObject *
AvifEncoderNew(PyObject *self_, PyObject *args) {
unsigned int width, height;
AvifEncoderObject *self = NULL;
avifEncoder *encoder = NULL;
avifEncoder *encoder;

char *subsampling;
int qmin;
Expand Down Expand Up @@ -385,7 +385,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
self->icc_bytes = NULL;
self->exif_bytes = NULL;
self->xmp_bytes = NULL;
self->image = image;

avifResult result;
if (PyBytes_GET_SIZE(icc_bytes)) {
Expand All @@ -403,6 +402,8 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
);
avifImageDestroy(image);
avifEncoderDestroy(encoder);
Py_XDECREF(self->icc_bytes);
PyObject_Del(self);
return NULL;
}
} else {
Expand All @@ -427,6 +428,9 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
);
avifImageDestroy(image);
avifEncoderDestroy(encoder);
Py_XDECREF(self->icc_bytes);
Py_XDECREF(self->exif_bytes);
PyObject_Del(self);
return NULL;
}
}
Expand All @@ -445,13 +449,18 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
);
avifImageDestroy(image);
avifEncoderDestroy(encoder);
Py_XDECREF(self->icc_bytes);
Py_XDECREF(self->exif_bytes);
Py_XDECREF(self->xmp_bytes);
PyObject_Del(self);
return NULL;
}
}
if (exif_orientation > 1) {
exif_orientation_to_irot_imir(image, exif_orientation);
}

self->image = image;
self->encoder = encoder;

return (PyObject *)self;
Expand Down Expand Up @@ -656,6 +665,7 @@ PyObject *
AvifDecoderNew(PyObject *self_, PyObject *args) {
PyObject *avif_bytes;
AvifDecoderObject *self = NULL;
avifDecoder *decoder;

char *codec_str;
avifCodecChoice codec;
Expand All @@ -678,27 +688,26 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
PyErr_SetString(PyExc_RuntimeError, "could not create decoder object");
return NULL;
}
self->decoder = NULL;

Py_INCREF(avif_bytes);
self->data = avif_bytes;

self->decoder = avifDecoderCreate();
decoder = avifDecoderCreate();
#if AVIF_VERSION >= 80400
self->decoder->maxThreads = max_threads;
decoder->maxThreads = max_threads;
#endif
#if AVIF_VERSION >= 90200
// Turn off libavif's 'clap' (clean aperture) property validation.
self->decoder->strictFlags &= ~AVIF_STRICT_CLAP_VALID;
decoder->strictFlags &= ~AVIF_STRICT_CLAP_VALID;
// Allow the PixelInformationProperty ('pixi') to be missing in AV1 image
// items. libheif v1.11.0 and older does not add the 'pixi' item property to
// AV1 image items.
self->decoder->strictFlags &= ~AVIF_STRICT_PIXI_REQUIRED;
decoder->strictFlags &= ~AVIF_STRICT_PIXI_REQUIRED;
#endif
self->decoder->codecChoice = codec;
decoder->codecChoice = codec;

result = avifDecoderSetIOMemory(
self->decoder,
decoder,
(uint8_t *)PyBytes_AS_STRING(self->data),
PyBytes_GET_SIZE(self->data)
);
Expand All @@ -708,22 +717,20 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
"Setting IO memory failed: %s",
avifResultToString(result)
);
avifDecoderDestroy(self->decoder);
self->decoder = NULL;
Py_DECREF(self);
avifDecoderDestroy(decoder);
PyObject_Del(self);
return NULL;
}

result = avifDecoderParse(self->decoder);
result = avifDecoderParse(decoder);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),
"Failed to decode image: %s",
avifResultToString(result)
);
avifDecoderDestroy(self->decoder);
self->decoder = NULL;
Py_DECREF(self);
avifDecoderDestroy(decoder);
PyObject_Del(self);
return NULL;
}

Expand All @@ -733,6 +740,8 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
self->mode = "RGB";
}

self->decoder = decoder;

return (PyObject *)self;
}

Expand Down

0 comments on commit 853d8c6

Please sign in to comment.