Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions ext/standard/tests/general_functions/gh24743_1.phpt
Original file line number Diff line number Diff line change
@@ -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,
))
35 changes: 35 additions & 0 deletions ext/standard/tests/general_functions/gh24743_2.phpt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,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);
Expand Down
Loading