Skip to content

Commit 193a0db

Browse files
committed
Don't crash on unary minus overflow
Fixes #9464
1 parent c82191b commit 193a0db

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Psalm\Type\Union;
2121
use RuntimeException;
2222

23+
use function is_int;
24+
2325
/**
2426
* @internal
2527
*/
@@ -51,7 +53,9 @@ public static function analyze(
5153
continue;
5254
}
5355
if ($type_part instanceof TLiteralInt) {
54-
$type_part = new TLiteralInt(-$type_part->value);
56+
/** @var int|float $value */
57+
$value = -$type_part->value;
58+
$type_part = is_int($value) ? new TLiteralInt($value) : new TLiteralFloat($value);
5559
} elseif ($type_part instanceof TLiteralFloat) {
5660
$type_part = new TLiteralFloat(-$type_part->value);
5761
} elseif ($type_part instanceof TIntRange) {

tests/BinaryOperationTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,15 @@ function toPositiveInt(int $i): int
10131013
'$b===' => 'non-falsy-string',
10141014
],
10151015
],
1016+
'unaryMinusOverflows' => [
1017+
'code' => <<<'PHP'
1018+
<?php
1019+
$a = -(1 << 63);
1020+
PHP,
1021+
'assertions' => [
1022+
'$a===' => 'float(9.2233720368548E+18)',
1023+
],
1024+
],
10161025
];
10171026
}
10181027

0 commit comments

Comments
 (0)