Skip to content

fix: throw ParseException for unterminated list or dict - #22

Merged
Rhilip merged 1 commit into
Rhilip:masterfrom
bardrop-oss:fix-unterminated-list-dict-warning
Nov 27, 2026
Merged

Rhilip merged 1 commit into
Rhilip:masterfrom
bardrop-oss:fix-unterminated-list-dict-warning

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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 the ParseException:

Bencode::decode('li1e');
// Warning: Uninitialized string offset 4
// then ParseException: Unterminated bencode string literal

Same for l, lle, d and d1:ai1e.

If the app turns warnings into exceptions (a common error handler setup), the caller gets ErrorException: Uninitialized string offset 4 instead of ParseException, so catch (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 next decode() call then throws the same ParseException as before.

Tests

Added testDecodeListOrDictionaryUnterminated with 5 inputs: l, li1e, lle, d, d1:ai1e.

  • Before the fix: Tests: 298, Assertions: 567, Errors: 5, Skipped: 12 (the 5 new cases fail with Uninitialized string offset).
  • After the fix: Tests: 298, Assertions: 577, Skipped: 12, all pass.

Run with vendor/bin/phpunit tests/ on PHP 8.5.

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.
@Rhilip
Rhilip merged commit b06b3bf into Rhilip:master Nov 27, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants