Skip to content

Commit e709b38

Browse files
committed
Inlined integer overflow check
1 parent fde73f2 commit e709b38

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/Decoder.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ protected function __construct(string $bencoded)
6868
*/
6969
protected function castInteger(string $string, bool $negative): int
7070
{
71+
$clamp = PHP_INT_MAX;
7172
if ($negative)
7273
{
7374
$string = "-$string";
7475
$clamp = PHP_INT_MIN;
7576
}
76-
else
77-
{
78-
$clamp = PHP_INT_MAX;
79-
}
8077

8178
$value = (int) $string;
82-
if ($value === $clamp)
79+
if ($value === $clamp && is_float(+$string))
8380
{
84-
$this->checkIntegerOverflow($string);
81+
throw new DecodingException('Integer overflow', $this->offset - 1 - strlen($string));
8582
}
8683

8784
return $value;
@@ -116,14 +113,6 @@ protected function checkCursorPosition(): void
116113
}
117114
}
118115

119-
protected function checkIntegerOverflow(string $str): void
120-
{
121-
if (is_float(+$str))
122-
{
123-
throw new DecodingException('Integer overflow', $this->offset - 1 - strlen($str));
124-
}
125-
}
126-
127116
protected function complianceError(string $message, int $offset): void
128117
{
129118
throw new ComplianceError($message, $offset);

0 commit comments

Comments
 (0)