php-src icon indicating copy to clipboard operation
php-src copied to clipboard

var_export does not handle exception correctly when retrieving object

Open Sjord opened this issue 1 month ago • 0 comments

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

PHP 8.4 branch and PHP master branch

Operating System

No response

Sjord avatar Oct 26 '26 14:10 Sjord