[php-src] Issue #24743: var_export does not handle exception correctly when retrieving object
| From: | Sjord | Date: | Mon, 26 Oct 2026 14:23:05 +0000 |
| Subject: | [php-src] Issue #24743: var_export does not handle exception correctly when retrieving object | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-253214@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/24743
Author: Sjord
### Description
In ext/standard/var.c the return value of php_object_element_export is not checked. If retrieving an
object property throws an exception,
var_export outputs a partial export. I would
expect no output and only an exception.
```
--TEST--
var_export(): exception from a nested object's property hook must abort the export
--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,
))
```
### PHP Version
```plain
PHP 8.4 branch and PHP master branch
```
### Operating System
_No response_