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

Conversation

eddynaka
Copy link
Collaborator

@eddynaka eddynaka commented Apr 26, 2022

Fixes #2481
This change will fix the ArgumentNullException when passing a null comparer to the ConcurrentDictionary when using the TypedPropertiesDictionary.

The issue:

  1. public class SarifWorkItemContext : PropertiesDictionary, IDisposable
    {
    public SarifWorkItemContext()
    {
    this.CurrentProvider = FilingClient.SourceControlProvider.AzureDevOps;
    this.AssemblyLocationMap = new Dictionary<string, string>();
    }
    public SarifWorkItemContext(SarifWorkItemContext initializer) : base(initializer)
    {
    }
  2. public class PropertiesDictionary : TypedPropertiesDictionary<object>
    {
    internal const string DEFAULT_POLICY_NAME = "default";
    public PropertiesDictionary() : this(null)
    {
    }
    public PropertiesDictionary(PropertiesDictionary initializer) :
    this(initializer, null)
    {
    }
  3. public class TypedPropertiesDictionary<T> : ConcurrentDictionary<string, T>, IMarker where T : new()
    {
    public TypedPropertiesDictionary() : this(null, StringComparer.Ordinal)
    {
    }
    public TypedPropertiesDictionary(PropertiesDictionary initializer, IEqualityComparer<string> comparer) : base(comparer)
    {
    if (initializer != null)
    {
    foreach (string key in initializer.Keys)
    {
    this[key] = (T)initializer[key];
    }
    }
    }

With that, on (3), the second constructor will receive a null comparer, generating the ArgumentNullException.

@eddynaka eddynaka changed the title Defaulting comparer when its null Fix ArgumentNullException when PropertiesDictionary is used and comparer is null Apr 26, 2022
@@ -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.

@@ -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.

@eddynaka eddynaka marked this pull request as ready for review April 26, 2022 18:29
@eddynaka eddynaka requested a review from marmegh April 26, 2022 18:29
@@ -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.'

Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

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

:shipit:

@eddynaka eddynaka merged commit cb83683 into main Apr 26, 2022
@eddynaka eddynaka deleted the users/ednakamu/fixing-argument-null-exception branch April 26, 2022 18:52
@eddynaka eddynaka mentioned this pull request Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PropertiesDictionary is throwing ArgumentNullException when comparer is null
2 participants