Commit 9d13beeef3f for php

commit 9d13beeef3fda5eb23f802ad45afb7ff2f51ed7f
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date:   Mon Nov 30 17:47:02 2026 +0100

    Fix NULL dereference in php_init_config() for unresolvable search path ini (GH-25882)

    php_fopen_with_path() may open the file but leave opened_path NULL when
    the absolute path exceeds MAXPATHLEN. Reject the file in that case so
    the search continues with the next directory, as on ZTS, and as
    c88c89e351c does for -c naming a file.

    Closes GH-25882

diff --git a/NEWS b/NEWS
index d0e9f3cffd9..2767862cb8c 100644
--- a/NEWS
+++ b/NEWS
@@ -45,6 +45,8 @@ PHP                                                                        NEWS
   . Fix OSS-Fuzz #588264417. (ndossche)
   . Fixed bug GH-24701 (Use-after-free on typed trait constants during
     opcache preload). (sanklamm, lazerg)
+  . Fixed NULL dereference at startup when the ini file found in the search
+    path has an absolute path exceeding MAXPATHLEN. (Jorg Sowa)

 - Curl:
   . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c
index c06bada1112..1260c86636d 100644
--- a/main/fopen_wrappers.c
+++ b/main/fopen_wrappers.c
@@ -355,10 +355,13 @@ static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, z
 	if (fp && opened_path) {
 		//TODO :avoid reallocation
 		char *tmp = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND);
-		if (tmp) {
-			*opened_path = zend_string_init(tmp, strlen(tmp), 0);
-			efree(tmp);
+		if (!tmp) {
+			/* Reject the file, like ZTS where VCWD_FOPEN() already fails */
+			fclose(fp);
+			return NULL;
 		}
+		*opened_path = zend_string_init(tmp, strlen(tmp), 0);
+		efree(tmp);
 	}
 	return fp;
 }
diff --git a/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt b/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt
new file mode 100644
index 00000000000..d2db38ad25f
--- /dev/null
+++ b/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt
@@ -0,0 +1,29 @@
+--TEST--
+php-cli.ini in search path is skipped when its absolute path exceeds MAXPATHLEN
+--SKIPIF--
+<?php
+include "skipif.inc";
+if (PHP_OS_FAMILY === "Windows") die("skip not for Windows");
+?>
+--FILE--
+<?php
+$dir = __DIR__ . "/ini_search_path_sapi_unresolvable";
+@mkdir($dir);
+file_put_contents("$dir/php-cli.ini", "foo=bar\n");
+
+$relative = str_repeat("./", intdiv(PHP_MAXPATHLEN - strlen($dir), 2));
+$proc = proc_open(
+    [getenv("TEST_PHP_EXECUTABLE"), "-c", $relative . PATH_SEPARATOR . ".", "-r", 'var_dump(get_cfg_var("foo"));'],
+    [1 => ["pipe", "w"]],
+    $pipes,
+    $dir
+);
+echo stream_get_contents($pipes[1]);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . "/ini_search_path_sapi_unresolvable/php-cli.ini");
+@rmdir(__DIR__ . "/ini_search_path_sapi_unresolvable");
+?>
+--EXPECT--
+string(3) "bar"
diff --git a/sapi/cli/tests/ini_search_path_unresolvable.phpt b/sapi/cli/tests/ini_search_path_unresolvable.phpt
new file mode 100644
index 00000000000..82bb059897d
--- /dev/null
+++ b/sapi/cli/tests/ini_search_path_unresolvable.phpt
@@ -0,0 +1,29 @@
+--TEST--
+php.ini in search path is skipped when its absolute path exceeds MAXPATHLEN
+--SKIPIF--
+<?php
+include "skipif.inc";
+if (PHP_OS_FAMILY === "Windows") die("skip not for Windows");
+?>
+--FILE--
+<?php
+$dir = __DIR__ . "/ini_search_path_unresolvable";
+@mkdir($dir);
+file_put_contents("$dir/php.ini", "foo=bar\n");
+
+$relative = str_repeat("./", intdiv(PHP_MAXPATHLEN - strlen($dir), 2));
+$proc = proc_open(
+    [getenv("TEST_PHP_EXECUTABLE"), "-c", $relative . PATH_SEPARATOR . ".", "-r", 'var_dump(get_cfg_var("foo"));'],
+    [1 => ["pipe", "w"]],
+    $pipes,
+    $dir
+);
+echo stream_get_contents($pipes[1]);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . "/ini_search_path_unresolvable/php.ini");
+@rmdir(__DIR__ . "/ini_search_path_unresolvable");
+?>
+--EXPECT--
+string(3) "bar"