Skip to content

Fix NullReferenceException in AlignAssignmentStatement rule when CheckHashtable is enabled #838

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

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
24 changes: 17 additions & 7 deletions Rules/AlignAssignmentStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,28 @@ private static List<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
foreach (var kvp in hashtableAst.KeyValuePairs)
{
var keyStartOffset = kvp.Item1.Extent.StartOffset;
bool keyStartOffSetReached = false;
var keyTokenNode = tokenOps.GetTokenNodes(
token => token.Extent.StartOffset == keyStartOffset).FirstOrDefault();
if (keyTokenNode == null
|| keyTokenNode.Next == null
|| keyTokenNode.Next.Value.Kind != TokenKind.Equals)
token =>
{
if (keyStartOffSetReached)
{
return token.Kind == TokenKind.Equals;
}
if (token.Extent.StartOffset == keyStartOffset)
{
keyStartOffSetReached = true;
}
return false;
}).FirstOrDefault();
if (keyTokenNode == null || keyTokenNode.Value == null)
{
return null;
continue;
}
var assignmentToken = keyTokenNode.Value.Extent;

nodeTuples.Add(new Tuple<IScriptExtent, IScriptExtent>(
kvp.Item1.Extent,
keyTokenNode.Next.Value.Extent));
kvp.Item1.Extent, assignmentToken));
}

return nodeTuples;
Expand Down
27 changes: 27 additions & 0 deletions Tests/Engine/SettingsTest/Issue828/Issue828.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Describe "Issue 828: No NullReferenceExceptionin AlignAssignmentStatement rule when CheckHashtable is enabled" {
It "Should not throw" {
# For details, see here: https://github.com/PowerShell/PSScriptAnalyzer/issues/828
# The issue states basically that calling 'Invoke-ScriptAnalyzer .' with a certain settings file being in the same location that has CheckHashtable enabled
# combined with a script contatining the command '$MyObj | % { @{$_.Name = $_.Value} }' could make it throw a NullReferencException.
$cmdletThrewError = $false
$initialErrorActionPreference = $ErrorActionPreference
$initialLocation = Get-Location
try
{
Set-Location $PSScriptRoot
$ErrorActionPreference = 'Stop'
Invoke-ScriptAnalyzer .
}
catch
{
$cmdletThrewError = $true
}
finally
{
$ErrorActionPreference = $initialErrorActionPreference
Set-Location $initialLocation
}

$cmdletThrewError | Should Be $false
}
}
63 changes: 63 additions & 0 deletions Tests/Engine/SettingsTest/Issue828/PSScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@{
Severity = @(
'Error',
'Warning',
'Information'
)
ExcludeRules = @(
'PSUseOutputTypeCorrectly',
'PSUseShouldProcessForStateChangingFunctions'
)
Rules = @{
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
}
PSAvoidUsingCmdletAliases = @{
# only whitelist verbs from *-Object cmdlets
Whitelist = @(
'%',
'?',
'compare',
'foreach',
'group',
'measure',
'select',
'sort',
'tee',
'where'
)
}
PSPlaceCloseBrace = @{
Enable = $true
NoEmptyLineBefore = $false
IgnoreOneLineBlock = $true
NewLineAfter = $false
}
PSPlaceOpenBrace = @{
Enable = $true
OnSameLine = $true
NewLineAfter = $true
IgnoreOneLineBlock = $true
}
PSProvideCommentHelp = @{
Enable = $true
ExportedOnly = $true
BlockComment = $true
VSCodeSnippetCorrection = $true
Placement = "before"
}
PSUseConsistentIndentation = @{
Enable = $true
IndentationSize = 4
Kind = "space"
}
PSUseConsistentWhitespace = @{
Enable = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $false
CheckSeparator = $true
}
}
}
2 changes: 2 additions & 0 deletions Tests/Engine/SettingsTest/Issue828/script.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This script has to be like that in order to reproduce the issue
$MyObj | % { @{$_.Name = $_.Value} }