From 16966f2387824d549fd171a25230ffbd3faa7eb3 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 14 Oct 2024 11:35:13 +0200 Subject: [PATCH] Check libavif API return values, if available --- ext/gd/libgd/gd_avif.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ext/gd/libgd/gd_avif.c b/ext/gd/libgd/gd_avif.c index 30075d2a899c8..9c1ffdc34bc68 100644 --- a/ext/gd/libgd/gd_avif.c +++ b/ext/gd/libgd/gd_avif.c @@ -393,7 +393,13 @@ gdImagePtr gdImageCreateFromAvifCtx (gdIOCtx *ctx) // (While AVIF image pixel depth can be 8, 10, or 12 bits, GD truecolor images are 8-bit.) avifRGBImageSetDefaults(&rgb, decoder->image); rgb.depth = 8; +#if AVIF_VERSION >= 1000000 + result = avifRGBImageAllocatePixels(&rgb); + if (isAvifError(result, "Allocating RGB pixels failed")) + goto cleanup; +#else avifRGBImageAllocatePixels(&rgb); +#endif result = avifImageYUVToRGB(decoder->image, &rgb); if (isAvifError(result, "Conversion from YUV to RGB failed")) @@ -522,14 +528,25 @@ void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed) // Note that MATRIX_COEFFICIENTS_IDENTITY enables lossless conversion from RGB to YUV. avifImage *avifIm = avifImageCreate(gdImageSX(im), gdImageSY(im), 8, subsampling); - +#if AVIF_VERSION >= 1000000 + if (avifIm == NULL) { + gd_error("avif error - Creating image failed\n"); + goto cleanup; + } +#endif avifIm->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709; avifIm->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB; avifIm->matrixCoefficients = lossless ? AVIF_MATRIX_COEFFICIENTS_IDENTITY : AVIF_MATRIX_COEFFICIENTS_BT709; avifRGBImageSetDefaults(&rgb, avifIm); // this allocates memory, and sets rgb.rowBytes and rgb.pixels. +#if AVIF_VERSION >= 1000000 + result = avifRGBImageAllocatePixels(&rgb); + if (isAvifError(result, "Allocating RGB pixels failed")) + goto cleanup; +#else avifRGBImageAllocatePixels(&rgb); +#endif // Parse RGB data from the GD image, and copy it into the AVIF RGB image. // Convert 7-bit GD alpha channel values to 8-bit AVIF values. @@ -555,6 +572,12 @@ void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed) // Encode the image in AVIF format. encoder = avifEncoderCreate(); +#if AVIF_VERSION >= 1000000 + if (encoder == NULL) { + gd_error("avif error - Creating encoder failed\n"); + goto cleanup; + } +#endif int quantizerQuality = quality == QUALITY_DEFAULT ? QUANTIZER_DEFAULT : quality2Quantizer(quality);