Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit efaae3a

Browse files
authored
Merge pull request #14 from PowerShell/cbnilrem-refactor
Apply mitigation to CimInstance disposal race condition
2 parents 7451a3e + 5ca73c8 commit efaae3a

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

src/Microsoft.Management.Infrastructure/CimInstance.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ private void Dispose(bool disposing)
384384
{
385385
if (this.InstanceHandle != null && !this.InstanceHandle.IsNull)
386386
{
387-
this.InstanceHandle.Delete();
387+
using (this.InstanceHandle)
388+
{
389+
this.InstanceHandle.Delete();
390+
}
388391
}
389392
}
390393
}

src/Microsoft.Management.Infrastructure/Native/MI_Instance.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.Management.Infrastructure.Native
55
{
6-
internal class MI_Instance : MI_NativeObjectWithFT<MI_Instance.MI_InstanceFT>
6+
internal class MI_Instance : MI_NativeObjectWithFT<MI_Instance.MI_InstanceFT>, IDisposable
77
{
88
internal MI_Result GetElement(
99
string name,
@@ -124,21 +124,6 @@ internal MI_Result Delete()
124124
return localResult;
125125
}
126126

127-
private void ZeroPtr()
128-
{
129-
if (this.isDirect)
130-
{
131-
this.allocatedData = IntPtr.Zero;
132-
}
133-
else if (this.allocatedData != IntPtr.Zero)
134-
{
135-
unsafe
136-
{
137-
*(IntPtr*)this.allocatedData = IntPtr.Zero;
138-
}
139-
}
140-
}
141-
142127
internal MI_Result IsA(
143128
MI_ClassDecl classDecl,
144129
out bool flag

src/Microsoft.Management.Infrastructure/Native/MI_NativeObject.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.Management.Infrastructure.Native
55
{
6-
internal abstract class MI_NativeObject
6+
internal abstract class MI_NativeObject : IDisposable
77
{
88
[StructLayout(LayoutKind.Sequential, CharSet = MI_PlatformSpecific.AppropriateCharSet)]
99
protected struct MI_NativeObjectNormalMembersLayout
@@ -60,11 +60,13 @@ internal struct ArrayPtr
6060
protected IntPtr allocatedData;
6161
protected bool isDirect;
6262

63-
~MI_NativeObject()
63+
public virtual void Dispose()
6464
{
6565
if (this.allocatedData != IntPtr.Zero)
6666
{
67-
Marshal.FreeHGlobal(this.allocatedData);
67+
IntPtr tmp = this.allocatedData;
68+
this.ZeroPtr();
69+
Marshal.FreeHGlobal(tmp);
6870
}
6971
}
7072

@@ -126,5 +128,20 @@ internal IntPtr Ptr
126128
return structurePtr;
127129
}
128130
}
131+
132+
protected void ZeroPtr()
133+
{
134+
if (this.isDirect)
135+
{
136+
this.allocatedData = IntPtr.Zero;
137+
}
138+
else if (this.allocatedData != IntPtr.Zero)
139+
{
140+
unsafe
141+
{
142+
*(IntPtr*)this.allocatedData = IntPtr.Zero;
143+
}
144+
}
145+
}
129146
}
130147
}

src/Microsoft.Management.Infrastructure/Native/Structures/MI_Value.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal MI_Value() : base(true)
7777
this.Dispose(false);
7878
}
7979

80-
internal void Dispose()
80+
public override void Dispose()
8181
{
8282
this.Dispose(true);
8383
GC.SuppressFinalize(this);
@@ -105,6 +105,8 @@ private void Dispose(bool disposing)
105105
this.allocatedData = IntPtr.Zero;
106106
}
107107
}
108+
109+
base.Dispose();
108110
}
109111

110112
internal string String

0 commit comments

Comments
 (0)