10BC0 Fix NRE in ConsiceView by iSazonov · Pull Request #11435 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@iSazonov
Copy link
Collaborator
@iSazonov iSazonov commented Dec 26, 2019

PR Summary

Use LanguagePrimitives.TryConvertTo() as last resort to string conversion.

PR Context

Playing with AccountManagement module I catch an error where properties with disposed object reference present.

PS C:\Windows\System32> $e.psobject.properties

GetterScript    : & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
SetterScript    :
MemberType      : ScriptProperty
IsSettable      : False
IsGettable      : True
Value           :
TypeNameOfValue : System.Object
Name            : PSMessageDetails
IsInstance      : False

MemberType      : Property
Value           : System.Management.Automation.MethodInvocationException: Exception calling "ToString" with "0"
                  argument(s): "Can�t delete an already deleted object"
                   ---> System.InvalidOperationException: Can�t delete an already deleted object
                     at System.DirectoryServices.AccountManagement.Principal.CheckDisposedOrDeleted()
                     at System.DirectoryServices.AccountManagement.Principal.HandleGet[T](T& currentValue, String
                  name, LoadState& state)
                     at System.DirectoryServices.AccountManagement.Principal.get_Name()
                     at System.DirectoryServices.AccountManagement.Principal.ToString()
                     at CallSite.Target(Closure , CallSite , Object )
                     --- End of inner exception stack trace ---
                     at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception
                  exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo) in C:\Users\sie
                  \Documents\GitHub\iSazonov\PowerShell\src\System.Management.Automation\engine\runtime\Operations\Misc
                  Ops.cs:line 2062
                     at CallSite.Target(Closure , CallSite , Object )
                     at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
                     at CallSite.Target(Closure , CallSite , Object )
                     at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame) in C:
                  \Users\sie\Documents\GitHub\iSazonov\PowerShell\src\System.Management.Automation\engine\interpreter\D
                  ynamicInstructions.Generated.cs:line 141
                     at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                  frame) in C:\Users\sie\Documents\GitHub\iSazonov\PowerShell\src\System.Management.Automation\engine\i
                  nterpreter\ControlFlowInstructions.cs:line 357
IsSettable      : False
IsGettable      : True
TypeNameOfValue : System.Exception
Name            : Exception
IsInstance      : True

MemberType      : Property
Value           :
IsSettable      : False
IsGettable      : True
TypeNameOfValue : System.Object
Name            : TargetObject
IsInstance      : True

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 26, 2019
@iSazonov iSazonov added this to the GA-consider milestone Dec 26, 2019
@iSazonov iSazonov requested a review from SteveL-MSFT December 26, 2019 04:43
@iSazonov iSazonov self-assigned this Dec 26, 2019
Copy link
Member
@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to add a test? Seems like we can create an ErrorRecord that hits this code path?

@iSazonov
Copy link
Collaborator Author
iSazonov commented Jan 3, 2020

Possible to add a test? Seems like we can create an ErrorRecord that hits this code path?

@SteveL-MSFT I did not find how to do it :-(

@SteveL-MSFT
Copy link
Member

@iSazonov seems like line 802 would have already covered this? Can you show how it looks before and after this change with your error?

@adityapatwardhan
Copy link
Member

@iSazonov Can you provide a repro with your module?

@iSazonov
Copy link
Collaborator Author
iSazonov commented Jan 10, 2020

My fix is not right.

$value = $prop.Value.ToString().Trim()

The line throws if ToString() throws ($prop.Value has a reference on an object with property with disposed object in my case)

I will update the PR.

Copy link
Member
@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so it's calling a method on a disposed object.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Jan 10, 2020
@SteveL-MSFT
Copy link
Member

Tried to simulate the original issue of a disposed object, but couldn't find a way to do it.

@iSazonov
Copy link
Collaborator Author

Tried to simulate the original issue of a disposed object, but couldn't find a way to do it.

I too. I could port a code from AccountManagement module but I think it is not correct behavior for ToString() to throw and I will report the issue to Core team.

@iSazonov iSazonov merged commit ac06918 into PowerShell:master Jan 14, 2020
@iSazonov iSazonov deleted the consiceview-null-check branch January 14, 2020 03:33
daxian-dbw pushed a commit that referenced this pull request Jan 14, 2020
Use LanguagePrimitives.TryConvertTo() as last resort to string conversion.
@daxian-dbw daxian-dbw modified the milestones: GA-approved, 7.0.0-rc.2 Jan 14, 2020
@ghost
Copy link
ghost commented Jan 16, 2020

🎉v7.0.0-rc.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

0