Package: src:libgd2
Version: 2.3.3-14
Severity: normal
Tags: upstream patch
Forwarded: https://github.com/libgd/libgd/pull/832
Dear Maintainer,
Repeated calls to gdImageAvifCtx() (or imageavif() via PHP bindings) in a loop
result in a continuous, linear leak of native C heap memory (~1.6 MB per call).
Over a batch run of 500-1,000 images, memory accumulates into multiple gigabytes
until the process is killed by the Linux OOM killer. Calling malloc_trim(0) does
not reclaim this memory.
=== Root Cause ===
In src/gd_avif.c, gdImageAvifCtx allocates an avifImage struct:
avifIm = avifImageCreate(gdImageSX(im), gdImageSY(im), 8, subsampling);
In the cleanup block of _gdImageAvifCtx(), avifImageDestroy(avifIm) is missing.
This was reported upstream in libgd/libgd#831 and fixed in upstream PR #832:
https://github.com/libgd/libgd/pull/832
Commit: Fix #831: gdImageAvif memory leak
PHP also backported this into its bundled copy in commit 036bed01ce:
https://github.com/php/php-src/commit/036bed01ce
However, upstream LibGD has not tagged any new release since 2.3.3 (2021).
Because Debian libgd2 (2.3.3-14) is built from the 2.3.3 source tarball,
Debian's libgd.so.3 is missing this fix.
Inspecting the dynamic symbol table of Debian's
/usr/lib/x86_64-linux-gnu/libgd.so.3
confirms that avifImageCreate is imported, but avifImageDestroy is absent:
$ objdump -T /usr/lib/x86_64-linux-gnu/libgd.so.3 | grep -i avifImage
0000000000000000 DF *UND* 0000000000000000 Base
avifImageYUVToRGB
0000000000000000 DF *UND* 0000000000000000 Base
avifImageCreate
0000000000000000 DF *UND* 0000000000000000 Base
avifImageRGBToYUV
=== Minimal Reproducer (PHP CLI) ===
<?php
for ($i = 1; $i <= 50; $i++) {
$img = imagecreatetruecolor(800, 800);
$tmp = "/tmp/test_{$i}.avif";
imageavif($img, $tmp, 80);
imagedestroy($img);
@unlink($tmp);
}
During this loop, process Resident Set Size (VmRSS via /proc/self/status) grows
by ~80 MB (~1.6 MB/image) while Zend emalloc stays flat at 2 MB.
By comparison, imagewebp() and imagejpeg() have 0 bytes leaked under identical
conditions.
=== Proposed Fix ===
Please cherry-pick upstream PR #832 into debian/patches for src:libgd2:
https://github.com/libgd/libgd/pull/832
This adds `if (avifIm) avifImageDestroy(avifIm);` to the cleanup block in
src/gd_avif.c.
References:
- Upstream LibGD Issue: https://github.com/libgd/libgd/issues/831
- Upstream LibGD PR: https://github.com/libgd/libgd/pull/832
- PHP Core Issue: https://github.com/php/php-src/issues/25442