[php-src] master: Merge branch 'PHP-8.3'
| From: | Ilija Tovilo | Date: | Wed, 20 Mar 2024 10:08:06 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.3' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127805@lists.php.net to get a copy of this message | ||
Author: Ilija Tovilo (iluuu1994)
Date: 2024-03-20T10:53:35+01:00
Commit: https://github.com/php/php-src/commit/bc59e79d21118ea5459a6417ad95e99c775ec8ff
Raw diff: https://github.com/php/php-src/commit/bc59e79d21118ea5459a6417ad95e99c775ec8ff.diff
Merge branch 'PHP-8.3'
* PHP-8.3:
Restore error handler after running it
Changed paths:
A Zend/tests/gh13446_1.phpt
A Zend/tests/gh13446_2.phpt
A Zend/tests/gh13446_3.phpt
A Zend/tests/gh13446_4.phpt
M Zend/zend.c
Diff:
diff --git a/Zend/tests/gh13446_1.phpt b/Zend/tests/gh13446_1.phpt
new file mode 100644
index 000000000000..cb51d041c969
--- /dev/null
+++ b/Zend/tests/gh13446_1.phpt
@@ -0,0 +1,19 @@
+--TEST--
+GH-13446: Exception handler is restored after is has finished
+--FILE--
+<?php
+function exception_handler($ex) {
+ echo 'Exception caught: ', $ex->getMessage(), "\n";
+}
+set_exception_handler('exception_handler');
+
+register_shutdown_function(function () {
+ echo set_exception_handler(null), "\n";
+ restore_exception_handler();
+});
+
+throw new Exception('Test');
+?>
+--EXPECT--
+Exception caught: Test
+exception_handler
diff --git a/Zend/tests/gh13446_2.phpt b/Zend/tests/gh13446_2.phpt
new file mode 100644
index 000000000000..c1bab44ce80b
--- /dev/null
+++ b/Zend/tests/gh13446_2.phpt
@@ -0,0 +1,16 @@
+--TEST--
+GH-13446: Exception handler attempting to free itself
+--FILE--
+<?php
+$x = new \stdClass();
+$handler = function ($ex) use (&$handler, $x) {
+ $handler = null;
+ var_dump($x);
+};
+unset($x);
+set_exception_handler($handler);
+throw new Exception('Unhandled');
+?>
+--EXPECT--
+object(stdClass)#1 (0) {
+}
diff --git a/Zend/tests/gh13446_3.phpt b/Zend/tests/gh13446_3.phpt
new file mode 100644
index 000000000000..eb56eb1b32d8
--- /dev/null
+++ b/Zend/tests/gh13446_3.phpt
@@ -0,0 +1,25 @@
+--TEST--
+GH-13446: Exception handler isn't restored if it is previously modified
+--FILE--
+<?php
+function exception_handler_1($ex) {
+ echo "Handler 1\n";
+ set_exception_handler('exception_handler_2');
+}
+
+function exception_handler_2($ex) {
+ echo "Handler 2\n";
+}
+
+set_exception_handler('exception_handler_1');
+
+register_shutdown_function(function () {
+ echo set_exception_handler(null), "\n";
+ restore_exception_handler();
+});
+
+throw new Exception();
+?>
+--EXPECT--
+Handler 1
+exception_handler_2
diff --git a/Zend/tests/gh13446_4.phpt b/Zend/tests/gh13446_4.phpt
new file mode 100644
index 000000000000..634d16f38a0a
--- /dev/null
+++ b/Zend/tests/gh13446_4.phpt
@@ -0,0 +1,21 @@
+--TEST--
+GH-13446: Exception handler isn't restored if stack is empty
+--FILE--
+<?php
+function exception_handler() {
+ echo "Handler\n";
+ restore_exception_handler();
+ restore_exception_handler();
+}
+set_exception_handler('exception_handler');
+
+register_shutdown_function(function () {
+ var_dump(set_exception_handler(null));
+ restore_exception_handler();
+});
+
+throw new Exception();
+?>
+--EXPECT--
+Handler
+NULL
diff --git a/Zend/zend.c b/Zend/zend.c
index f8628079f9f1..01ce30631275 100644
--- a/Zend/zend.c
+++ b/Zend/zend.c
@@ -1859,7 +1859,9 @@ ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
old_exception = EG(exception);
EG(exception) = NULL;
ZVAL_OBJ(¶ms[0], old_exception);
+
ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
+ zend_stack_push(&EG(user_exception_handlers), &orig_user_exception_handler);
ZVAL_UNDEF(&EG(user_exception_handler));
if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2,
1, params) == SUCCESS) {
@@ -1873,7 +1875,13 @@ ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
EG(exception) = old_exception;
}
- zval_ptr_dtor(&orig_user_exception_handler);
+ if (Z_TYPE(EG(user_exception_handler)) == IS_UNDEF) {
+ zval *tmp = zend_stack_top(&EG(user_exception_handlers));
+ if (tmp) {
+ ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
+ zend_stack_del_top(&EG(user_exception_handlers));
+ }
+ }
} /* }}} */
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle)