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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
GH-24605: var_export() checks native stack limit for arrays and objects
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting');
}
?>
--INI--
zend.max_allowed_stack_size=256K
--FILE--
<?php
function test_export(string $name, mixed $value): void {
try {
var_export($value, true);
echo "$name: NOT GUARDED\n";
} catch (Error $e) {
echo $name, ': ', str_contains($e->getMessage(), 'Maximum call stack size')
? "guarded\n"
: "unexpected error\n";
}
}

$data = 0;
for ($i = 0; $i < 3000; $i++) {
$data = [$data];
}
test_export('array', $data);

// Detach the nested arrays without recursively destroying them.
while (is_array($data)) {
$data = $data[0];
}

class StackNode { public ?StackNode $next = null; }
$first = new StackNode();
$node = $first;
for ($i = 0; $i < 3000; $i++) {
$node->next = new StackNode();
$node = $node->next;
}
test_export('object', $first);

// Break the chain iteratively to avoid teardown recursion.
$node = $first;
while ($node !== null) {
$next = $node->next;
$node->next = null;
$node = $next;
}
?>
--EXPECT--
array: guarded
object: guarded
14 changes: 13 additions & 1 deletion ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /*
zend_string *key;
zval *val;

#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_call_stack_size_error();
return FAILURE;
}
#endif

again:
switch (Z_TYPE_P(struc)) {
case IS_FALSE:
Expand Down Expand Up @@ -672,10 +679,15 @@ PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /*
return FAILURE;
}
}
php_object_element_export(val, index, key, level, buf);
zend_result result = php_object_element_export(val, index, key, level, buf);
if (val == &tmp) {
zval_ptr_dtor(val);
}
if (UNEXPECTED(result == FAILURE)) {
ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj);
zend_release_properties(myht);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
}
zend_release_properties(myht);
Expand Down
Loading