Skip to content

Stop parsing after an invalid encoding error - #399

Open
orien wants to merge 1 commit into
ruby:mainfrom
orien:fix-invalid-encoding-error-loop
Open

orien wants to merge 1 commit into
ruby:mainfrom
orien:fix-invalid-encoding-error-loop

Conversation

@orien

@orien orien commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #398.

Context

With File or IO input, an invalid byte sequence can make CSV#shift raise on every later call without reading any input, while CSV#eof? stays false. A loop that rescues CSV::MalformedCSVError to skip bad rows never ends. This happens in these cases:

  • Invalid byte in the sample read for row separator detection (the start of a file or IO). build_scanner raises before @scanner is assigned, so the next call reads the same sample again. This loops on every release from 3.1.2.
  • Invalid chunk while parse_quotable_robust is parsing, for example after a row with escaped quotes. It only reads a new chunk after a successful scan, so it raises "TODO: Meaningful message" forever. This loops from 3.3.6.
  • Transcoding IO (encoding: "UTF-8:UTF-16LE"). IO#gets raises Encoding::InvalidByteSequenceError, which leaves the parser in the same state.

In the other cases, the parser resumes at the next chunk. After bytes it can't decode, it can't tell where the current record ends, so that can turn part of a field into a row: a bad byte in "junk\xFF\n9999,admin,true\nX" makes the next row ["9999", "admin", "true"], and a bad line longer than 8 KB returns its tail as a row.

Change

  • After any encoding error, the parser raises one CSV::InvalidEncodingError and stops. Later calls return nil, eof? returns true and line returns "", for String, File and IO input alike.
  • After the sample, every row before the invalid line is returned, with the invalid line's number in the error:
    • parse_quotable_robust reads the next chunk before it returns the current row. InputsScanner#scan now defers the error from that read until the scanner next reads from the input that failed, so data that keep_back pushed back is read first.
    • When the 8 KB chunk limit splits a row separator such as "\r\n", the rest of it at the start of the invalid chunk ends the previous row first.
    • When skipping a malformed line reads the invalid chunk after it, the malformed line's error is raised first.
    • read_chunk adjusts the keeps only just before it replaces the scanner, so CSV#line for the last row isn't doubled.
  • Transcoding errors from IO#gets are raised as CSV::InvalidEncodingError, in the sample and in later chunks, with the original Encoding error as the cause. The message names the IO's encodings, and an undefined conversion is reported as one, through a new optional message: keyword on CSV::InvalidEncodingError.new.
  • The docs for CSV#shift, CSV#eof?, CSV::MalformedCSVError and CSV::InvalidEncodingError describe the stop, and how to read past invalid data by replacing it first.

Considerations

  • Behaviour change, needs a release note: parse_quotable_loose and parse_no_quote (the default for files without escaped quotes, and quote_char: nil) used to skip the bad chunk and continue, as did parse_quotable_robust with CRLF or skip_lines. They now stop, so rows after the bad line are no longer returned, and eof? is true although input is unread. With quote_char: nil no field can span lines, so resuming there could be kept if you prefer, as could a way to tell "stopped" from "finished".
  • Compatibility, needs a release note: reading a File or IO with a transcoding encoding: used to raise Encoding::InvalidByteSequenceError or Encoding::UndefinedConversionError (EncodingErrors). It now raises CSV::InvalidEncodingError (a CSV::MalformedCSVError) with the original error as its cause, so rescue Encoding::... around such reads needs rescue CSV::InvalidEncodingError. String input with a transcoding encoding: still raises the Encoding error from CSV.new, and the writer is unchanged.
  • Known limits:
    • An error in the sample still discards the rows in the sample, and an invalid byte there is reported as line 1 for a file larger than the sample. Report the right line for invalid encoding in an IO sample #393 fixes that line number, and the two changes combine without conflict.
    • With a transcoding IO, IO#gets discards the data it read before the error, so the line of a transcoding error in the sample isn't known, and a row whose "\r\n" is split by the chunk limit just before the error is lost.

@orien
orien force-pushed the fix-invalid-encoding-error-loop branch 4 times, most recently from f20f01e to 2f242ad Compare November 29, 2026 01:08
@orien orien changed the title Stop parsing after an invalid encoding error Recover from an invalid encoding error in File or IO input Nov 29, 2026
@orien
orien force-pushed the fix-invalid-encoding-error-loop branch from 2f242ad to 76eecb8 Compare November 29, 2026 01:34
@orien orien changed the title Recover from an invalid encoding error in File or IO input Stop parsing after an invalid encoding error that can't be skipped Nov 29, 2026
@orien
orien force-pushed the fix-invalid-encoding-error-loop branch 3 times, most recently from 3c21c19 to 6c3f9b8 Compare November 29, 2026 03:14
@orien orien changed the title Stop parsing after an invalid encoding error that can't be skipped Stop parsing File or IO input after an invalid encoding error Nov 29, 2026
@orien
orien force-pushed the fix-invalid-encoding-error-loop branch 2 times, most recently from 2d4cb5d to bbeab91 Compare November 29, 2026 06:49
@orien orien changed the title Stop parsing File or IO input after an invalid encoding error Stop parsing after an invalid encoding error Nov 29, 2026
GitHub: Fix rubyGH-398

With File or IO input, CSV#shift could raise the same error on every
later call without reading any input, and CSV#eof? stayed false. A loop
that rescues CSV::MalformedCSVError to skip bad rows never ended. This
happened for an invalid byte in the sample read for row separator
detection, and for an invalid chunk while parse_quotable_robust was
parsing, which only reads a new chunk after a successful scan.

After bytes it can't decode, the parser can't tell where the current
record ends: the bytes may be inside a quoted field that spans lines,
or partway through a line longer than a chunk. Resuming at the next
chunk can return part of a field as a row. So parsing now stops after
any encoding error: later calls return nil, CSV#eof? returns true and
CSV#line returns "". Previously parse_quotable_loose and parse_no_quote
resumed at the next chunk, and so did parse_quotable_robust with CRLF
row separators or skip_lines. String input returned nil from later
calls, but CSV#eof? was false and CSV#line raised TypeError.

Transcoding errors from IO#gets are raised as CSV::InvalidEncodingError
too, in the sample and in later chunks, with the original
Encoding::InvalidByteSequenceError or Encoding::UndefinedConversionError
as the cause. The message names the IO's encodings, and an undefined
conversion says so, through a new message: keyword on
CSV::InvalidEncodingError.new.

After the sample, every row before the invalid line is returned, with
the invalid line's number in the error:

- The robust parser reads the next chunk as soon as a scan reaches the
  end of the current one, before the row is returned. InputsScanner#scan
  now defers an error from that read until the scanner next reads from
  the input that failed.
- When a chunk boundary splits a row separator of two or more
  characters, the rest of the separator at the start of the invalid
  chunk ends the previous row first. With a transcoding IO, IO#gets
  discards that rest, so the previous row is lost.
- Skipping a malformed line can read the invalid chunk after it. The
  error for the malformed line is raised first.
- InputsScanner#read_chunk adjusts the keeps only just before it
  replaces the scanner, so CSV#line for the last row doesn't contain it
  twice.

An error in the sample still discards the rows in the sample, as for
String input.

The docs for CSV#shift, CSV#eof?, CSV::MalformedCSVError and
CSV::InvalidEncodingError describe the stop and how to read past
invalid data.
@orien
orien force-pushed the fix-invalid-encoding-error-loop branch from bbeab91 to c92d09b Compare November 29, 2026 06:57
@orien
orien marked this pull request as ready for review November 29, 2026 07:18
@orien

orien commented Nov 29, 2026

Copy link
Copy Markdown
Author

/cc @kou

This branch has not been deployed

No deployments
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.

CSV#shift raises the same InvalidEncodingError forever on File or IO input

1 participant