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 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
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,32 @@ 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 === $array_creation_info->int_offset) {
if ($item_key_value <= PHP_INT_MAX
weirdan marked this conversation as resolved.
Show resolved Hide resolved
&& $item_key_value > $array_creation_info->int_offset
) {
if ($item_key_value - 1 === $array_creation_info->int_offset) {
$item_is_list_item = true;
}
$array_creation_info->int_offset = $item_key_value + 1;
$array_creation_info->int_offset = $item_key_value;
}
}
} else {
$key_type = Type::getArrayKey();
}
} else {
if ($array_creation_info->int_offset === PHP_INT_MAX) {
IssueBuffer::maybeAdd(
new InvalidArrayOffset(
'Cannot add an item with an offset beyond PHP_INT_MAX',
new CodeLocation($statements_analyzer->getSource(), $item),
),
);
return;
}

$item_is_list_item = true;
$item_key_value = $array_creation_info->int_offset++;
$item_key_value = ++$array_creation_info->int_offset;

$key_atomic_type = new TLiteralInt($item_key_value);
$array_creation_info->item_key_atomic_types[] = $key_atomic_type;
$key_type = new Union([$key_atomic_type]);
Expand Down Expand Up @@ -538,7 +551,17 @@ private static function handleUnpackedArray(
$array_creation_info->item_key_atomic_types[] = Type::getAtomicStringFromLiteral($new_offset);
$array_creation_info->all_list = false;
} else {
$new_offset = $array_creation_info->int_offset++;
if ($array_creation_info->int_offset === PHP_INT_MAX) {
IssueBuffer::maybeAdd(
new InvalidArrayOffset(
'Cannot add an item with an offset beyond PHP_INT_MAX',
new CodeLocation($statements_analyzer->getSource(), $item->value),
),
$statements_analyzer->getSuppressedIssues(),
);
continue 2;
}
$new_offset = ++$array_creation_info->int_offset;
$array_creation_info->item_key_atomic_types[] = new TLiteralInt($new_offset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ class ArrayCreationInfo
*/
public array $array_keys = [];

public int $int_offset = 0;
/**
* Holds the integer offset of the *last* element added
*
* -1 may mean no elements have been added yet, but can also mean there's an element with offset -1
*/
public int $int_offset = -1;

public bool $all_list = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,17 +666,22 @@ 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 === $array_creation_info->int_offset) {
if ($item_key_value <= PHP_INT_MAX
&& $item_key_value > $array_creation_info->int_offset
) {
if ($item_key_value - 1 === $array_creation_info->int_offset) {
$item_is_list_item = true;
}
$array_creation_info->int_offset = $item_key_value + 1;
$array_creation_info->int_offset = $item_key_value;
}
}
}
} else {
if ($array_creation_info->int_offset === PHP_INT_MAX) {
return false;
}
$item_is_list_item = true;
$item_key_value = $array_creation_info->int_offset++;
$item_key_value = ++$array_creation_info->int_offset;
$array_creation_info->item_key_atomic_types[] = new TLiteralInt($item_key_value);
}

Expand Down Expand Up @@ -760,7 +765,10 @@ private static function handleUnpackedArray(
$new_offset = $key;
$array_creation_info->item_key_atomic_types[] = Type::getAtomicStringFromLiteral($new_offset);
} else {
$new_offset = $array_creation_info->int_offset++;
if ($array_creation_info->int_offset === PHP_INT_MAX) {
return false;
}
$new_offset = ++$array_creation_info->int_offset;
$array_creation_info->item_key_atomic_types[] = new TLiteralInt($new_offset);
}

Expand Down
18 changes: 16 additions & 2 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
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;
}

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

return $name;
Expand Down
70 changes: 70 additions & 0 deletions tests/ConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,42 @@ 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.

],
],
'autoincrementAlmostOverflow' => [
'code' => <<<'PHP'
<?php
class A {
public const I = [
9223372036854775806 => 0,
1, // expected key = PHP_INT_MAX
];
}
$s = A::I;
PHP,
'assertions' => [
'$s===' => 'array{9223372036854775806: 0, 9223372036854775807: 1}',
],
],
];
}

Expand Down Expand Up @@ -2060,6 +2096,40 @@ class Foo {
',
'error_message' => 'InvalidStringClass',
],
'integerOverflowInArrayKey' => [
'code' => <<<'PHP'
<?php
class A {
// PHP_INT_MAX + 1
public const IO = [9223372036854775808 => 1];
}
PHP,
'error_message' => 'InvalidArrayOffset',
],
'autoincrementOverflow' => [
'code' => <<<'PHP'
<?php
class A {
public const I = [
9223372036854775807 => 0,
1, // this is a fatal error
];
}
PHP,
'error_message' => 'InvalidArrayOffset',
],
'autoincrementOverflowWithUnpack' => [
'code' => <<<'PHP'
<?php
class A {
public const I = [
9223372036854775807 => 0,
...[1], // this is a fatal error
];
}
PHP,
'error_message' => 'InvalidArrayOffset',
],
];
}
}
100 changes: 100 additions & 0 deletions tests/ExpressionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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}",
],
];
yield 'autoincrementAlmostOverflow' => [
'code' => <<<'PHP'
<?php
$a = [
9223372036854775806 => 0,
1, // expected key = PHP_INT_MAX
];
PHP,
'assertions' => [
'$a===' => 'array{9223372036854775806: 0, 9223372036854775807: 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',
];

yield 'autoincrementOverflow' => [
'code' => <<<'PHP'
<?php
$a = [
9223372036854775807 => 0,
1, // this is a fatal error
];
PHP,
'error_message' => 'InvalidArrayOffset',
];

yield 'autoincrementOverflowWithUnpack' => [
'code' => <<<'PHP'
<?php
$a = [
9223372036854775807 => 0,
...[1], // this is a fatal error
];
PHP,
'error_message' => 'InvalidArrayOffset',
];
}
}