[php-src] master: ext/gd: fix undefined behavior with GD2 images with `INT_MAX`-sized chunks

From: Date: Mon, 28 Sep 2026 23:27:17 +0000
Subject: [php-src] master: ext/gd: fix undefined behavior with GD2 images with `INT_MAX`-sized chunks
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141652@lists.php.net to get a copy of this message
Author: Daniel Scherzer (DanielEScherzer) Date: 2026-09-28T15:50:07-07:00 Commit: https://github.com/php/php-src/commit/b8c63871c84cd72b8e2cac5c844605ff5f3c9f5d Raw diff: https://github.com/php/php-src/commit/b8c63871c84cd72b8e2cac5c844605ff5f3c9f5d.diff ext/gd: fix undefined behavior with GD2 images with INT_MAX-sized chunks Apply the changes from libgd/libgd@7ff626c48a133eff1b6608bf28b1cfae30597408 in order to fix undefined behavior when creating a GD2 image when a compressed chunk claims to take INT_MAX space. Changed paths: A ext/gd/tests/createfromstring-overflow.phpt M ext/gd/libgd/gd_gd2.c Diff: diff --git a/ext/gd/libgd/gd_gd2.c b/ext/gd/libgd/gd_gd2.c index 8e0307e5b120..f7e6e514915e 100644 --- a/ext/gd/libgd/gd_gd2.c +++ b/ext/gd/libgd/gd_gd2.c @@ -164,7 +164,7 @@ static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, in gdFree(cidx); goto fail1; } - if (cidx[i].offset < 0 || cidx[i].size < 0) { + if (cidx[i].offset < 0 || cidx[i].size < 0 || cidx[i].size == INT_MAX) { gdFree(cidx); goto fail1; } diff --git a/ext/gd/tests/createfromstring-overflow.phpt b/ext/gd/tests/createfromstring-overflow.phpt new file mode 100644 index 000000000000..e79f380424ec --- /dev/null +++ b/ext/gd/tests/createfromstring-overflow.phpt @@ -0,0 +1,42 @@ +--TEST-- +imagecreatefromstring overflow with compressed chunk of size INT_MAX +--EXTENSIONS-- +gd +--FILE-- +<?php + +// Documentation available at https://libgd.github.io/manuals/2.3.3/files/gd_gd2-c.html +$fileHeaderParts = [ + "signature" => "gd2\x00", + "version" => "\x00\x02", + "width" => "\x00\x01", + "height" => "\x00\x01", + "chunk_size" => "\x00\x40", + "format" => "\x00\x04", // compressed truecolor image data + "x_chunk_count" => "\x00\x01", + "y_chunk_count" => "\x00\x01", +]; +$fileHeader = implode("", $fileHeaderParts); + +$chunkHeaderParts = [ + "offset" => "\x00\x00\x00\x20", + "size" =>"\x7F\xFF\xFF\xFF", // INT_MAX +]; +$chunkHeader = implode("", $chunkHeaderParts); + +$trueColorHeaderParts = [ + "truecolor" => "\x01", + "transparent" => "\x00\x00\x00\x00", +]; +$trueColorHeader = implode("", $trueColorHeaderParts); + +$source = $fileHeader . $chunkHeader . $trueColorHeader; +$img = imagecreatefromstring($source); +var_dump($img); + +?> +--EXPECTF-- +Warning: imagecreatefromstring(): Passed data is not in "GD2" format in %s on line %d + +Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in %s on line %d +bool(false)

« previous php.cvs (#141652) next »