Skip to content
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

Fix NullReferenceException when the native enumerator returns null #620

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Orc.SystemInfo.Tests/Orc.SystemInfo.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="NUnit" Version="3.13.3" PrivateAssets="all" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" PrivateAssets="all" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Orc.SystemInfo.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Orc.SystemInfo.Win32;
using Orc.SystemInfo.Wmi;

public class WindowsManagementObjectEnumeratorFacts
{
[Test]
public async Task Returns_False_When_Enumerator_Returns_Null_Async()
{
IWbemClassObject objects;
uint returnedCount;

var wbemClassObjectEnumeratorMock = new Mock<IWbemClassObjectEnumerator>();

wbemClassObjectEnumeratorMock.Setup(x => x.Next(It.IsAny<int>(), It.IsAny<uint>(), out objects, out returnedCount))
.Returns((int to, uint c, out IWbemClassObject o, out uint rc) =>
{
o = null;
rc = 0;

return new HResult(0);
});

using var enumerator = new WindowsManagementObjectEnumerator(wbemClassObjectEnumeratorMock.Object);

var moveNextResult = enumerator.MoveNext();

Assert.That(moveNextResult, Is.False);
}
}
}
1 change: 1 addition & 0 deletions src/Orc.SystemInfo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
[assembly: ComVisible(false)]

[assembly: InternalsVisibleTo("Orc.SystemInfo.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
7 changes: 6 additions & 1 deletion src/Orc.SystemInfo/Win32/HResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,9 @@ public void ThrowIfFailed()
throw ex;
}
}
}

public override string ToString()
{
return $"{_value} (Failed = {Failed})";
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Orc.SystemInfo.Win32;

using System;
using System.Threading;

public static class IWbemClassObjectEnumeratorExtensions
{
internal static IWbemClassObject Next(this IWbemClassObjectEnumerator wbemClassObjectEnumerator)
internal static IWbemClassObject? Next(this IWbemClassObjectEnumerator wbemClassObjectEnumerator)
{
const uint count = 1;
var hresult = wbemClassObjectEnumerator.Next(Timeout.Infinite, count, out var current, out _);
Copy link
Contributor

Choose a reason for hiding this comment

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

IWbemClassObjectEnumerator i think it should have nullable notation for parameter: out IWbemClassObject? objects,

Expand Down
10 changes: 9 additions & 1 deletion src/Orc.SystemInfo/Wmi/WindowsManagementObjectEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public bool MoveNext()
ThrowIfDisposed();

var currentWmiObject = _wbemClassObjectEnumerator.Next();
if (currentWmiObject is null)
{
return false;
}

#pragma warning disable IDISP003 // Dispose previous before re-assigning
Current = new WindowsManagementObject(currentWmiObject);
#pragma warning restore IDISP003 // Dispose previous before re-assigning

return true;
}

Expand All @@ -70,7 +75,10 @@ public void Dispose()
return;
}

Marshal.ReleaseComObject(_wbemClassObjectEnumerator);
if (Marshal.IsComObject(_wbemClassObjectEnumerator))
{
Marshal.ReleaseComObject(_wbemClassObjectEnumerator);
}

_disposed = true;
}
Expand Down