Skip to content

Commit

Permalink
Don't crash on unary minus overflow
Browse files Browse the repository at this point in the history
Fixes #9464
  • Loading branch information
weirdan committed Mar 7, 2023
1 parent c82191b commit 193a0db
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 @@ -20,6 +20,8 @@
use Psalm\Type\Union;
use RuntimeException;

use function is_int;

/**
* @internal
*/
Expand Down Expand Up @@ -51,7 +53,9 @@ public static function analyze(
continue;
}
if ($type_part instanceof TLiteralInt) {
$type_part = new TLiteralInt(-$type_part->value);
/** @var int|float $value */
$value = -$type_part->value;
$type_part = is_int($value) ? new TLiteralInt($value) : new TLiteralFloat($value);
} elseif ($type_part instanceof TLiteralFloat) {
$type_part = new TLiteralFloat(-$type_part->value);
} elseif ($type_part instanceof TIntRange) {
Expand Down
9 changes: 9 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,15 @@ function toPositiveInt(int $i): int
'$b===' => 'non-falsy-string',
],
],
'unaryMinusOverflows' => [
'code' => <<<'PHP'
<?php
$a = -(1 << 63);
PHP,
'assertions' => [
'$a===' => 'float(9.2233720368548E+18)',
],
],
];
}

Expand Down

0 comments on commit 193a0db

Please sign in to comment.