Commit 3a82d70397b for php

commit 3a82d70397bb8ab175b8e3688df4e53dbb083351
Author: lazerg <lazerg2@gmail.com>
Date:   Thu Nov 26 17:06:29 2026 +0800

    Fix GH-25360: ZipArchive assertion when error handler throws at shutdown (#25361)

    If zip_close() fails during shutdown and the warning handler throws,
    the bailout interrupts archive release after the refcount drops to zero.
    The object still points to the archive, so free_storage tries closing
    it again and hits an assertion.

    Catch bailouts from close warnings in php_zip_archive_release() and
    php_zipobj_close() and let the existing bailout path run after cleanup.
    The second guard also lets free_storage discard the libzip handle when
    the object destructor did not run.

    Closes #25361

diff --git a/NEWS b/NEWS
index 47b729fab04..50c3719a3cc 100644
--- a/NEWS
+++ b/NEWS
@@ -280,6 +280,10 @@ PHP                                                                        NEWS
   . Fixed a crash in XSLTProcessor::transformToDoc() when the return class
     cannot be instantiated. (Ilia Alshanetsky)

+- Zip:
+  . Fixed bug GH-25360 (ZipArchive assertion when an error handler throws
+    on implicit close at shutdown). (lazerg)
+
 19 Nov 2026, PHP 8.6.0

 - Core:
diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c
index 5baecfc1893..14ee1bf3b82 100644
--- a/ext/zip/php_zip.c
+++ b/ext/zip/php_zip.c
@@ -660,7 +660,11 @@ static bool php_zipobj_close(ze_zip_object *obj, zend_string **out_str) /* {{{ *
 		archive->close = true;
 		int err = zip_close(intern);
 		if (err) {
-			php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
+			zend_try {
+				php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
+			} zend_catch {
+				archive->bailout_callback = true;
+			} zend_end_try();
 			/* Save error for property reader */
 			zip_error_t *ziperr = zip_get_error(intern);
 			obj->err_zip = zip_error_code_zip(ziperr);
@@ -1184,7 +1188,12 @@ bool php_zip_archive_release(php_zip_archive *archive)
 		int err = zip_close(archive->za);
 		if (err != 0) {
 			if (!archive->bailout_callback) {
-				php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(archive->za));
+				/* An error handler that throws during shutdown bails out. */
+				zend_try {
+					php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(archive->za));
+				} zend_catch {
+					archive->bailout_callback = true;
+				} zend_end_try();
 			}
 			zip_discard(archive->za);
 		}
diff --git a/ext/zip/tests/gh25360.phpt b/ext/zip/tests/gh25360.phpt
new file mode 100644
index 00000000000..51b297251ec
--- /dev/null
+++ b/ext/zip/tests/gh25360.phpt
@@ -0,0 +1,31 @@
+--TEST--
+GH-25360 (Assertion failure when the error handler throws on the implicit close at shutdown)
+--CREDITS--
+YuanchengJiang
+--EXTENSIONS--
+zip
+--FILE--
+<?php
+$zip = new ZipArchive();
+$zip->open(__DIR__ . '/gh25360.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
+file_put_contents(__DIR__ . '/gh25360.txt', 'contents');
+$zip->addFile(__DIR__ . '/gh25360.txt', 'file.txt');
+unlink(__DIR__ . '/gh25360.txt');
+set_error_handler(function () {
+    throw new Exception('handler');
+});
+echo "Done\n";
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/gh25360.zip');
+@unlink(__DIR__ . '/gh25360.txt');
+?>
+--EXPECTF--
+Done
+
+Fatal error: Uncaught Exception: handler in %s:%d
+Stack trace:
+#0 [internal function]: {closure:%s:%d}(2, '%s', 'Unknown', 0)
+#1 {main}
+  thrown in %s on line %d