Skip to content

Image format detection false positives with external GD #12019

Closed
@orlitzky

Description

@orlitzky

Description

To check for supported image types, PHP (ext/gd/config.m4) tries to link against libgd:

AC_DEFUN([PHP_GD_CHECK_VERSION],[
  PHP_CHECK_LIBRARY(gd, gdImageCreateFromPng,          [AC_DEFINE(HAVE_GD_PNG,               1, [ ])], [], [ $GD_SHARED_LIBADD ])
  PHP_CHECK_LIBRARY(gd, gdImageCreateFromAvif,         [AC_DEFINE(HAVE_GD_AVIF,              1, [ ])], [], [ $GD_SHARED_LIBADD ])
  ...
])

However, upstream libgd declares those functions unconditionally. Using AVIF as an example, gdImageCreateFromAvif is declared unconditionally at https://github.com/libgd/libgd/blob/master/src/gd.h#L683, and then implemented (when AVIF support is disabled) in https://github.com/libgd/libgd/blob/master/src/gd_avif.c#L674. As a result, PHP decides that it supports AVIF images when it does not.

In particular this causes imagetypes() to return types that PHP does not actually support. It also results in several PHP functions being made available that should not be, e.g.

#ifdef HAVE_GD_AVIF
/* {{{ Create a new image from AVIF file or URL */
PHP_FUNCTION(imagecreatefromavif)
{
        _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF", gdImageCreateFromAvif, gdImageCreateFromAvifCtx);
}
/* }}} */
#endif /* HAVE_GD_AVIF */

Finally, those two issues lead to test failures when format-specific are not skipped as they should be:

$ head ./ext/gd/tests/imagecreatefromstring_avif.phpt 
--TEST--
imagecreatefromstring() - AVIF format
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!(imagetypes() & IMG_AVIF)) {
    die('skip AVIF support required');
}
?>

Thus when some formats are missing from the system gd, there are a few more test failures to add to the list in #11252

PHP Version

git head

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions