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 ArgumentNullException when PropertiesDictionary is used and comparer is null #2482

Merged
merged 11 commits into from
Apr 26, 2022
2 changes: 2 additions & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SARIF Package Release History (SDK, Driver, Converters, and Multitool)

## Unreleased

* BUGFIX: Fix `ArgumentNullException` when `PropertiesDictionary` is used and comparer is null. [#2482](https://github.com/microsoft/sarif-sdk/pull/2482)
Copy link
Member

Choose a reason for hiding this comment

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

used

'is instantiated with a null comparer.'

* BUGFIX: Fix `UnhandledEngineException` when target path does not exist for multithreaded application by validating directories as is done for singlethreaded analysis. [#2461](https://github.com/microsoft/sarif-sdk/pull/2461)

## **v2.4.14** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.4.14) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.4.14) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.4.14) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.4.14) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/2.4.14)
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/TypedPropertiesDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public TypedPropertiesDictionary() : this(null, StringComparer.Ordinal)
{
}

public TypedPropertiesDictionary(PropertiesDictionary initializer, IEqualityComparer<string> comparer) : base(comparer)
public TypedPropertiesDictionary(PropertiesDictionary initializer, IEqualityComparer<string> comparer) : base(comparer ?? StringComparer.Ordinal)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

StringComparer

As I mentioned in the description, when this is instantiated by the SarifWorkItemContext, the comparer will be null.

So, to use the same strategy as the constructor above, I'm defaulting the comparer to StringComparer.Ordinal.

Copy link
Member

Choose a reason for hiding this comment

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

Yes! Good fix. Did you confirm that this is the behavior for Dictionary<string, T> by examining the reference implementation? Ideally, we would match the behavior of that concrete type.

{
if (initializer != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net48</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Test.UnitTests.Sarif.WorkItems</RootNamespace>
Expand Down
18 changes: 18 additions & 0 deletions src/Test.UnitTests.Sarif/PropertiesDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ public void PropertiesDictionary_TestCacheDescription()
PropertiesDictionary_TestCacheDescription_Helper(GeneratePropertiesDictionaryProperty, PROPERTIES_NONDEFAULT);
}

[Fact]
public void PropertiesDictionary_ShouldNotThrownException_WhenComparerIsNull()
{
var initializer = new PropertiesDictionary();

Exception exception = Record.Exception(() =>
{
new PropertiesDictionary(initializer);
});
exception.Should().BeNull();

exception = Record.Exception(() =>
{
new PropertiesDictionary(initializer, comparer: null);
});
exception.Should().BeNull();
}

private void PropertiesDictionary_TestCacheDescription_Helper(Func<int, string, IOption> GeneratePropertyMethod, object value)
{
string textLoaded = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net48</TargetFrameworks>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

net48

To simulate the issue, we need to change to targetFramework 48. It will happen every time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As a side note, I also tried to add net48 for all the other test projects to enhance our testing capabilities BUT the pipeline is timing out. So, we have to investigate why is that happening.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also included a simple and focused test to prevent us from facing this issue again.

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down