Skip to content

Commit af66475

Browse files
committed
Improved decoding of short dictionary keys
1 parent 8685f89 commit af66475

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Decoder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ protected function decodeFastString(string $match, int $len, string $value): str
190190

191191
return $value;
192192
}
193+
if ($this->bencoded[$this->offset + 1] === ':')
194+
{
195+
$value = substr($this->bencoded, $this->offset + 2, $len - 2);
196+
$this->offset += $len;
197+
198+
return $value;
199+
}
193200

194201
return $this->decodeString();
195202
}

tests/DecoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ public static function getDecodeTests()
194194
'd8:announcei1e8:xxxxxxxx8:xxxxxxxxe',
195195
new ArrayObject(['announce' => 1, 'xxxxxxxx' => 'xxxxxxxx'])
196196
],
197+
[
198+
'd4:pathi0e40:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxi1ee',
199+
new ArrayObject(['path' => 0, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' => 1])
200+
],
197201
];
198202
}
199203

@@ -393,6 +397,10 @@ public static function getDecodeInvalidTests()
393397
'd4:pathd',
394398
new DecodingException('Premature end of data', 7)
395399
],
400+
[
401+
'd4',
402+
new DecodingException('Premature end of data', 1)
403+
],
396404
[
397405
'ld1',
398406
new DecodingException('Premature end of data', 2)

0 commit comments

Comments
 (0)