Skip to content

[ExpressionLanguage] Fix nesting level of chained ternaries - #66720

Merged
nicolas-grekas merged 1 commit into
symfony:5.4from
lazerg:fix/issue-66719-ternary-nesting-level
Oct 9, 2026
Merged

nicolas-grekas merged 1 commit into
symfony:5.4from
lazerg:fix/issue-66719-ternary-nesting-level

Conversation

@lazerg

@lazerg lazerg commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #66719
License MIT

Since #65690, a chain of ternaries can hit the nesting limit even when the tree is shallow. str_repeat('a and a and a ? 1 : ', 64).'0' is rejected, but the same expression with brackets around each condition is accepted.

parseExpression() adds one level for each binary operator of the condition. It kept these levels when it parsed the then and else branches. In a chain, the operator counts of all conditions add up. The branches are siblings of the condition, not children. So the level is now reset before parseConditionalExpression() runs, the same way brackets already do it.

Deep chains are still rejected: the existing ternaries case passes, and 300 chained ? :, ?: or ?? still throw. I added a test with the expression from the issue. It fails without the fix. ./phpunit src/Symfony/Component/ExpressionLanguage passes (385 tests).

@lazerg

lazerg commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

The Fabbot typo check fails on "does't" in an existing ParserTest comment that this PR does not change. The Unit Tests (8.3) failure is in HttpClient (testNormalizeBodyMultipartForwardStream), which this PR does not touch, and the same job passes on 6.4.

@nicolas-grekas

Copy link
Copy Markdown
Member

Thank you @lazerg.

@nicolas-grekas
nicolas-grekas merged commit 0bdf8ee into symfony:5.4 Oct 9, 2026
10 of 11 checks passed
nicolas-grekas added a commit that referenced this pull request Oct 9, 2026
…ee (nicolas-grekas)

This PR was merged into the 5.4 branch.

Discussion
----------

[ExpressionLanguage] Bound the depth of the parsed node tree

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Follows #65690: the nesting level misses the levels that a chain of binary operators or of property accesses adds above its first operand. With `p` nested parentheses holding `k` additions each, as in `((1 + 1 + 1) + 1 + 1)`, an expression costs about `p + k` levels but builds a tree `p × k` levels deep. With `p = k = 60`, the expression is accepted and `serialize()`, which the default cache of `ExpressionLanguage` runs on parsed expressions, segfaults on PHP 8.1 and 8.2 (8.3+ throws "Maximum call stack size reached").

This also checks the depth of the node tree once parsed. A tree is at most one level deeper than its number of tokens, so the walk is skipped for expressions shorter than 256 characters.

Merge-up to 6.4: keep `unset($this->stream, $this->names);` after the new check and use `\sprintf()` in `checkDepth()` (trial-merged and tested up to 8.2, together with #66720).

Commits
-------

944be84 [ExpressionLanguage] Bound the depth of the parsed node tree
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.

3 participants