From 312b7461056893ca16320ab7079c7db6eba302ce Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Tue, 17 Jan 2023 19:31:32 +0100 Subject: [PATCH] Fix off-by-one index validation bug in "ToArrayByIndex" --- MoreLinq.Test/ToArrayByIndexTest.cs | 2 +- MoreLinq/ToArrayByIndex.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MoreLinq.Test/ToArrayByIndexTest.cs b/MoreLinq.Test/ToArrayByIndexTest.cs index 27d3567c4..fcc02f64e 100644 --- a/MoreLinq.Test/ToArrayByIndexTest.cs +++ b/MoreLinq.Test/ToArrayByIndexTest.cs @@ -83,7 +83,7 @@ public void ToArrayByIndexWithLengthWithBadIndexSelectorThrows(int length, int b Assert.That(() => input.ToArrayByIndex(length, _ => badIndex), Throws.TypeOf()); - Assert.That(() => input.ToArrayByIndex(10, _ => -1, BreakingFunc.Of()), + Assert.That(() => input.ToArrayByIndex(length, _ => badIndex, BreakingFunc.Of()), Throws.TypeOf()); } diff --git a/MoreLinq/ToArrayByIndex.cs b/MoreLinq/ToArrayByIndex.cs index c4ff35474..5fa513ab5 100644 --- a/MoreLinq/ToArrayByIndex.cs +++ b/MoreLinq/ToArrayByIndex.cs @@ -249,7 +249,7 @@ public static TResult[] ToArrayByIndex(this IEnumerable 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();