Repository navigation
fix: throw ParseException for unterminated list or dict - #22
Merged
Rhilip merged 1 commit intoNov 27, 2026
Merged
Conversation
decode() read $data[$pos] past the end of the input when a list or dictionary had no closing 'e' (for example 'l', 'li1e' or 'd1:ai1e'). This raised an 'Uninitialized string offset' warning before the ParseException. With a strict error handler the caller got an ErrorException instead of ParseException.
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.
What was wrong
When a list or dict has no closing
e,decode()reads$data[$pos]past the end of the input. PHP raises a warning before theParseException:Same for
l,lle,dandd1:ai1e.If the app turns warnings into exceptions (a common error handler setup), the caller gets
ErrorException: Uninitialized string offset 4instead ofParseException, socatch (ParseException $e)does not catch it.Right output: only
ParseException: Unterminated bencode string literal, no warning.The fix
In the list and dict loops, use
($data[$pos] ?? '')so an out-of-range read gives''with no warning. The nextdecode()call then throws the sameParseExceptionas before.Tests
Added
testDecodeListOrDictionaryUnterminatedwith 5 inputs:l,li1e,lle,d,d1:ai1e.Tests: 298, Assertions: 567, Errors: 5, Skipped: 12(the 5 new cases fail withUninitialized string offset).Tests: 298, Assertions: 577, Skipped: 12, all pass.Run with
vendor/bin/phpunit tests/on PHP 8.5.