Commit 75802f2cd56 for php

commit 75802f2cd5648c686a4b09f9b4eabd12e96a0c97
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date:   Sun Nov 8 07:28:42 2026 -0500

    ext/standard: Reject out-of-range XBM dimensions

    XBM width and height were read with sscanf("%d"), whose behaviour on
    overflow is undefined; glibc wraps 4294967298 to 2, so getimagesize()
    reported a width of 2, and negative values were reported as negative
    dimensions. Parse the value with strtol() and accept only 1 to INT_MAX.

    Closes GH-25293

diff --git a/NEWS b/NEWS
index 974c95c1d22..2b369fadce4 100644
--- a/NEWS
+++ b/NEWS
@@ -157,6 +157,8 @@ PHP                                                                        NEWS
   . Fixed bug GH-24441 (quoted_printable_encode() produces lines longer than
     76 characters for bytes 0xF5 to 0xFF). (Sebastian Turner,
     Lazizbek Ergashev)
+  . Fixed getimagesize() reporting wrapped dimensions for XBM images with
+    out-of-range width or height. (Ilia Alshanetsky)

 - Streams:
   . Fixed bug GH-22063 (Stream filter chain UAF on self-removal during
diff --git a/ext/standard/image.c b/ext/standard/image.c
index f58569495d3..58795384f81 100644
--- a/ext/standard/image.c
+++ b/ext/standard/image.c
@@ -1000,7 +1000,9 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
 	char *fline;
 	char *iname;
 	char *type;
-	int value;
+	char *end;
+	int offset;
+	long value;
 	unsigned int width = 0, height = 0;

 	if (result) {
@@ -1017,25 +1019,29 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
 			break;
 		}
 		iname = estrdup(fline); /* simple way to get necessary buffer of required size */
-		if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
-			if (!(type = strrchr(iname, '_'))) {
-				type = iname;
-			} else {
-				type++;
-			}
+		if (sscanf(fline, "#define %s %n", iname, &offset) == 1) {
+			errno = 0;
+			value = strtol(fline + offset, &end, 10);
+			if (end != fline + offset && errno == 0 && value > 0 && value <= INT_MAX) {
+				if (!(type = strrchr(iname, '_'))) {
+					type = iname;
+				} else {
+					type++;
+				}

-			if (!strcmp("width", type)) {
-				width = (unsigned int) value;
-				if (height) {
-					efree(iname);
-					break;
+				if (!strcmp("width", type)) {
+					width = (unsigned int) value;
+					if (height) {
+						efree(iname);
+						break;
+					}
 				}
-			}
-			if (!strcmp("height", type)) {
-				height = (unsigned int) value;
-				if (width) {
-					efree(iname);
-					break;
+				if (!strcmp("height", type)) {
+					height = (unsigned int) value;
+					if (width) {
+						efree(iname);
+						break;
+					}
 				}
 			}
 		}
diff --git a/ext/standard/tests/image/xbm_out_of_range.phpt b/ext/standard/tests/image/xbm_out_of_range.phpt
new file mode 100644
index 00000000000..1252b00c945
--- /dev/null
+++ b/ext/standard/tests/image/xbm_out_of_range.phpt
@@ -0,0 +1,21 @@
+--TEST--
+getimagesize() rejects out-of-range XBM dimensions
+--FILE--
+<?php
+$path = __DIR__ . '/xbm_out_of_range.tmp.xbm';
+foreach ([['4294967298', '1'], ['1', '4294967298'], ['2147483648', '1'], ['-5', '1'], ['2147483647', '1']] as [$w, $h]) {
+    file_put_contents($path, "#define x_width $w\n#define x_height $h\nstatic char x_bits[] = {\n0x00 };\n");
+    $info = getimagesize($path);
+    echo "$w x $h: ", $info ? $info[3] : 'false', "\n";
+}
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/xbm_out_of_range.tmp.xbm');
+?>
+--EXPECT--
+4294967298 x 1: false
+1 x 4294967298: false
+2147483648 x 1: false
+-5 x 1: false
+2147483647 x 1: width="2147483647" height="1"