Skip to content

GH-18572: infinite stack recursion in fallback object comparison. #18577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
39 changes: 39 additions & 0 deletions Zend/tests/gh18519.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-18519: Nested object comparison leading to stack overflow
--SKIPIF--
<?php
if (getenv('SKIP_ASAN')) die('skip as it fatally crash');
?>
--FILE--
<?php

#[AllowDynamicProperties]
class Node {
public $next;
// forcing dynamic property creation is key
}

$first = new Node();
$first->previous = $first;
$first->next = $first;

$cur = $first;

for ($i = 0; $i < 50000; $i++) {
Copy link
Contributor

@mvorisek mvorisek May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test "fail" even for i=0 (no loop at all) - https://3v4l.org/gkKqd. The comparison probably cannot find cycles correctly.

Copy link
Member Author

@devnexen devnexen May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does trigger the Nesting level too deep warning indeed but it does not trigger the Maximum call stack size reached one, which is the whole point of this test. (just tried locally with master)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have simplified the code and opened #18585. I belive the recursion in weak comparasion should be handled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$new = new Node();
$new->previous = $cur;
$cur->next = $new;
$new->next = $first;
$first->previous = $new;
$cur = $new;
}

try {
// Force comparison manually to trigger zend_hash_compare
$first == $cur;
} catch(Error $e) {
echo $e->getMessage(). PHP_EOL;
}
?>
--EXPECTREGEX--
(Maximum call stack size reached during object comparison|Fatal error: Nesting level too deep - recursive dependency?.+)
14 changes: 14 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
#define IN_UNSET ZEND_GUARD_PROPERTY_UNSET
#define IN_ISSET ZEND_GUARD_PROPERTY_ISSET

static zend_always_inline bool zend_objects_check_stack_limit(void)
{
#ifdef ZEND_CHECK_STACK_LIMIT
return zend_call_stack_overflowed(EG(stack_limit));
#else
return false;
#endif
}

/*
__X accessors explanation:

Expand Down Expand Up @@ -1714,6 +1723,11 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
{
zend_object *zobj1, *zobj2;

if (zend_objects_check_stack_limit()) {
zend_throw_error(NULL, "Maximum call stack size reached during object comparison");
return ZEND_UNCOMPARABLE;
}

if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
/* Object and non-object */
zval *object;
Expand Down
Loading