Skip to content

NRE in CommandSearcher.GetNextCmdlet - #12659

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

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

Conversation

@powercode

@powercode Staffan Gustafsson (powercode) commented May 14, 2020

Copy link
Copy Markdown
Collaborator

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

Steve Lee (@SteveL-MSFT) Can you review? Didn't you add the abbreviation feature?

Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
@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 Ilya (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 Ilya (iSazonov) added this to the 7.1.0-preview.3 milestone May 14, 2020
@adityapatwardhan

Copy link
Copy Markdown
Member

Staffan Gustafsson (@powercode) please have a look at failing tests

…is true.

This will result in NRE if we don't check for it.
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandSearcher.cs Outdated
Functionallity wise equivalent but easier to read.
@iSazonov Ilya (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

Hello Ilya (@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

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

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
<!-- Anything that looks like this is a comment and can't be seen after the Pull Request is created. -->

# PR Summary

Fixes a NullReferenceException when searching for malformed cmdlet names

## PR Context

In GetNextCmdlet, there is a check
```csharp
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

- [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - Use the present tense and imperative mood when describing your changes
- [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress).
    - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready.
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
    - [x] None
    - **OR**
    - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
        - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
    - [x] Not Applicable
    - **OR**
    - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
        - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
    - [x] N/A or can only be tested interactively
    - **OR**
    - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)
- **Tooling**
    - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    - **OR**
    - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
        - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host).
        - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
        - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions).
        - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
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