Skip to content

Fix GH-23111: stack overflow in array_walk_recursive() with deep arrays - #23125

Merged
arnaud-lb merged 1 commit into
php:PHP-8.4from
lazerg:fix/gh-23111-array-walk-stack-limit
Aug 10, 2026
Merged

Fix GH-23111: stack overflow in array_walk_recursive() with deep arrays#23125
arnaud-lb merged 1 commit into
php:PHP-8.4from
lazerg:fix/gh-23111-array-walk-stack-limit

Conversation

@lazerg

@lazerg lazerg commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

php_array_walk() recurses once per nesting level with no stack check, so array_walk_recursive() on a deeply nested array exhausts the native stack and the process dies with a segfault.

This adds the same stack limit check ext/standard already uses in var.c and http.c, so the call throws an Error instead of crashing. The existing GC_IS_RECURSIVE guard only covers self-referential arrays, not plain deep nesting.

count($array, COUNT_RECURSIVE) in the same file crashes on the same input for the same reason. I kept this PR to the reported function, but can cover that here too if you prefer one change.

Fixes #23111.

@lazerg
lazerg requested a review from bukka as a code owner August 8, 2026 04:31
@arnaud-lb
arnaud-lb merged commit 69d5f1b into php:PHP-8.4 Aug 10, 2026
18 checks passed
arnaud-lb added a commit that referenced this pull request Aug 10, 2026
* PHP-8.5:
  Add a stack limit check in php_array_replace_recursive (#23124)
  Add a stack limit check in php_array_walk() (#23125)
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

count($array, COUNT_RECURSIVE) in the same file crashes on the same input for the same reason. I kept this PR to the reported function, but can cover that here too if you prefer one change.

Well spotted! Please submit a separate PR and I will review it.

pull Bot pushed a commit to dolfly/php-src that referenced this pull request Aug 10, 2026
* PHP-8.4:
  Add a stack limit check in php_array_replace_recursive (php#23124)
  Add a stack limit check in php_array_walk() (php#23125)
@lazerg

lazerg commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Opened it as #23197.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants