Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix int overflow #5369

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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