Skip to content

Commit

Permalink
Fix NullReferenceException in ResizeOptions equality logic #6128 (#6308)
Browse files Browse the repository at this point in the history
  • Loading branch information
henon committed Feb 11, 2023
1 parent c1e8e62 commit 9c47347
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
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

0 comments on commit 9c47347

Please sign in to comment.