Skip to content

NRE in CommandSearcher.GetNextCmdlet#12659

Merged
4 commits merged intoPowerShell:masterfrom
powercode:Bugs/CommandSearcherNullRef
May 19, 2020
Merged

NRE in CommandSearcher.GetNextCmdlet#12659
4 commits merged intoPowerShell:masterfrom
powercode:Bugs/CommandSearcherNullRef

Conversation

@powercode
Copy link
Copy Markdown
Collaborator

@powercode powercode commented May 14, 2020

PR Summary

Fixes a NullReferenceException when searching for malformed cmdlet names

PR Context

In GetNextCmdlet, there is a check

if (!useAbbreviationExpansion && PSSnapinQualifiedCommandName == null)
{
    return null;
}

i.e. the null check is only done if useAbbreviationExpansion is false.

Later on we reference PSSnapinQualifiedCommandName in anyway and get an NRE.

PR Checklist

@powercode
Copy link
Copy Markdown
Collaborator Author

@SteveL-MSFT Can you review? Didn't you add the abbreviation feature?

@iSazonov iSazonov requested a review from SteveL-MSFT May 14, 2020 11:13
@iSazonov
Copy link
Copy Markdown
Collaborator

Repo steps:

 gcm a\b\get-qqq* -UseAbbreviationExpansion

Get-Command: Object reference not set to an instance of an object.

@iSazonov
Copy link
Copy Markdown
Collaborator

We could simplify the change:

                    WildcardPattern cmdletMatcher = null;
                    if (PSSnapinQualifiedCommandName != null && PSSnapinQualifiedCommandName.ShortName != null)
                    {
                        cmdletMatcher =
                            WildcardPattern.Get(
                                PSSnapinQualifiedCommandName.ShortName,
                                WildcardOptions.IgnoreCase);
                    }

                    SessionStateInternal ss = _context.EngineSessionState;

                    foreach (List<CmdletInfo> cmdletList in ss.GetCmdletTable().Values)
                    {
                        foreach (CmdletInfo cmdlet in cmdletList)
                        {
                            if ((cmdletMatcher != null && cmdletMatcher.IsMatch(cmdlet.Name)) ||
                                (_commandResolutionOptions.HasFlag(SearchResolutionOptions.FuzzyMatch) &&
                                FuzzyMatcher.IsFuzzyMatch(cmdlet.Name, _commandName)))
                            {
                                if (string.IsNullOrEmpty(PSSnapinQualifiedCommandName?.PSSnapInName) ||
                                    (PSSnapinQualifiedCommandName.PSSnapInName.Equals(
                                        cmdlet.ModuleName, StringComparison.OrdinalIgnoreCase)))

@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label May 14, 2020
@iSazonov iSazonov added this to the 7.1.0-preview.3 milestone May 14, 2020
@adityapatwardhan
Copy link
Copy Markdown
Member

@powercode please have a look at failing tests

…is true.

This will result in NRE if we don't check for it.
@powercode powercode force-pushed the Bugs/CommandSearcherNullRef branch from c658f21 to 0cc26d9 Compare May 18, 2020 15:28
@powercode powercode force-pushed the Bugs/CommandSearcherNullRef branch from e70700b to fe84eaa Compare May 19, 2020 06:43
Functionallity wise equivalent but easier to read.
@iSazonov iSazonov added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log AutoMerge informs the bot to automerge the PR and removed CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log labels May 19, 2020
@ghost
Copy link
Copy Markdown

ghost commented May 19, 2020

Hello @iSazonov!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 240a8f7 into PowerShell:master May 19, 2020
@ghost
Copy link
Copy Markdown

ghost commented Jun 25, 2020

🎉v7.1.0-preview.4 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AutoMerge informs the bot to automerge the PR CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants