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

Fix integer overflow in array keys #9499

Merged
merged 6 commits into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -333,7 +333,9 @@ private static function analyzeArrayItem(
} elseif ($key_type->isSingleIntLiteral()) {
$item_key_value = $key_type->getSingleIntLiteral()->value;

if ($item_key_value >= $array_creation_info->int_offset) {
if ($item_key_value < PHP_INT_MAX
&& $item_key_value >= $array_creation_info->int_offset
) {
if ($item_key_value === $array_creation_info->int_offset) {
$item_is_list_item = true;
}
Expand Down
Expand Up @@ -666,7 +666,9 @@ private static function handleArrayItem(
} elseif ($key_type->isSingleIntLiteral()) {
$item_key_value = $key_type->getSingleIntLiteral()->value;

if ($item_key_value >= $array_creation_info->int_offset) {
if ($item_key_value < PHP_INT_MAX
&& $item_key_value >= $array_creation_info->int_offset
) {
if ($item_key_value === $array_creation_info->int_offset) {
$item_is_list_item = true;
}
Expand Down
18 changes: 16 additions & 2 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Expand Up @@ -663,8 +663,22 @@ public function getList(): TList
*/
private function escapeAndQuote($name)
{
if (is_string($name) && ($name === '' || preg_match('/[^a-zA-Z0-9_]/', $name))) {
$name = '\'' . str_replace("\n", '\n', addslashes($name)) . '\'';
if (is_string($name)) {
$quote = false;

if ($name === '' || preg_match('/[^a-zA-Z0-9_]/', $name)) {
$quote = true;
}

if (preg_match('/^[1-9][0-9]*$/', $name)
&& (string)(int) $name !== $name // overflow occured
) {
$quote = true;
}
weirdan marked this conversation as resolved.
Show resolved Hide resolved

if ($quote) {
$name = '\'' . str_replace("\n", '\n', addslashes($name)) . '\'';
}
}

return $name;
Expand Down
31 changes: 31 additions & 0 deletions tests/ConstantTest.php
Expand Up @@ -1539,6 +1539,27 @@ class B
$z = B::ARRAY['b'];
PHP,
],
'maxIntegerInArrayKey' => [
'code' => <<<'PHP'
<?php
class A {
// PHP_INT_MAX
public const S = ['9223372036854775807' => 1];
public const I = [9223372036854775807 => 1];

// PHP_INT_MAX + 1
public const SO = ['9223372036854775808' => 1];
}
$s = A::S;
$i = A::I;
$so = A::SO;
PHP,
'assertions' => [
'$s===' => 'array{9223372036854775807: 1}',
'$i===' => 'array{9223372036854775807: 1}',
'$so===' => "array{'9223372036854775808': 1}",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is an example of a numeric that PHP keeps as a string.

],
],
];
}

Expand Down Expand Up @@ -2060,6 +2081,16 @@ class Foo {
',
'error_message' => 'InvalidStringClass',
],
'integerOverflowInArrayKey' => [
'code' => <<<'PHP'
<?php
class A {
// PHP_INT_MAX + 1
public const IO = [9223372036854775808 => 1];
}
PHP,
'error_message' => 'InvalidArrayOffset',
],
];
}
}
66 changes: 66 additions & 0 deletions tests/ExpressionTest.php
@@ -0,0 +1,66 @@
<?php

namespace Psalm\Tests;

use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

class ExpressionTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;

/**
* @return iterable<
* string,
* array{
* code: string,
* assertions?: array<string, string>,
* ignored_issues?: list<string>,
* php_version?: string,
* }
* >
*/
public function providerValidCodeParse(): iterable
{
yield 'maxIntegerInArrayKey' => [
'code' => <<<'PHP'
<?php
// PHP_INT_MAX
$s = ['9223372036854775807' => 1];
$i = [9223372036854775807 => 1];

// PHP_INT_MAX + 1
$so = ['9223372036854775808' => 1];
PHP,
'assertions' => [
'$s===' => 'array{9223372036854775807: 1}',
'$i===' => 'array{9223372036854775807: 1}',
'$so===' => "array{'9223372036854775808': 1}",
],
];
}

/**
* @return iterable<
* string,
* array{
* code: string,
* error_message: string,
* ignored_issues?: list<string>,
* php_version?: string,
* }
* >
*/
public function providerInvalidCodeParse(): iterable
{
yield 'integerOverflowInArrayKey' => [
'code' => <<<'PHP'
<?php
// PHP_INT_MAX + 1
[9223372036854775808 => 1];
PHP,
'error_message' => 'InvalidArrayOffset',
];
}
}