diff --git a/UndoRedo.Test/UndoRedoStackTests.cs b/UndoRedo.Test/UndoRedoStackTests.cs index e70b1bc..bfc5339 100644 --- a/UndoRedo.Test/UndoRedoStackTests.cs +++ b/UndoRedo.Test/UndoRedoStackTests.cs @@ -240,6 +240,23 @@ public void Events_FiredCorrectly() Assert.IsTrue(saveBoundaryCreatedFired, "SaveBoundaryCreated event should fire when marking as saved"); } + [TestMethod] + public void GetCommandsInRange_CountOfIntMaxValueFromNonZeroStart_ReturnsRemainingCommands() + { + StackManager stack = new(); + DelegateCommand first = new("A", () => { }, () => { }); + DelegateCommand second = new("B", () => { }, () => { }); + DelegateCommand third = new("C", () => { }, () => { }); + stack.AddCommand(first); + stack.AddCommand(second); + stack.AddCommand(third); + + // startIndex + count overflowed to a negative length before #105 + CollectionAssert.AreEqual(new[] { second, third }, stack.GetCommandsInRange(1, int.MaxValue).ToList()); + CollectionAssert.AreEqual(new[] { third }, stack.GetCommandsInRange(2, int.MaxValue).ToList()); + CollectionAssert.AreEqual(new[] { second }, stack.GetCommandsInRange(1, 1).ToList()); + } + private sealed class MockNavigationProvider : INavigationProvider { public string? LastNavigatedContext { get; private set; } diff --git a/UndoRedo/Services/StackManager.cs b/UndoRedo/Services/StackManager.cs index 59d9056..0e3c3bb 100644 --- a/UndoRedo/Services/StackManager.cs +++ b/UndoRedo/Services/StackManager.cs @@ -111,7 +111,8 @@ public IEnumerable GetCommandsInRange(int startIndex, int count) return []; } - int endIndex = Math.Min(startIndex + count, _commands.Count); - return _commands.GetRange(startIndex, endIndex - startIndex); + // Clamp against what remains rather than summing startIndex + count, which overflows for large counts + int available = _commands.Count - startIndex; + return _commands.GetRange(startIndex, Math.Min(count, available)); } }