Skip to content

Fix IndexOutOfRangeException when a comment line ends on the buffer boundary - #2381

Open
scepion1d wants to merge 1 commit into
JoshClose:masterfrom
scepion1d:fix/comment-line-buffer-boundary
Open

Fix IndexOutOfRangeException when a comment line ends on the buffer boundary#2381
scepion1d wants to merge 1 commit into
JoshClose:masterfrom
scepion1d:fix/comment-line-buffer-boundary

Conversation

@scepion1d

Copy link
Copy Markdown

Why?

CsvParser throws System.IndexOutOfRangeException when parsing a comment (or blank) line whose trailing line ending lands exactly on the read-buffer boundary. It reproduces reliably at comment-line lengths of the form k × BufferSize − 2 for power-of-two k (2, 4, 8, …), because FillBuffer doubles the buffer when a record outgrows it. Any input with a sufficiently long comment line can hit this

What?

  • Edit: src/CsvHelper/CsvParser.cs - In ReadLine, a comment/blank line is consumed by ReadBlankLine, which reads the line including its \r\n. On the state-machine resume path (after a buffer refill), completion fell through to c = buffer[bufferPosition] without re-checking bufferPosition < charsRead. When the line ending aligned with the buffer boundary (bufferPosition == charsRead == buffer.Length), this read past the end of the buffer. The fix loops back (continue) when a BlankLine state completes, so the bound is re-evaluated - mirroring the already-safe fresh-detection path.
  • Tests: parameterized by line ending (CRLF and LF) - an exhaustive comment-length sweep (sync + async) across buffer sizes 16/32/64, plus explicit  k × BufferSize  boundary cases at 4096/8192. The CRLF cases reproduce the crash without the fix; the LF cases lock in that LF stays safe.

@scepion1d
scepion1d force-pushed the fix/comment-line-buffer-boundary branch from a1717ce to 35a6b05 Compare July 1, 2026 09:44
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.

1 participant