Skip to content

fix: throw ParseException when ':' or 'e' is missing - #21

Merged
Rhilip merged 1 commit into
Rhilip:masterfrom
bardrop-oss:fix-missing-delimiter-loop
Nov 21, 2026
Merged

Rhilip merged 1 commit into
Rhilip:masterfrom
bardrop-oss:fix-missing-delimiter-loop

Conversation

@bardrop

@bardrop bardrop commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #20.

  • String branch: throw ParseException('Missing colon after string length') when there is no :. Before, l0a looped and appended empty strings until PHP ran out of memory.
  • Integer branch: throw ParseException('Unterminated bencode integer') when there is no e.
  • Dictionary: a key followed by e now throws Dictionary key without value: <key>. This replaces the // FIXME message in testDecodeDictionaryWithKeyButWithoutValue.

Tests: 3 new cases in DecodeTest (l0a, d3:key0, li12). Before the fix the suite dies with "Allowed memory size exhausted"; after: 293 tests OK.

I also ran 8k random and mutated inputs against a strict BEP 3 decoder: no crashes left. The only differences are the documented ones (unsorted keys are accepted and sorted, ints over PHP_INT_MAX are rejected).

decode() did not check strpos() for false. For 'l0a' the string length
ran to the end of the input, the position never moved, and the list
loop appended empty strings until PHP ran out of memory.

Also report a dictionary key without a value directly (was the FIXME
in testDecodeDictionaryWithKeyButWithoutValue).
@Rhilip
Rhilip merged commit 5ce90fb into Rhilip:master Nov 21, 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.

decode('l0a') loops until PHP runs out of memory

2 participants