[php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Daniel Scherzer | Date: | Wed, 30 Sep 2026 01:00:54 +0000 |
| Subject: | [php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141749@lists.php.net to get a copy of this message | ||
Author: Daniel Scherzer (DanielEScherzer)
Date: 2026-09-29T17:58:25-07:00
Commit: https://github.com/php/php-src/commit/f8be041aaa538e12f74d541ee1b12325892032e6
Raw diff: https://github.com/php/php-src/commit/f8be041aaa538e12f74d541ee1b12325892032e6.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
NEWS
ext/gd: fix undefined behavior with GIFs with problematic LZW compression data
Changed paths:
A ext/gd/tests/gif-oob.phpt
M NEWS
M ext/gd/libgd/gd_gif_in.c
Diff:
diff --git a/NEWS b/NEWS
index 6fe75ef13e92..530749ccd7d2 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,8 @@ PHP NEWS
- GD:
. Fixed undefined behavior with GD2 images with
INT_MAX-sized chunks.
(DanielEScherzer, cmb)
+ . Fixed undefined behavior with GIFs with problematic LZW compression data.
+ (DanielEScherzer, vapier)
- Intl:
. Fixed cloning IntlDateFormatter and MessageFormatter losing PHP-side state
diff --git a/ext/gd/libgd/gd_gif_in.c b/ext/gd/libgd/gd_gif_in.c
index 14c27f3293cc..f2d9981688a8 100644
--- a/ext/gd/libgd/gd_gif_in.c
+++ b/ext/gd/libgd/gd_gif_in.c
@@ -517,12 +517,20 @@ LWZReadByte_(gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size,
i
/* Bad compressed data stream */
return -1;
}
+ if(code >= (1 << MAX_LWZ_BITS)) {
+ /* Corrupted code */
+ return -1;
+ }
*sd->sp++ = sd->table[1][code];
if (code == sd->table[0][code]) {
/* Oh well */
}
code = sd->table[0][code];
}
+ if(code >= (1 << MAX_LWZ_BITS)) {
+ /* Corrupted code */
+ return -1;
+ }
*sd->sp++ = sd->firstcode = sd->table[1][code];
diff --git a/ext/gd/tests/gif-oob.phpt b/ext/gd/tests/gif-oob.phpt
new file mode 100644
index 000000000000..73fd0427c8e8
--- /dev/null
+++ b/ext/gd/tests/gif-oob.phpt
@@ -0,0 +1,63 @@
+--TEST--
+GIF OOB array access when using code size of 12
+--EXTENSIONS--
+gd
+--FILE--
+<?php
+
+$fileHeaderParts = [
+ "signature" => "GIF",
+ "version" => "89a",
+];
+$fileHeader = implode("", $fileHeaderParts);
+
+$logicalScreenDescriptorParts = [
+ // little-endian format
+ "width" => "\x04\x00",
+ "height" => "\x04\x00",
+ // packed data: global color table flag (most significant bit),
+ // color resolution (3 bits, only meaningful if global color table is enabled
+ // and we don't enable it here)
+ // sort flag (one bit, again only meaningful if global color table is enabled)
+ // size of global color table (3 bits)
+ "packed_info" => "\x00",
+ "background_color_index" => "\x00",
+ "pixel_aspect_ratio" => "\x00",
+];
+$logicalScreenDescriptor = implode("", $logicalScreenDescriptorParts);
+
+$startImage = ",";
+
+$imgDescParts = [
+ // little-ending format
+ "left" => "\x00\x00",
+ "top" => "\x00\x00",
+ "width" => "\x04\x00",
+ "height" => "\x04\x00",
+ // more packed data: local color table flag, interlace flag, sort flag,
+ // 2 bits reserved for future use, then 3 bits for size of local color
+ // table, which we don't have
+ "packed_info" => "\x00",
+];
+$imgDesc = implode("", $imgDescParts);
+
+$imgDataParts = [
+ "lzw_min_code_size" => "\x0c", // 12
+ "sub_block_num_bytes" => "\x05",
+ // Data in the block: 3 12-bit codes, and then 4 trailing 0 bits
+ "sub_block_bytes" => "\xff\x5f\x00\x06\x40",
+ // end of data
+ "end" => "\x00"
+];
+$imgData = implode("", $imgDataParts);
+
+$trailer = ";";
+
+$source = $fileHeader . $logicalScreenDescriptor . $startImage . $imgDesc . $imgData . $trailer;
+$img = imagecreatefromstring($source);
+var_dump($img);
+
+?>
+--EXPECTF--
+object(GdImage)#%d (0) {
+}