Skip to content

Commit 0cef8c0

Browse files
authored
Merge pull request #661 from rjmholt/pester-null-deref-660
Fix possible null dereference in PesterDocumentSymbolProvider
2 parents 58fab2d + 2d460a2 commit 0cef8c0

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/PowerShellEditorServices/Symbols/PesterDocumentSymbolProvider.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ private static bool IsNamedCommandWithArguments(Ast ast)
4545
{
4646
CommandAst commandAst = ast as CommandAst;
4747

48-
return
49-
commandAst != null &&
50-
commandAst.InvocationOperator != TokenKind.Dot &&
51-
PesterSymbolReference.GetCommandType(commandAst.GetCommandName()).HasValue &&
52-
commandAst.CommandElements.Count >= 2;
48+
return commandAst != null &&
49+
commandAst.InvocationOperator != TokenKind.Dot &&
50+
PesterSymbolReference.GetCommandType(commandAst.GetCommandName()).HasValue &&
51+
commandAst.CommandElements.Count >= 2;
5352
}
5453

5554
/// <summary>
@@ -89,7 +88,11 @@ private static bool IsPesterCommand(CommandAst commandAst)
8988
private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFile scriptFile, CommandAst pesterCommandAst)
9089
{
9190
string testLine = scriptFile.GetLine(pesterCommandAst.Extent.StartLineNumber);
92-
string commandName = pesterCommandAst.GetCommandName();
91+
PesterCommandType? commandName = PesterSymbolReference.GetCommandType(pesterCommandAst.GetCommandName());
92+
if (commandName == null)
93+
{
94+
return null;
95+
}
9396

9497
// Search for a name for the test
9598
// If the test has more than one argument for names, we set it to null
@@ -122,7 +125,7 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
122125

123126
return new PesterSymbolReference(
124127
scriptFile,
125-
commandName,
128+
commandName.Value,
126129
testLine,
127130
testName,
128131
pesterCommandAst.Extent
@@ -179,7 +182,7 @@ public class PesterSymbolReference : SymbolReference
179182

180183
internal PesterSymbolReference(
181184
ScriptFile scriptFile,
182-
string commandName,
185+
PesterCommandType commandType,
183186
string testLine,
184187
string testName,
185188
IScriptExtent scriptExtent)
@@ -190,14 +193,14 @@ internal PesterSymbolReference(
190193
scriptFile.FilePath,
191194
testLine)
192195
{
193-
this.Command = GetCommandType(commandName).Value;
196+
this.Command = commandType;
194197
this.TestName = testName;
195198
}
196199

197200
internal static PesterCommandType? GetCommandType(string commandName)
198201
{
199202
PesterCommandType pesterCommandType;
200-
if (!PesterKeywords.TryGetValue(commandName, out pesterCommandType))
203+
if (commandName == null || !PesterKeywords.TryGetValue(commandName, out pesterCommandType))
201204
{
202205
return null;
203206
}

0 commit comments

Comments
 (0)