1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix crash when assigning reference to unknown array offset.

This commit is contained in:
AndrolGenhald 2022-02-04 08:23:16 -06:00
parent c5722dd5e8
commit 5b469ca577
2 changed files with 35 additions and 0 deletions

View File

@ -929,6 +929,12 @@ class AssignmentAnalyzer
return false;
}
if (!isset($context->vars_in_scope[$rhs_var_id])) {
// Sometimes the $rhs_var_id isn't set in $vars_in_scope, for example if it's an unknown array offset.
$context->vars_in_scope[$rhs_var_id] = $statements_analyzer->node_data->getType($stmt->expr)
?? Type::getMixed();
}
if (isset($context->references_in_scope[$lhs_var_id])) {
// Decrement old referenced variable's reference count
$reference_count = &$context->referenced_counts[$context->references_in_scope[$lhs_var_id]];

View File

@ -218,6 +218,35 @@ class ReferenceTest extends TestCase
}
',
],
'dontCrashOnReferenceToMixedVariableArrayOffset' => [
'code' => '<?php
function func(&$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
'ignored_issues' => ['MixedArrayAccess', 'MissingParamType'],
],
'dontCrashOnReferenceToArrayUnknownOffset' => [
'code' => '<?php
function func(array &$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
],
'dontCrashOnReferenceToArrayMixedOffset' => [
'code' => '<?php
/** @param array{f: mixed} $a */
function func(array &$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
],
];
}