1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix crash for empty string array access

This commit is contained in:
bugreportuser 2019-02-24 18:41:53 -06:00 committed by Matthew Brown
parent 4f770d356e
commit 7f7125f334
2 changed files with 15 additions and 5 deletions

View File

@ -642,13 +642,17 @@ class ArrayFetchAnalyzer
if ($type instanceof TSingleLetter) { if ($type instanceof TSingleLetter) {
$valid_offset_type = Type::getInt(false, 0); $valid_offset_type = Type::getInt(false, 0);
} elseif ($type instanceof TLiteralString) { } elseif ($type instanceof TLiteralString) {
$valid_offsets = []; if (!strlen($type->value)) {
$valid_offset_type = Type::getEmpty();
} else {
$valid_offsets = [];
for ($i = -strlen($type->value), $l = strlen($type->value); $i < $l; $i++) { for ($i = -strlen($type->value), $l = strlen($type->value); $i < $l; $i++) {
$valid_offsets[] = new TLiteralInt($i); $valid_offsets[] = new TLiteralInt($i);
}
$valid_offset_type = new Type\Union($valid_offsets);
} }
$valid_offset_type = new Type\Union($valid_offsets);
} else { } else {
$valid_offset_type = Type::getInt(); $valid_offset_type = Type::getInt();
} }

View File

@ -571,6 +571,12 @@ class ArrayAccessTest extends TestCase
echo $a[-6];', echo $a[-6];',
'error_message' => 'InvalidArrayOffset', 'error_message' => 'InvalidArrayOffset',
], ],
'emptyStringAccess' => [
'<?php
$a = "";
echo $a[0];',
'error_message' => 'InvalidArrayOffset',
],
]; ];
} }
} }