-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix NullReferenceException in CompleteFilename when called with empty string #26286
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
Conversation
Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
|
@copilot Please move new test to TabCompletion.Tests.ps1 file. |
…ts.ps1 Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
|
@copilot Please remove your comments from the new test. |
Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a NullReferenceException thrown when calling CompleteFilename("") with an empty string by adding a null check for context.RelatedAsts before accessing it.
Key Changes:
- Added null/empty check for
context.RelatedAstsbefore array indexing - Added fallback to use current provider path when
RelatedAstsis null/empty - Added Pester test to verify the fix works correctly
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs | Added null check for context.RelatedAsts and fallback logic to use current provider path |
| test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 | Added test case to verify CompleteFilename("") doesn't throw exception |
Comments suppressed due to low confidence (1)
src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs:1
- The else block at line 4598 is paired with the if statement at line 4583, but based on the overall control flow structure, this creates an incorrect nesting. The else block (lines 4599-4602) should be associated with the if statement at line 4583, but there's already a closing brace at line 4598 that closes that if block. This means the else at line 4598 cannot be paired with the if at line 4583. The correct structure should have the else block (4599-4602) immediately follow the closing brace at 4598 without the 'else' keyword on line 4598, or the braces need to be restructured to properly nest the conditional logic.
// Copyright (c) Microsoft Corporation.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Suppressed by #26291 |
|
📣 Hey @@Copilot, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Summary
Fixed
NullReferenceExceptionwhen calling[System.Management.Automation.CompletionCompleters]::CompleteFilename("").Root Cause
The public API
CompleteFilename(string fileName)creates aCompletionContextwithRelatedAstsset to null. The internal method tried to accesscontext.RelatedAsts[^1]without checking for null, causing the exception.Fix
Added null/empty check for
context.RelatedAstsbefore accessing it. When null or empty, falls back to usingcontext.ExecutionContext.SessionState.Internal.CurrentLocation.ProviderPath.Test
Added Pester test in
TabCompletion.Tests.ps1that verifies:CompleteFilename("")doesn't throw exceptionOriginal prompt
This section details on the original issue you should resolve
<issue_title>
[System.Management.Automation.CompletionCompleters]::CompleteFilename("")throws NullReferenceException</issue_title><issue_description>### Prerequisites
Steps to reproduce
on 7.6.0-preview.5
[System.Management.Automation.CompletionCompleters]::CompleteFilename("")throwsNullReferenceExceptionExpected behavior
should returns CompletionResultActual behavior
Error details