Skip to content

fix: add bounds check in ProcessSnippetInjection to prevent IndexOutOfRangeException#323

Merged
MIchaelMainer merged 2 commits intoOneDrive:masterfrom
gavinbarron:fix/snippet-injection-index-out-of-range
Mar 17, 2026
Merged

fix: add bounds check in ProcessSnippetInjection to prevent IndexOutOfRangeException#323
MIchaelMainer merged 2 commits intoOneDrive:masterfrom
gavinbarron:fix/snippet-injection-index-out-of-range

Conversation

@gavinbarron
Copy link
Contributor

@gavinbarron gavinbarron commented Mar 17, 2026

Problem

The Generate snippets pipeline step crashes with:

System.AggregateException: One or more errors occurred. (Index was outside the bounds of the array.)
 ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at ApiDoctor.ConsoleApp.Program.ProcessSnippetInjection(...)

Root Cause

In ProcessSnippetInjection, the FindEndOfTabSection state accesses originalFileContents[currentIndex + 1] without bounds checking. When a markdown file's tab section terminator --- is the very last line with no trailing blank line, this throws an IndexOutOfRangeException.

Fix

Added a bounds check (\currentIndex + 1 < originalFileContents.Length) before accessing the next array element.

Tests

  • *\ExistingTabSection_DashesAsLastLine_DoesNotThrow* — reproduces the exact pipeline crash scenario
  • *\FirstInsertion_CommentOnFirstLine_DoesNotThrow* — covers the
    equestStartLine - 1\ edge case

Copilot AI review requested due to automatic review settings March 17, 2026 21:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash in the snippet-injection pipeline by preventing an out-of-bounds array access when a markdown tab section terminator (---) is the last line of a file.

Changes:

  • Add a bounds check before reading originalFileContents[currentIndex + 1] when detecting the end of a tab section.
  • Add unit tests covering the EOF --- scenario and an additional “comment on first line” edge-case scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ApiDoctor.Console/Program.cs Adds bounds check in the tab-section end detection to avoid IndexOutOfRangeException.
ApiDoctor.Console.UnitTests/ProcessSnippetInjectionTests.cs Adds tests intended to reproduce the EOF --- crash scenario and cover an additional edge case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…fRangeException

When a markdown file's tab section terminator '---' is the very last
line with no trailing blank line, two issues occurred:

1. Accessing originalFileContents[currentIndex + 1] without bounds
   checking caused an IndexOutOfRangeException crash.
2. The '---' line was not included in the removable splice range,
   leaving a stray terminator in the output.

Fix both by:
- Setting snippetsTabSectionEndLine to currentIndex + 1 so the '---'
  line is always included in the splice range.
- Adding a bounds check before peeking at the next line.
- Clamping the FileSplicer offset when insertionLine falls past the
  end of the array after the splice.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gavinbarron gavinbarron force-pushed the fix/snippet-injection-index-out-of-range branch from acf2c81 to 65605e5 Compare March 17, 2026 22:18
@MIchaelMainer MIchaelMainer merged commit c855708 into OneDrive:master Mar 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants