Commit ae961499489 for php

commit ae961499489e6672db10573935d5463d798ecf47
Author: Arnaud Le Blanc <arnaud.lb@gmail.com>
Date:   Tue Nov 10 12:12:55 2026 +0100

    Fix GHSA-2fjv-p558-g82m: do not set last_fatal_error_backtrace during shutdown

    Avoid setting EG(last_fatal_error_backtrace) during shutdown, as this may not be
    cleared before the next request. EG(last_fatal_error_backtrace) is invalid when
    the executor is inactive, so don't consume it.

    Co-authored-by: Jakub Zelenka <bukka@php.net>

diff --git a/Zend/zend.c b/Zend/zend.c
index a0c487aff00..a445456c0a5 100644
--- a/Zend/zend.c
+++ b/Zend/zend.c
@@ -1507,7 +1507,9 @@ ZEND_API ZEND_COLD void zend_error_zstr_at(
 	}

 	// Always clear the last backtrace.
-	zval_ptr_dtor(&EG(last_fatal_error_backtrace));
+	if (EG(active)) {
+		zval_ptr_dtor(&EG(last_fatal_error_backtrace));
+	}
 	ZVAL_UNDEF(&EG(last_fatal_error_backtrace));

 	/* Report about uncaught exception in case of fatal errors */
@@ -1532,7 +1534,11 @@ ZEND_API ZEND_COLD void zend_error_zstr_at(
 			}
 		}
 	} else if (EG(fatal_error_backtrace_on) && (type & E_FATAL_ERRORS)) {
-		zend_fetch_debug_backtrace(&EG(last_fatal_error_backtrace), 0, EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
+		// EG(last_fatal_error_backtrace) is useless when the executor is
+		// inactive, and setting it at this point would leak.
+		if (EG(active)) {
+			zend_fetch_debug_backtrace(&EG(last_fatal_error_backtrace), 0, EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
+		}
 	}

 	zend_observer_error_notify(type, error_filename, error_lineno, message);
diff --git a/main/main.c b/main/main.c
index 0db01692170..58f75a924c1 100644
--- a/main/main.c
+++ b/main/main.c
@@ -1415,7 +1415,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
 		}
 	}

