Skip to content

Fix stack overflow in mb_check_encoding() and mb_convert_encoding() - #25219

Closed
eyupcanakman wants to merge 1 commit into
php:PHP-8.4from
eyupcanakman:mbstring-recursive-stack-limit
Closed

eyupcanakman wants to merge 1 commit into
php:PHP-8.4from
eyupcanakman:mbstring-recursive-stack-limit

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

mb_check_encoding() and mb_convert_encoding() walk array arguments with php_mb_check_encoding_recursive() and php_mb_convert_encoding_recursive(). Neither helper looks at the stack limit before going one level deeper, so a deep enough array crashes the process. mb_convert_encoding() already segfaults at 30000 levels with the default stack.

mbstring.c has had mb_check_stack_limit() since GH-20836, but only mb_convert_variables() uses it. Both helpers now call it and return early once the Error is thrown, so the walk stops there and a second nested array never adds a chained Error. The helper moves up the file so the convert path can see it.

There is no GitHub issue for this.

The array walk in both functions goes one C call deeper per nesting level without checking the stack limit, so a deeply nested array crashes the process.
Call mb_check_stack_limit() in the two recursive helpers, as mb_convert_variables() already does.
@alexdowad

Copy link
Copy Markdown
Contributor

@eyupcanakman Thanks very much for the report! Will look into it ASAP.

@alexdowad

Copy link
Copy Markdown
Contributor

OK, the bug is real, the fix is good, the regression test catches it... this looks very nice!

@alexdowad

Copy link
Copy Markdown
Contributor

Merged into PHP 8.4 → 8.5 → 8.6 → master.
Thank you for your good contribution!

@alexdowad alexdowad closed this Nov 6, 2026
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