Fix bug in Intervals<T>::update

When `Intervals<T>::update` was called with `finish=UINT64_MAX`, the loop
condition `i->start() < finish` would remain true for every interval, until `i`
went past the last interval, at which point `i->start()` (and all the other
accesses to `i` within the loop) have undefined behaviuor.
This commit is contained in:
Benson Joeris
2019-02-13 11:16:04 -05:00
committed by Baldur Karlsson
parent 2828232ae8
commit d438adb701
2 changed files with 22 additions and 1 deletions
+1 -1
View File
@@ -222,7 +222,7 @@ public:
i->split(start);
// Loop over all the intervals in `a` that intersect the interval [start, finish)
for(; i->start() < finish; i++)
for(; i != end() && i->start() < finish; i++)
{
if(i->finish() > finish)
{
+21
View File
@@ -244,6 +244,27 @@ TEST_CASE("Test Intervals type", "[intervals]")
test.update(7, 20, 1, [](uint64_t, uint64_t) -> uint64_t { return 1; });
check_intervals(test, {{0, 0, 5}, {5, 1, 30}, {30, 0, UINT64_MAX}});
};
SECTION("update a interval starting at 0")
{
Intervals<uint64_t> test = make_intervals({{0, 0, 5}, {5, 1, 10}, {10, 0, UINT64_MAX}});
test.update(0, 10, 1, [](uint64_t x, uint64_t y) -> uint64_t { return x + y; });
check_intervals(test, {{0, 1, 5}, {5, 2, 10}, {10, 0, UINT64_MAX}});
};
SECTION("update a interval finishing at UINT64_MAX")
{
Intervals<uint64_t> test = make_intervals({{0, 0, 5}, {5, 1, 10}, {10, 0, UINT64_MAX}});
test.update(5, UINT64_MAX, 1, [](uint64_t x, uint64_t y) -> uint64_t { return x + y; });
check_intervals(test, {{0, 0, 5}, {5, 2, 10}, {10, 1, UINT64_MAX}});
};
SECTION("update entire range")
{
Intervals<uint64_t> test = make_intervals({{0, 0, 5}, {5, 1, 10}, {10, 0, UINT64_MAX}});
test.update(0, UINT64_MAX, 1, [](uint64_t x, uint64_t y) -> uint64_t { return x + y; });
check_intervals(test, {{0, 1, 5}, {5, 2, 10}, {10, 1, UINT64_MAX}});
};
};
SECTION("mergeIntervals tests")