Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2087 bug in SingleRange.ixSubIndexByPosOnNew. #2091

Merged
merged 6 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public final OrderedLongSet ixRemove(final long key) {
return OrderedLongSet.twoRanges(rangeStart(), key - 1, key + 1, rangeEnd());
}

private static long addSaturated(final long x, final long y) {
final long res = x + y;
if (res < x) {
return Long.MAX_VALUE;
}
jcferretti marked this conversation as resolved.
Show resolved Hide resolved
return res;
}

@Override
public final OrderedLongSet ixSubindexByPosOnNew(final long startPos, final long endPosExclusive) {
final long endPos = endPosExclusive - 1; // make inclusive.
Expand All @@ -219,8 +227,8 @@ public final OrderedLongSet ixSubindexByPosOnNew(final long startPos, final long
return ixCowRef();
}
return make(
Math.max(rangeStart() + startPos, rangeStart()),
Math.min(rangeStart() + endPos, rangeEnd()));
Math.max(addSaturated(rangeStart(), startPos), rangeStart()),
jcferretti marked this conversation as resolved.
Show resolved Hide resolved
Math.min(addSaturated(rangeStart(), endPos), rangeEnd()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,14 @@ public void testRowSequenceIteratorAdvanceRegression0() {
assertTrue(valid);
assertEquals(1000, rsIt.peekNextKey());
}

@Test
public void testSaturation() {
final long first = 1073741821;
final long last = first + 6;
final SingleRange sr = SingleRange.make(first, last);
final OrderedLongSet res = sr.ixSubindexByPosOnNew(5, Long.MAX_VALUE);
assertEquals(last - 1, res.ixFirstKey());
assertEquals(last, res.ixLastKey());
}
}