[BREAKING]: `ParsedThemeRule.GetHashCode()` violates hash code contract causing incorrect behavior of equality and in hash-based collections
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 44/100
Hướng nghiên cứu
Start with the attached ParsedThemeRuleTests.cs.txt test cases and locate ParsedThemeRule and its Equals() and GetHashCode() methods. Check the equality and hash behavior for differing names and equivalent parent scope contents, then run the relevant tests. Done means equality distinguishes names and equal objects produce matching hashes, including when used as dictionary keys.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
The ParsedThemeRule class has a problematic implementation of GetHashCode() and Equals() methods are inconsistent, violating the fundamental hash code contract. This causes incorrect behavior when ParsedThemeRule instances are used testing equality or as keys in Dictionary<T> or stored in HashSet<T>.
I've noticed 3 distinct manifestations of this bug:
- Two
ParsedThemeRuleinstances which only differ bynameare considered equal - Two
ParsedThemeRuleinstances which are "equivalent" by definition do not produce the same hash code and thus break the GetHashCode API contract. - The
ParsedThemeRuletype cannot effectively be used as a key in a Dictionary
[!Important]
Most of theGetHashCodeimplementations I have seen inTextMateSharphave a fundamental design issue: the fields are not immutable. Fields/properties that are used inGetHashCodeshould be immutable. This ensures that objects aren't lost in hash-based collectins.
The bug fix I propose here DOES NOT address the field immutability issue. That would be a breaking change since the fields are currently writable.
Root Causes
GetHashCode()method: does not usenameproperty as part of the hash code calculationEquals()method: UsesEnumerable.SequenceEqual()forparentScopescomparison (content-based)GetHashCode()method: UsesList<T>.GetHashCode()forparentScopes(identity-based/reference-based)
This means twoParsedThemeRuleobjects that are considered equal byEquals()can produce different hash codes, violating the contract:
If
obj1.Equals(obj2)returnstrue, thenobj1.GetHashCode()MUST equalobj2.GetHashCode()
Impact
This bug affects any code using ParsedThemeRule for Equality or in hash-based collections:
- ❌ Two
ParsedThemeRuleinstances which only differ bynameare considered equal - ❌ Objects which have the same content but different references are not considered equal
- ❌ Dictionary lookups may fail to find equal keys
- ❌ HashSet may store duplicate "equal" objects
- ❌ LINQ operations like
Distinct()may not work correctly - ❌ Caching strategies using these objects as keys will be unreliable
Steps to Reproduce
Several of the unit tests in the attached unit test class demonstrate the problem:
Equals_DifferentName_ReturnsFalseGetHashCode_CanBeUsedAsDictionaryKeyGetHashCode_EqualObjects_ProduceSameHashCodeToString_NullParentScopes_DoesNotThrow
Proposed Solution
The GetHashCode() method needs to:
- Include the
namefield in the hash computation - Compute a hash based on the contents of
parentScopes, not its reference
Here's the fix:
public override int GetHashCode()
{
int prime = 31;
int result = 1;
result = (prime * result) + ((background == null) ? 0 : background.GetHashCode());
result = (prime * result) + (int)fontStyle;
result = (prime * result) + ((foreground == null) ? 0 : foreground.GetHashCode());
result = (prime * result) + index;
result = (prime * result) + ((name == null) ? 0 : name.GetHashCode());
if (parentScopes != null)
{
foreach (string parentScope in parentScopes)
{
result = (prime * result) + ((parentScope == null) ? 0 : parentScope.GetHashCode());
}
}
result = (prime * result) + ((scope == null) ? 0 : scope.GetHashCode());
return result;
}
public override bool Equals(object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (GetType() != obj.GetType())
return false;
ParsedThemeRule other = (ParsedThemeRule)obj;
if (background == null)
{
if (other.background != null)
return false;
}
else if (!background.Equals(other.background))
return false;
if (fontStyle != other.fontStyle)
return false;
if (foreground == null)
{
if (other.foreground != null)
return false;
}
else if (!foreground.Equals(other.foreground))
return false;
if (index != other.index)
return false;
if (parentScopes == null)
{
if (other.parentScopes != null)
return false;
}
else if (!Enumerable.SequenceEqual(parentScopes, other.parentScopes))
return false;
if (scope == null)
{
if (other.scope != null)
return false;
}
else if (!scope.Equals(other.scope))
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.Equals(other.name))
return false;
return true;
}
- Ngôn ngữ chính
- C#
- Star
- 170
- Fork
- 35
- Merge trung bình
- 2 giờ 52 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của danipen/TextMateSharp
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xmlCó thể đã có người làm @alexeyfala đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
danipen/TextMateSharp#145 · 2 bình luận ·
-
[Breaking]: Visibility of fields/properties/classes does not match upstream tm4e implementationĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
danipen/TextMateSharp#123 · 3 bình luận ·
-
good first issue
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
danipen/TextMateSharp#119 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
danipen/TextMateSharp#118 · 11 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
danipen/TextMateSharp#75 · 2 bình luận ·
Tất cả issue của danipen/TextMateSharp
Issue tương tự
-
good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Klyroo/klyroo-dashboard#12 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CONTRIBUTING rules Node out in step 0, then tells client-side contributors to run node --test, which is what CI gates onCó thể đã có người làm @yadavraj2001 đã nhận hôm nay. Đang mởdocumentation good first issue
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
juandresrodca/AdminForge#73 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[辞書] VIVANTĐang mở要トリアージ 辞書
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 94/100
christian-raj/S-Aloha#504 ·
-
Issue: Feature Request Xenonid
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày