Add 'NullReference' checks to two code paths related to 'PseudoParameterBinder' (#5738)

1. Add a null check in the tab completion code on the binding result returned from `PseudoParameterBinder`;
2. Add a null check in `CommandDiscovery.LookupCommandProcessor` on the `CommandInfo` object returned from the method `LookupCommandInfo`.
This commit is contained in:
kwkam 2018-03-17 00:10:06 +08:00 committed by Dongbo Wang
parent 80d5df55c2
commit 5f202d676f
3 changed files with 13 additions and 3 deletions

View file

@ -6180,6 +6180,11 @@ namespace System.Management.Automation
if (commandAst != null)
{
var binding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, bindingType: PseudoParameterBinder.BindingType.ArgumentCompletion);
if (binding == null)
{
return null;
}
string parameterName = null;
foreach (var boundArg in binding.BoundArguments)
{

View file

@ -300,11 +300,15 @@ namespace System.Management.Automation
internal CommandProcessorBase LookupCommandProcessor(string commandName,
CommandOrigin commandOrigin, bool? useLocalScope)
{
CommandProcessorBase processor = null;
CommandInfo commandInfo = LookupCommandInfo(commandName, commandOrigin);
CommandProcessorBase processor = LookupCommandProcessor(commandInfo, commandOrigin, useLocalScope, null);
// commandInfo.Name might be different than commandName - restore the original invocation name
processor.Command.MyInvocation.InvocationName = commandName;
if (commandInfo != null)
{
processor = LookupCommandProcessor(commandInfo, commandOrigin, useLocalScope, null);
// commandInfo.Name might be different than commandName - restore the original invocation name
processor.Command.MyInvocation.InvocationName = commandName;
}
return processor;
}

View file

@ -424,6 +424,7 @@ Describe "TabCompletion" -Tags CI {
@{ inputStr = 'cd "$pshome\Modu"'; expected = "`"$(Join-Path $PSHOME 'Modules')`""; setup = $null }
@{ inputStr = '$PSHOME\System.Management.Au'; expected = Join-Path $PSHOME 'System.Management.Automation.dll'; setup = $null }
@{ inputStr = '"$PSHOME\System.Management.Au"'; expected = "`"$(Join-Path $PSHOME 'System.Management.Automation.dll')`""; setup = $null }
@{ inputStr = '& "$PSHOME\System.Management.Au"'; expected = "`"$(Join-Path $PSHOME 'System.Management.Automation.dll')`""; setup = $null }
## tab completion AST-based tests
@{ inputStr = 'get-date | ForEach-Object { $PSItem.h'; expected = 'Hour'; setup = $null }
@{ inputStr = '$a=gps;$a[0].h'; expected = 'Handle'; setup = $null }