-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix TabExpansion2 variable leak when completing variables #18763
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
Merged
daxian-dbw
merged 4 commits into
PowerShell:master
from
MartinGC94:FixTabExpansion2VarLeak
Jun 19, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
896748e
Fix TabExpansion2 variable leak when completing variables
MartinGC94 f1f2fab
Add null check for parent scope
MartinGC94 8581b54
Add check for dot sourced TabExpansion2 call.
MartinGC94 c1adf27
Merge branch 'master' into FixTabExpansion2VarLeak
MartinGC94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
If
TabExpansion2is defined in a module then this will revert to the module'sscriptscope rather than the actual previous scope.Also if
TabExpansion2is dot sourced from global,Parentwill be null hereThere 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.
What's the difference? If I look at the session state API it seems to simply use the Parent property to move to different scopes:
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/SessionStateScopeAPIs.cs#L128
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/SessionStateScopeEnumerator.cs#L30
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/SessionStateVariableAPIs.cs#L1791
Can you show a simple example where it would cause a problem?
How do you dot source from global? Do you mean like this:
. $Function:TabExpansion2 "$"because if so it won't use the parent because the command name isn't "TabExpansion2".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.
A
SessionStateis sort of like aStackof scopes. Every module has its own stack. To move to a different stack,EngineSessionStateneeds to be assigned. Though, afaik the previousSessionStateis only stored in a local variable inside a few differenttry/finallys.You'd do
. TabExpansion2 'etc', or the more likely route would be callingPowerShell.AddCommand("TabExpansion2", useLocalScope: false)from a host application.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.
All right, thanks for the explanation. I've added a null check for the dot sourcing scenario.
As for the module scope, would it be any different from the current behavior? If you are tab completing a variable today it's just calling
Get-Item variable:\*from the tabexpansion2 scope which as far as I can tell retrieves variables from that scope and traverses through theParentproperty on each scope. So with my change the only difference would be that it skips the initial TabExpansion2 scope, right?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.
So
nullis one potential dot sourcing problem. Lets say the debugger is stopped in the middle of a script and the REPL starts. IfTabExpansion2is dot sourced then we'd be reverting not justTabExpansion2but the scope of the currently running command as well. Now, I can't say for sure if that would be a problem, but some very bizarre bugs are possible when changing to an unexpected scope.Ideally we'd be able to detect that
TabExpansion2was dot sourced and abort the attempt to revert scope. I don't know of a reliable way to tell that from here though, @daxian-dbw may have some ideasMmmm that's a fair point. Though maybe the behavior should be to skip trying to "fix" the issue if
EngineSessionStateis notTopLevelSessionState. I can't come up with a specific scenario, but changing scope when a module is involved worries me, and even the fix doesn't necessarily help the scenario.Although at the same time, if
TabExpansion2is defined with no session state affinity, and lets say the debugger is stopped inside module code, then we'd want to revert still.Maybe the check should be
if (commandInfo.ScriptBlock.SesssionStateInternal != null) { DontRevert }?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.
I think you do without realizing. There's a
UseLocalScopeproperty in theCurrentCommandProcessorand you said thatPowerShell.AddCommand("TabExpansion2", useLocalScope: false)is equivalent to dot sourcing, so can't we just use that? It seems to toggle back and forth like I would expect when I dotsource or run it normally.Alternatively, instead of changing the scope we could just add the parent scope as an additional property to the CompletionContext object and update the variable/member completion code to use the parent scope if it's available. I don't think any other completion paths is affected by the session state but I could be wrong.
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.
Oh yay I was wrong! 😁 I must have been thinking of when you only have the call stack, good catch!