Skip to content

Commit c5d0902

Browse files
committed
Fix int overflow internal error
1 parent ee93446 commit c5d0902

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/Analyser/MutatingScope.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
use function get_class;
125125
use function in_array;
126126
use function is_float;
127+
use function is_int;
127128
use function is_string;
128129
use function ltrim;
129130
use function max;
@@ -1060,7 +1061,12 @@ private function resolveType(Expr $node): Type
10601061
$newTypes = [];
10611062
foreach ($scalarValues as $scalarValue) {
10621063
if ($scalarValue instanceof ConstantIntegerType) {
1063-
$newTypes[] = new ConstantIntegerType(-$scalarValue->getValue());
1064+
/** @var int|float $newValue */
1065+
$newValue = -$scalarValue->getValue();
1066+
if (!is_int($newValue)) {
1067+
return $type;
1068+
}
1069+
$newTypes[] = new ConstantIntegerType($newValue);
10641070
} elseif ($scalarValue instanceof ConstantFloatType) {
10651071
$newTypes[] = new ConstantFloatType(-$scalarValue->getValue());
10661072
}

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ public function testBug4308(): void
641641
$this->assertNoErrors($errors);
642642
}
643643

644+
public function testBug6979(): void
645+
{
646+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-6979.php');
647+
$this->assertNoErrors($errors);
648+
}
649+
644650
/**
645651
* @param string[]|null $allAnalysedFiles
646652
* @return Error[]
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug6979;
4+
5+
foreach ([PHP_INT_MIN, 0, PHP_INT_MAX] as $v) {
6+
if ($v !== PHP_INT_MIN) {
7+
$invalidNumbers[] = -$v;
8+
}
9+
}

0 commit comments

Comments
 (0)