Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crashing for negative bit shifts #1447

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Expand Up @@ -245,12 +245,12 @@ parameters:
path: src/Reflection/InitializerExprTypeResolver.php

-
message: "#^Binary operation \"\\<\\<\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\|string\\|null results in an error\\.$#"
message: "#^Binary operation \"\\<\\<\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\<0, max\\>\\|string\\|null results in an error\\.$#"
count: 1
path: src/Reflection/InitializerExprTypeResolver.php

-
message: "#^Binary operation \"\\>\\>\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\|string\\|null results in an error\\.$#"
message: "#^Binary operation \"\\>\\>\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\<0, max\\>\\|string\\|null results in an error\\.$#"
count: 1
path: src/Reflection/InitializerExprTypeResolver.php

Expand Down
8 changes: 8 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -1097,6 +1097,10 @@ public function getShiftLeftType(Expr $left, Expr $right, callable $getTypeCallb
throw new ShouldNotHappenException();
}

if ($rightNumberType->getValue() < 0) {
return new ErrorType();
}

$resultType = $this->getTypeFromValue($leftNumberType->getValue() << $rightNumberType->getValue());
if ($generalize) {
$resultType = $resultType->generalize(GeneralizePrecision::lessSpecific());
Expand Down Expand Up @@ -1146,6 +1150,10 @@ public function getShiftRightType(Expr $left, Expr $right, callable $getTypeCall
throw new ShouldNotHappenException();
}

if ($rightNumberType->getValue() < 0) {
return new ErrorType();
}

$resultType = $this->getTypeFromValue($leftNumberType->getValue() >> $rightNumberType->getValue());
if ($generalize) {
$resultType = $resultType->generalize(GeneralizePrecision::lessSpecific());
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -916,6 +916,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7153.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/in-array-non-empty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4117.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7490.php');
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7490.php
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Bug7490;

use function PHPStan\Testing\assertType;

function () {
assertType('*ERROR*', 1 << -1);
assertType('*ERROR*', 1 >> -1);
};
Comment on lines +7 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used a simpler test case, because the test case in the issue took around a minute to process. This tests exactly the same thing.