Repository navigation
Conversation
ast_to_zval(), ast_fill_children_ht(), and zend_ast_destroy() recursed without bound on deeply nested ASTs (such as left-recursive expression chains that bypass Bison's YYMAXDEPTH or right-deep chains), overflowing the C call stack and crashing with SIGSEGV even on PHP 8.3+ with zend.max_allowed_stack_size configured. Check zend_call_stack_overflowed(EG(stack_limit)) when ZEND_CHECK_STACK_LIMIT is available (PHP 8.3+), with a fallback AST_MAX_RECURSION_DEPTH of 256 levels on older PHP versions or platforms without ZEND_CHECK_STACK_LIMIT, throwing Error and unwinding partially constructed nodes cleanly on failure. Flatten nested ZEND_AST_STMT_LIST nodes iteratively in ast_fill_stmt_list_ht() and replace recursive zend_ast_destroy() calls in parse_file() and parse_code() with iterative ast_destroy_ast().
Owner
|
Not interested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before
ast_to_zval()andast_fill_children_ht()recursed without bound on the C call stack when converting deeply nestedzend_asttrees, such as left-recursive expression chains ($a+1+1+...,$a->f()->f()...) that bypass Bison'sYYMAXDEPTH, or right-deep chains.PHP_FUNCTION(parse_file)andPHP_FUNCTION(parse_code)also called recursivezend_ast_destroy(), which can overflow the stack during teardown on PHP 7.2 or on right-deep trees on PHP 7.3+.After
ast_to_zval()withast_check_stack_limit(), usingzend_call_stack_overflowed(EG(stack_limit))whenZEND_CHECK_STACK_LIMITis defined (PHP 8.3+) and a fallbackAST_MAX_RECURSION_DEPTHlimit (256U) on older PHP versions or platforms withoutZEND_CHECK_STACK_LIMIT.int(SUCCESS/FAILURE) throughast_to_zval(),ast_fill_children_ht(),ast_create_virtual_node(), andast_name_to_zval(), throwingErrorand freeing partially constructed nodes cleanly on unwind.ZEND_AST_STMT_LISTflattening inast_fill_children_ht()with iterativeast_fill_stmt_list_ht().zend_ast_destroy(ast)inparse_file()andparse_code()with iterativeast_destroy_ast(), delegating leaf and child-cleared declaration payload cleanup tozend_ast_destroy().Scope rationale
ast_to_zval()into a heap state machine.package.xml, matching repository convention for PRs.Test plan
tests/deep_recursion.phptcovering left-deep binary expressions, method-call chains, right-deep??chains,parse_file(), deeply nested{...}statement lists, and post-error recovery (verified ASan stack-overflow failure onmaster)-Wall -Wextra -Wno-unused-parameter -Werrorand passed full test suite on PHP 7.4 debug, 8.4 ASan NTS/ZTS, 8.5 debug/release, 8.5 fallback (-UZEND_CHECK_STACK_LIMIT), and 8.6 debugrun-tests.php -m) ontests/deep_recursion.phpt,tests/stmt_list.phpt,tests/php81_first_class_callable_support.phpt, andtests/class.phpt: 4/4 passed, 0 leakspecl packagesucceeds