Skip to content

Commit

Permalink
fix int overflow (#5369)
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah authored Mar 12, 2021
1 parent 3817193 commit cddef00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use function preg_match;
use function strtolower;
use function is_int;
use const PHP_INT_MAX;

/**
* @internal
Expand Down Expand Up @@ -312,7 +313,12 @@ private static function analyzeNonDivOperands(
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$calculated_type = Type::getInt(false, $left_type_part->value % $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
$calculated_type = Type::getInt(false, $left_type_part->value * $right_type_part->value);
$result = $left_type_part->value * $right_type_part->value;
if ($result <= PHP_INT_MAX) {
$calculated_type = Type::getInt(false, $result);
} else {
$calculated_type = Type::getFloat($result);
}
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
$calculated_type = Type::getInt(false, $left_type_part->value ** $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
Expand Down
7 changes: 7 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ function foo($a, $b): void {
echo "Actually, zero\n";
}
}'
],
'IntOverflow' => [
'<?php
$a = (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);',
'assertions' => [
'$a' => 'float'
],
]
];
}
Expand Down

0 comments on commit cddef00

Please sign in to comment.