1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

fix int overflow (#5369)

This commit is contained in:
orklah 2021-03-12 19:24:00 +01:00 committed by GitHub
parent 38171934cf
commit cddef00692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -37,6 +37,7 @@ use function array_values;
use function preg_match;
use function strtolower;
use function is_int;
use const PHP_INT_MAX;
/**
* @internal
@ -312,7 +313,12 @@ class NonDivArithmeticOpAnalyzer
} 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) {

View File

@ -388,6 +388,13 @@ class BinaryOperationTest extends TestCase
echo "Actually, zero\n";
}
}'
],
'IntOverflow' => [
'<?php
$a = (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);',
'assertions' => [
'$a' => 'float'
],
]
];
}