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

Non-numeric strings in pow() lead to error #2796

Merged
merged 6 commits into from
Jul 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Type/ExponentiateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use function is_float;
use function is_int;
use function is_numeric;
use function is_string;
use function pow;

final class ExponentiateHelper
{
Expand Down Expand Up @@ -83,10 +86,16 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
$min = null;
$max = null;
if ($exponent->getMin() !== null) {
$min = $base->getValue() ** $exponent->getMin();
$min = self::pow($base->getValue(), $exponent->getMin());
if ($min === null) {
return new ErrorType();
}
}
if ($exponent->getMax() !== null) {
$max = $base->getValue() ** $exponent->getMax();
$max = self::pow($base->getValue(), $exponent->getMax());
if ($max === null) {
return new ErrorType();
}
}

if (!is_float($min) && !is_float($max)) {
Expand All @@ -95,7 +104,11 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
}

if ($exponent instanceof ConstantScalarType) {
$result = $base->getValue() ** $exponent->getValue();
$result = self::pow($base->getValue(), $exponent->getValue());
if ($result === null) {
return new ErrorType();
}

if (is_int($result)) {
return new ConstantIntegerType($result);
}
Expand All @@ -105,4 +118,15 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
return null;
}

private static function pow(mixed $base, mixed $exp): float|int|null
{
if (is_string($base) && !is_numeric($base)) {
return null;
}
if (is_string($exp) && !is_numeric($exp)) {
return null;
}
return pow($base, $exp);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pow.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,23 @@ function doFoo(int $intA, int $intB, string $s, bool $bool, $numericS, float $fl
assertType('*ERROR*', $bool ** $arr);
assertType('*ERROR*', $bool ** []);
};

function invalidConstantOperands(): void {
assertType('*ERROR*', 'a' ** 1);
assertType('*ERROR*', 1 ** 'a');

assertType('*ERROR*', [] ** 1);
assertType('*ERROR*', 1 ** []);

assertType('*ERROR*', (new \stdClass()) ** 1);
assertType('*ERROR*', 1 ** (new \stdClass()));
}

function validConstantOperands(): void {
assertType('1', '1' ** 1);
assertType('1', 1 ** '1');
assertType('1', '1' ** '1');

assertType('1', true ** 1);
assertType('1', 1 ** false);
}
Loading