[php-src] master: Merge branch 'PHP-8.6'
| From: | Weilin Du | Date: | Sat, 14 Nov 2026 11:20:10 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143703@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-11-14T19:15:09+08:00
Commit: https://github.com/php/php-src/commit/7cadb77e25c0466fb572134c36be024531127a84
Raw diff: https://github.com/php/php-src/commit/7cadb77e25c0466fb572134c36be024531127a84.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
Fix GH-24743: var_export() ignores exception from lazy object init (#24746)
Changed paths:
A ext/standard/tests/general_functions/gh24743_1.phpt
A ext/standard/tests/general_functions/gh24743_2.phpt
M ext/standard/var.c
Diff:
diff --git a/ext/standard/tests/general_functions/gh24743_1.phpt
b/ext/standard/tests/general_functions/gh24743_1.phpt
new file mode 100644
index 000000000000..3cf841cb17f4
--- /dev/null
+++ b/ext/standard/tests/general_functions/gh24743_1.phpt
@@ -0,0 +1,79 @@
+--TEST--
+GH-24743 (var_export() does not handle exception correctly when retrieving object)
+--FILE--
+<?php
+
+class Inner {
+ public static bool $fail = true;
+
+ public int $x = 1 {
+ get {
+ if (self::$fail) {
+ throw new Exception("hook failed");
+ }
+ return $this->x;
+ }
+ }
+}
+
+class Outer {
+ public Inner $a;
+ public int $b = 2;
+
+ public function __construct() {
+ $this->a = new Inner;
+ }
+}
+
+$o = new Outer;
+
+function show_export(string $label, callable $export): void
+{
+ ob_start();
+ $caught = null;
+ try {
+ $export();
+ } catch (Throwable $e) {
+ $caught = $e;
+ }
+ $output = ob_get_clean();
+
+ echo "--- ", $label, " ---\n";
+ if ($caught === null) {
+ echo "No exception\n";
+ } else {
+ echo "Caught: ", $caught->getMessage(), "\n";
+ }
+
+ if ($output !== '') {
+ echo "BUG: var_export() emitted partial output before the exception:\n";
+ echo $output, "\n";
+ echo "--- end of partial output ---\n";
+ }
+}
+
+show_export("print mode, nested object", fn() => var_export($o));
+show_export("return mode, nested object", fn() => var_export($o, true));
+show_export("print mode, object inside array", fn() => var_export([1, $o]));
+
+echo "--- recovery: recursion guards were released ---\n";
+Inner::$fail = false;
+var_export($o);
+echo "\n";
+
+?>
+--EXPECT--
+--- print mode, nested object ---
+Caught: hook failed
+--- return mode, nested object ---
+Caught: hook failed
+--- print mode, object inside array ---
+Caught: hook failed
+--- recovery: recursion guards were released ---
+\Outer::__set_state(array(
+ 'a' =>
+ \Inner::__set_state(array(
+ 'x' => 1,
+ )),
+ 'b' => 2,
+))
diff --git a/ext/standard/tests/general_functions/gh24743_2.phpt
b/ext/standard/tests/general_functions/gh24743_2.phpt
new file mode 100644
index 000000000000..fd57fe5f114e
--- /dev/null
+++ b/ext/standard/tests/general_functions/gh24743_2.phpt
@@ -0,0 +1,35 @@
+--TEST--
+GH-24743 (var_export() does not handle exception correctly when retrieving object): lazy object
initializer
+--FILE--
+<?php
+
+class C {
+ public int $a = 1;
+}
+
+class O {
+ public C $c;
+ public int $b = 2;
+}
+
+$o = new O;
+$o->c = (new ReflectionClass(C::class))->newLazyGhost(function ($obj) {
+ throw new Exception("init failed");
+});
+
+try {
+ var_export($o);
+} catch (Exception $e) {
+ echo $e->getMessage(), "\n";
+}
+
+try {
+ var_export($o->c);
+} catch (Exception $e) {
+ echo $e->getMessage(), "\n";
+}
+
+?>
+--EXPECT--
+init failed
+init failed
diff --git a/ext/standard/var.c b/ext/standard/var.c
index 525ad100cce2..48108f12e04c 100644
--- a/ext/standard/var.c
+++ b/ext/standard/var.c
@@ -637,6 +637,11 @@ PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /*
}
ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, EXPORT, zobj);
myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
+ if (UNEXPECTED(EG(exception))) {
+ ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj);
+ zend_release_properties(myht);
+ return FAILURE;
+ }
if (level > 1) {
smart_str_appendc(buf, '\n');
buffer_append_spaces(buf, level - 1);