Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1170

When GetHashCode() is called from BsonElement with circular reference - overflow exception is thrown

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • BSON
    • None

    Description

      Please consider the following code:

      var item1 = new BsonDocument();
      var item2 = new BsonDocument();
      item1.Add(new BsonElement("cycle_1", item2));
      item2.Add(new BsonElement("cycle_2", item1));

      Now, If I call item1.GetHashCode(), the application will terminate due to stack overflow exception (in my case I tried to add item1 to HashSet).
      Sure an exception should be thrown, but stackoverflow exception terminates a .NET application. I looked in the source code, and saw that BsonDocument.GetHashCode also calls GetHashCode of inner elements, which in this case cause the infinite loop.
      Is this considered a bug, or should the programmer know he should avoid such scenarios?

      Attachments

        Activity

          People

            Unassigned Unassigned
            shaye Shay
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: