Skip to content

Commit

Permalink
Static analysis: protect against mutating fields in hash calculations (
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Dec 18, 2021
1 parent 58b46ae commit d5cac5b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private bool Equals(TableReferenceExpression tableReferenceExpression)

/// <inheritdoc />
public override int GetHashCode()
=> Alias.GetHashCode();
=> 0;
}

private sealed class ConcreteColumnExpression : ColumnExpression
Expand Down
53 changes: 3 additions & 50 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;

Expand Down Expand Up @@ -3499,54 +3500,6 @@ private bool Equals(SelectExpression selectExpression)

/// <inheritdoc />
public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(base.GetHashCode());

foreach (var projection in _projection)
{
hash.Add(projection);
}

foreach (var projection in _clientProjections)
{
hash.Add(projection);
}

foreach (var (projectionMember, expression) in _projectionMapping)
{
hash.Add(projectionMember);
hash.Add(expression);
}

foreach (var tag in Tags)
{
hash.Add(tag);
}

foreach (var table in _tables)
{
hash.Add(table);
}

hash.Add(Predicate);

foreach (var groupingKey in _groupBy)
{
hash.Add(groupingKey);
}

hash.Add(Having);

foreach (var ordering in _orderings)
{
hash.Add(ordering);
}

hash.Add(Offset);
hash.Add(Limit);
hash.Add(IsDistinct);

return hash.ToHashCode();
}
// Since equality above is reference equality, hash code can also be based on reference.
=> RuntimeHelpers.GetHashCode(this);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ private bool Equals(TableExpressionBase tableExpressionBase)

/// <inheritdoc />
public override int GetHashCode()
=> HashCode.Combine(Alias);
=> 0;
}

0 comments on commit d5cac5b

Please sign in to comment.