Fix infinite recursion in specifyTypesForCountFuncCall with mixed TypeSpecifierContext#5653
Merged
ondrejmirtes merged 1 commit intoMay 13, 2026
Conversation
…eSpecifierContext When specifyTypesForCountFuncCall was entered with a `false` context (0b0100), it used $context->negate() to produce the recursive call's context. But negate(false) = 0b1011, which has BOTH truthy AND falsey bits set. The guard `$context->falsey()` matched this mixed context, causing the function to re-enter its own falsey branch indefinitely. Replace $context->negate() with an explicit createTrue()/createTruthy() depending on whether the incoming context is strictly false or falsey. This guarantees the recursive call uses a clean truthy context that cannot re-enter the falsey branch. Closes phpstan/phpstan#14605
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.
Summary
Fixes an infinite recursion / OOM crash triggered when PHPStan analyzes a ternary expression that:
count($arr) > Non a constant array with optional keys as the conditioncount($m) > 1 ? false : '')The crash occurs because
AssignHandlercallsspecifyTypesInConditioninfalsecontext on the ternary, which expands it intoBooleanOr(BooleanAnd(...), ...). Processing this reachesspecifyTypesForCountFuncCallwith afalsecontext. The function's falsey-branch guard passes, and it recurses with$context->negate(). Butnegate(false)=0b1011, which has both truthy and falsey bits set. On re-entry, the$context->falsey()guard passes again → infinite recursion → OOM.The fix replaces
$context->negate()with explicitTypeSpecifierContext::createTrue()orcreateTruthy(), ensuring the recursive call always uses a clean truthy context that cannot re-enter the falsey branch.Minimal reproducer (crashes without fix):
Closes phpstan/phpstan#14605