SimpleJSON.JSONXXX breaks Dictionary key semantics (Equals not reflexive) + mutable hash codes in value nodes (JSONBool/JSONString/JSONNumber)

Đang mở
#118 11 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
backend

Hướng nghiên cứu

Start by locating the JSONBool, JSONString, and JSONNumber equality and hash code implementations mentioned in the report, then reproduce the dictionary behavior with the supplied C# example. Compare the behavior before and after mutation, including the same-instance lookup immediately after insertion. Done means equality is reflexive and the reported collection behavior is addressed; the issue has had no comment for 199 days.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug
I noticed that using SimpleJSON.JSONBool as a dictionary key: after adding an instance as a key, ContainsKey(sameInstance) returns false even before any mutation. This appears to be because JSONBool.Equals(object) returns true only when compared to a boxed bool, and returns false when compared to a JSONBool (including itself).

Several Issues:

  1. JSONBool.Equals is not reflexive (x.Equals(x) can be false), which breaks core .NET equality and collection semantics immediately.
  2. JSONBool/JSONString/JSONNumber have hash codes derived from mutable state, so they’re unsafe in hash-based scenarios after mutation (including implicit hashing in LINQ
  3. maybe-by-design???: cross-type equality (JSONNumber vs numeric primitives, JSONString vs string) is inherently non-symmetric, so it should either be avoided, carefully documented, or implemented via explicit comparisons/conversions rather than Equals

Mutable hash (JSONBool/JSONString/JSONNumber)
This can break any hash-based collection or algorithm if the node is mutated after being used as a key/element, for example:

  • HashSet<JSONBool> membership can "lose" an item after mutation for the same reason a dictionary key can
    ​
  • LINQ methods that build internal hash sets/lookups (e.g., Distinct, Except, Intersect, GroupBy, ToLookup) can behave inconsistently if elements/keys mutate while participating

To Reproduce
small repro:

using System;
using System.Collections.Generic;
using SimpleJSON;

public static class Program
{
    public static void Main()
    {
        const Int32 val = 1;
        SimpleJSON.JSONBool jsonBool = new SimpleJSON.JSONBool(true);
        Dictionary<SimpleJSON.JSONBool, Int32> dict = new Dictionary<SimpleJSON.JSONBool, Int32>();
        dict.Add(jsonBool, val);

        Int32 hash1 = jsonBool.GetHashCode();
        Console.WriteLine("hashcode: " + hash1);
        Console.WriteLine("containsKey: " + dict.ContainsKey(jsonBool));
        Console.WriteLine("containsValue: " + dict.ContainsValue(val));

        jsonBool.AsBool = false; // mutate the object in the dict to lose it
        Int32 hash2 = jsonBool.GetHashCode();
        Console.WriteLine("hashcode: " + hash2);
        Console.WriteLine("containsKey: " + dict.ContainsKey(jsonBool));
        Console.WriteLine("containsValue: " + dict.ContainsValue(val));
    }
}

actual output:

hashcode: 1
containsKey: False
containsValue: True
hashcode: 0
containsKey: False
containsValue: True

Expected behavior

  • dict.ContainsKey(jsonBool) should return true immediately after dict.Add(jsonBool, val) when using the same instance as the lookup key.
  • x.Equals(x) equals 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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của danipen/TextMateSharp

Tất cả issue của danipen/TextMateSharp

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.