Skip to content

Commit

Permalink
Fix off-by-one index validation bug in "ToArrayByIndex"
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Jan 17, 2023
1 parent 18dcf2c commit 312b746
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MoreLinq.Test/ToArrayByIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void ToArrayByIndexWithLengthWithBadIndexSelectorThrows(int length, int b
Assert.That(() => input.ToArrayByIndex(length, _ => badIndex),
Throws.TypeOf<IndexOutOfRangeException>());

Assert.That(() => input.ToArrayByIndex(10, _ => -1, BreakingFunc.Of<int, object>()),
Assert.That(() => input.ToArrayByIndex(length, _ => badIndex, BreakingFunc.Of<int, object>()),
Throws.TypeOf<IndexOutOfRangeException>());
}

Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/ToArrayByIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static TResult[] ToArrayByIndex<T, TResult>(this IEnumerable<T> source, i
{
var i = indexSelector(e);

if (i < 0 || i > array.Length)
if (i < 0 || i >= array.Length)
{
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new IndexOutOfRangeException();
Expand Down

0 comments on commit 312b746

Please sign in to comment.