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 in ResizeOptions equality logic #6128 #6308

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/MudBlazor.UnitTests/Services/ResizeOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,15 @@ public void Equals_NotTheSame_DiffersInBreakpointDefinitions_DifferentValues()
option1.Should().NotBe(option2);
option2.Should().NotBe(option1);
}

[Test]
public void ComparingWithNull_ShouldNot_Fail()
{
var option = new ResizeOptions();
// this should not cause nullref
(option == null).Should().Be(false);
(option != null).Should().Be(true);
}
}

}
55 changes: 25 additions & 30 deletions src/MudBlazor/Services/ResizeListener/ResizeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ public class ResizeOptions : IEquatable<ResizeOptions>
/// </summary>
public Dictionary<string, int> BreakpointDefinitions { get; set; } = new();

public static bool operator ==(ResizeOptions l, ResizeOptions r) => l.Equals(r);
public static bool operator !=(ResizeOptions l, ResizeOptions r) => !l.Equals(r);
public static bool operator ==(ResizeOptions l, ResizeOptions r)
{
if (l is null && r is null)
return true;
if (l is null && r is not null)
return false;
return l.Equals(r);
}

public static bool operator !=(ResizeOptions l, ResizeOptions r) => !(l == r);

public override bool Equals(object obj)
{
Expand All @@ -43,6 +51,8 @@ public override bool Equals(object obj)

public bool Equals(ResizeOptions other)
{
if (other is null)
return false;
if (ReportRate != other.ReportRate ||
EnableLogging != other.EnableLogging ||
SuppressInitEvent != other.SuppressInitEvent ||
Expand All @@ -51,36 +61,21 @@ public bool Equals(ResizeOptions other)
return false;
}

if (BreakpointDefinitions is not null)
{
if (other.BreakpointDefinitions is null) { return false; }
else
{
if (BreakpointDefinitions.Count != other.BreakpointDefinitions.Count)
{
return false;
}

foreach (var item in BreakpointDefinitions.Keys)
{
if (other.BreakpointDefinitions.ContainsKey(item) == false)
{
return false;
}

if (BreakpointDefinitions[item] != other.BreakpointDefinitions[item])
{
return false;
}
}

return true;
}
}
else
{
if (BreakpointDefinitions is null)
return other.BreakpointDefinitions is null;
if (other.BreakpointDefinitions is null)
return false;
if (BreakpointDefinitions.Count != other.BreakpointDefinitions.Count)
return false;
foreach (var item in BreakpointDefinitions.Keys)
{
if (other.BreakpointDefinitions.ContainsKey(item) == false)
return false;
if (BreakpointDefinitions[item] != other.BreakpointDefinitions[item])
return false;
}

return true;
}

public override int GetHashCode() => ReportRate;
Expand Down