Skip to content

Commit cddef00

Browse files
authored
fix int overflow (#5369)
1 parent 3817193 commit cddef00

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonDivArithmeticOpAnalyzer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use function preg_match;
3838
use function strtolower;
3939
use function is_int;
40+
use const PHP_INT_MAX;
4041

4142
/**
4243
* @internal
@@ -312,7 +313,12 @@ private static function analyzeNonDivOperands(
312313
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
313314
$calculated_type = Type::getInt(false, $left_type_part->value % $right_type_part->value);
314315
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
315-
$calculated_type = Type::getInt(false, $left_type_part->value * $right_type_part->value);
316+
$result = $left_type_part->value * $right_type_part->value;
317+
if ($result <= PHP_INT_MAX) {
318+
$calculated_type = Type::getInt(false, $result);
319+
} else {
320+
$calculated_type = Type::getFloat($result);
321+
}
316322
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
317323
$calculated_type = Type::getInt(false, $left_type_part->value ** $right_type_part->value);
318324
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {

tests/BinaryOperationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ function foo($a, $b): void {
388388
echo "Actually, zero\n";
389389
}
390390
}'
391+
],
392+
'IntOverflow' => [
393+
'<?php
394+
$a = (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);',
395+
'assertions' => [
396+
'$a' => 'float'
397+
],
391398
]
392399
];
393400
}

0 commit comments

Comments
 (0)