Generic/InlineControlStructure: fix unchecked findNext() return value#1387
Merged
Merged
Conversation
On line 67, `findNext()` searches for the first non-empty token after a `T_ELSE` to detect the `else if` pattern. When `else` is the last non-empty token in the file (live coding/parse error), `findNext()` returns `false`. This `false` value was used directly as an array index on line 68 (`$tokens[$next]`), which silently becomes `$tokens[0]`, reading the `T_OPEN_TAG` token instead. The bug was silent because the code would eventually bail out at line 81-83. This commit adds a `$next === false` check, which also allows the sniff to bail out earlier for an `else` at end of file instead of continuing to do unnecessary work before reaching the existing live coding bail-out further down. Existing test file InlineControlStructureUnitTest.12.inc already covers this scenario.
jrfnl
approved these changes
Mar 6, 2026
Member
jrfnl
left a comment
There was a problem hiding this comment.
Thanks @rodrigoprimo ! Makes perfect sense.
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.
Description
On line 67,
findNext()searches for the first non-empty token after aT_ELSEto detect theelse ifpattern. Whenelseis the last non-empty token in the file (live coding/parse error),findNext()returnsfalse. Thisfalsevalue was used directly as an array index on line 68 ($tokens[$next]), which silently becomes$tokens[0], reading theT_OPEN_TAGtoken instead. The bug was silent because the code would eventually bail out at line 81-83.This commit adds a
$next === falsecheck, which also allows the sniff to bail out earlier for anelseat end of file instead of continuing to do unnecessary work before reaching the existing live coding bail-out further down.Existing test file InlineControlStructureUnitTest.12.inc already covers this scenario.
Suggested changelog entry
N/A
Types of changes
PR checklist