mbuchetics/RangeTree

Protect the RangeTree against StackOverflow exceptions

apacha opened this issue · 5 comments

When creating the tree with an illegal range, the current implementation crashes with a StackOverflow exception.

This code, added to the samples in sufficient to reproduce the issue:
IEnumerable<RangeItem> items = new []{ new RangeItem(11, 10, "1") };
var tree = new RangeTree<int, RangeItem>(items, new RangeItemComparer());

The problem is the illegal range from 11 to 10. I'd be happy if an illegal range exception was raised or something similar, but crashing with a StackOverflow terminates the entire application quite abruptly.
I suggest that we add a simple check when creating the tree to prevent this exception from happening.

beefo commented

I'm getting the same issue.

Sorry for the issues. I am not programming C# anymore and therefore not maintaining this project at the moment. Please make a fork with your changes or bugfixes and I can link to it in the readme.

I see. I will gladly continue to maintain this project and fix the open issues. Please add me as collaborator, transfer the repo to my account (apacha) or link to my fork. Cheers and thanks for your work.

Great! I just added you as a collaborator.

beefo commented

I ended up adding this to my local version of Range.cs:

public Range(T from, T to)
    : this()
{
    if (from.CompareTo(to) == 1)
    {
        throw new ArgumentOutOfRangeException($"{nameof(from)} cannot be larger than {nameof(to)}");
    }
    From = from;
    To = to;
}

Is this similar to your solution @apacha ?