Skip to content

Commit b817a4f

Browse files
committed
Fix GH-16427: Unchecked libavif return values
Prior to libavif 1.1.0, `avifAlloc()` was infallible (it called `abort()` on OOM conditions); thus, several API functions which used `avifAlloc()` did not report failure. That changed as of libavif 1.0.0[1], so checking and handling failure conditions can now be done. However, due to `avifAlloc()` being fallible as of libavif 1.1.0, this error checking and handling is mandatory to avoid more serious issues. [1] <https://github.com/AOMediaCodec/libavif/blob/eb02b2ec52df5c0f50b71fbc51321c5ce435aaca/CHANGELOG.md?plain=1#L273-L281> Closes GH-16434.
1 parent 06efe44 commit b817a4f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ PHP NEWS
2828
- GD:
2929
. Fixed bug GH-16334 (imageaffine overflow on matrix elements).
3030
(David Carlier)
31+
. Fixed bug GH-16427 (Unchecked libavif return values). (cmb)
3132

3233
- GMP:
3334
. Fixed floating point exception bug with gmp_pow when using

ext/gd/libgd/gd_avif.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ gdImagePtr gdImageCreateFromAvifCtx (gdIOCtx *ctx)
393393
// (While AVIF image pixel depth can be 8, 10, or 12 bits, GD truecolor images are 8-bit.)
394394
avifRGBImageSetDefaults(&rgb, decoder->image);
395395
rgb.depth = 8;
396+
#if AVIF_VERSION >= 1000000
397+
result = avifRGBImageAllocatePixels(&rgb);
398+
if (isAvifError(result, "Allocating RGB pixels failed"))
399+
goto cleanup;
400+
#else
396401
avifRGBImageAllocatePixels(&rgb);
402+
#endif
397403

398404
result = avifImageYUVToRGB(decoder->image, &rgb);
399405
if (isAvifError(result, "Conversion from YUV to RGB failed"))
@@ -522,14 +528,25 @@ void gdImageAvifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, int speed)
522528
// Note that MATRIX_COEFFICIENTS_IDENTITY enables lossless conversion from RGB to YUV.
523529

524530
avifImage *avifIm = avifImageCreate(gdImageSX(im), gdImageSY(im), 8, subsampling);
525-
531+
#if AVIF_VERSION >= 1000000
532+
if (avifIm == NULL) {
533+
gd_error("avif error - Creating image failed\n");
534+
goto cleanup;
535+
}
536+
#endif
526537
avifIm->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709;
527538
avifIm->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB;
528539
avifIm->matrixCoefficients = lossless ? AVIF_MATRIX_COEFFICIENTS_IDENTITY : AVIF_MATRIX_COEFFICIENTS_BT709;
529540

530541
avifRGBImageSetDefaults(&rgb, avifIm);
531542
// this allocates memory, and sets rgb.rowBytes and rgb.pixels.
543+
#if AVIF_VERSION >= 1000000
544+
result = avifRGBImageAllocatePixels(&rgb);
545+
if (isAvifError(result, "Allocating RGB pixels failed"))
546+
goto cleanup;
547+
#else
532548
avifRGBImageAllocatePixels(&rgb);
549+
#endif
533550

534551
// Parse RGB data from the GD image, and copy it into the AVIF RGB image.
535552
// 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)
555572
// Encode the image in AVIF format.
556573

557574
encoder = avifEncoderCreate();
575+
#if AVIF_VERSION >= 1000000
576+
if (encoder == NULL) {
577+
gd_error("avif error - Creating encoder failed\n");
578+
goto cleanup;
579+
}
580+
#endif
558581
int quantizerQuality = quality == QUALITY_DEFAULT ?
559582
QUANTIZER_DEFAULT : quality2Quantizer(quality);
560583

0 commit comments

Comments
 (0)