-	if (!Z_ISUNDEF(EG(last_fatal_error_backtrace))) {
+	if (EG(active) && !Z_ISUNDEF(EG(last_fatal_error_backtrace))) {
 		backtrace = zend_trace_to_string(Z_ARRVAL(EG(last_fatal_error_backtrace)), /* include_main */ true);
 	}

diff --git a/sapi/fpm/tests/ghsa-2fjv-p558-g82m-memory-limit.phpt b/sapi/fpm/tests/ghsa-2fjv-p558-g82m-memory-limit.phpt
new file mode 100644
index 00000000000..0285f76d5df
--- /dev/null
+++ b/sapi/fpm/tests/ghsa-2fjv-p558-g82m-memory-limit.phpt
@@ -0,0 +1,76 @@
+--TEST--
+FPM: late memory limit shutdown error does not recreate the fatal error backtrace
+--SKIPIF--
+<?php
+include "skipif.inc";
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+?>
+--FILE--
+<?php
+require_once "tester.inc";
+
+$cfg = <<<EOT
+[global]
+error_log = {{FILE:LOG}}
+[unconfined]
+listen = {{ADDR:UDS}}
+pm = static
+pm.max_children = 1
+EOT;
+
+$code = <<<'PHP'
+<?php
+if ($_SERVER['QUERY_STRING'] === 'producer') {
+    // The last error message is only freed after the INI entries are restored,
+    // so this keeps the allocation alive even without fast shutdown (debug builds).
+    @trigger_error(str_repeat('x', 20 * 1024 * 1024));
+}
+echo getmypid(), "\n", date_default_timezone_get();
+PHP;
+
+$testDir = __DIR__ . '/ghsa-2fjv-p558-g82m-memory-limit-data';
+mkdir($testDir);
+file_put_contents($testDir . '/producer.php', $code);
+file_put_contents($testDir . '/php.ini', "[PATH=$testDir]\nmax_memory_limit=64M\nmemory_limit=64M\n");
+
+$tester = new FPM\Tester($cfg, $code);
+$tester->start(extraArgs: ['-c', $testDir . '/php.ini'], iniEntries: [
+    'fatal_error_backtraces' => '1',
+    'display_errors' => '0',
+    'log_errors' => '1',
+    'date.timezone' => 'UTC',
+    'max_memory_limit' => '8M',
+    'memory_limit' => '8M',
+]);
+$tester->expectLogStartNotices();
+$body = $tester->request(address: '{{ADDR:UDS}}')->expectError(null)->getBody();
+var_dump(preg_match('/^\d+\nUTC$/D', $body) === 1);
+
+// Restoring the global maximum fails while the request allocation still exists.
+$tester->request(query: 'producer', address: '{{ADDR:UDS}}', scriptFilename: $testDir . '/producer.php')
+->expectBody($body)->expectErrorPattern(
+    '/\APHP message: PHP Fatal error:  Failed to set memory limit to 8388608 bytes '
+        . '\(Current memory usage is \d+ bytes\) in Unknown on line 0\z/'
+);
+
+$tester->request(address: '{{ADDR:UDS}}', headers: ['PHP_VALUE' => 'date.timezone=invalid'])
+    ->expectBody($body)
+    ->expectError("PHP message: PHP Warning:  PHP Request Shutdown: "
+        . "Invalid date.timezone value 'invalid', using 'UTC' instead in Unknown on line 0");
+$tester->terminate();
+$tester->expectLogTerminatingNotices();
+$tester->close();
+?>
+Done
+--EXPECT--
+bool(true)
+Done
+--CLEAN--
+<?php
+require_once "tester.inc";
+FPM\Tester::clean();
+$testDir = __DIR__ . '/ghsa-2fjv-p558-g82m-memory-limit-data';
+@unlink($testDir . '/producer.php');
+@unlink($testDir . '/php.ini');
+@rmdir($testDir);
+?>
diff --git a/sapi/fpm/tests/ghsa-2fjv-p558-g82m-zlib.phpt b/sapi/fpm/tests/ghsa-2fjv-p558-g82m-zlib.phpt
new file mode 100644
index 00000000000..3a2d44e61eb
--- /dev/null
+++ b/sapi/fpm/tests/ghsa-2fjv-p558-g82m-zlib.phpt
@@ -0,0 +1,70 @@
+--TEST--
+FPM: late zlib shutdown error does not recreate the fatal error backtrace
+--EXTENSIONS--
+zlib
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+require_once "tester.inc";
+
+$cfg = <<<EOT
+[global]
+error_log = {{FILE:LOG}}
+[unconfined]
+listen = {{ADDR:UDS}}
+pm = static
+pm.max_children = 1
+EOT;
+
+$code = <<<'PHP'
+<?php
+echo getmypid(), "\n", date_default_timezone_get();
+PHP;
+
+$testDir = __DIR__ . '/ghsa-2fjv-p558-g82m-zlib-data';
+mkdir($testDir);
+file_put_contents($testDir . '/producer.php', $code);
+file_put_contents($testDir . '/php.ini', "[PATH=$testDir]\nzlib.output_compression=0\noutput_handler=ob_gzhandler\n");
+
+$tester = new FPM\Tester($cfg, $code);
+$tester->start(extraArgs: ['-c', $testDir . '/php.ini'], extensions: ['zlib'], iniEntries: [
+    'fatal_error_backtraces' => '1',
+    'display_errors' => '0',
+    'log_errors' => '1',
+    'date.timezone' => 'UTC',
+    'zlib.output_compression' => '1',
+]);
+$tester->expectLogStartNotices();
+$body = $tester->request(address: '{{ADDR:UDS}}')->expectError(null)->getBody();
+var_dump(preg_match('/^\d+\nUTC$/D', $body) === 1);
+
+// Restore compression before output_handler to raise a late shutdown error.
+$tester->request(address: '{{ADDR:UDS}}', scriptFilename: $testDir . '/producer.php')
+->expectBody($body)->expectError(
+    'PHP message: PHP Fatal error:  PHP Request Shutdown: '
+        . 'Cannot use both zlib.output_compression and output_handler together!! in Unknown on line 0'
+);
+
+// This diagnostic occurs before the next request's executor initialization.
+$tester->request(address: '{{ADDR:UDS}}', headers: ['PHP_VALUE' => 'date.timezone=invalid'])
+    ->expectBody($body)
+    ->expectError("PHP message: PHP Warning:  PHP Request Shutdown: "
+        . "Invalid date.timezone value 'invalid', using 'UTC' instead in Unknown on line 0");
+$tester->terminate();
+$tester->expectLogTerminatingNotices();
+$tester->close();
+?>
+Done
+--EXPECT--
+bool(true)
+Done
+--CLEAN--
+<?php
+require_once "tester.inc";
+FPM\Tester::clean();
+$testDir = __DIR__ . '/ghsa-2fjv-p558-g82m-zlib-data';
+@unlink($testDir . '/producer.php');
+@unlink($testDir . '/php.ini');
+@rmdir($testDir);
+?>