Repository navigation
Conversation
orien
force-pushed
the
fix-invalid-encoding-error-loop
branch
4 times, most recently
from
November 29, 2026 01:08
f20f01e to
2f242ad
Compare
orien
force-pushed
the
fix-invalid-encoding-error-loop
branch
from
November 29, 2026 01:34
2f242ad to
76eecb8
Compare
orien
force-pushed
the
fix-invalid-encoding-error-loop
branch
3 times, most recently
from
November 29, 2026 03:14
3c21c19 to
6c3f9b8
Compare
orien
force-pushed
the
fix-invalid-encoding-error-loop
branch
2 times, most recently
from
November 29, 2026 06:49
2d4cb5d to
bbeab91
Compare
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
force-pushed
the
fix-invalid-encoding-error-loop
branch
from
November 29, 2026 06:57
bbeab91 to
c92d09b
Compare
orien
marked this pull request as ready for review
November 29, 2026 07:18
Author
|
/cc @kou |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #398.
Context
With File or IO input, an invalid byte sequence can make
CSV#shiftraise on every later call without reading any input, whileCSV#eof?stays false. A loop that rescuesCSV::MalformedCSVErrorto skip bad rows never ends. This happens in these cases:build_scannerraises before@scanneris assigned, so the next call reads the same sample again. This loops on every release from 3.1.2.parse_quotable_robustis 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.encoding: "UTF-8:UTF-16LE").IO#getsraisesEncoding::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
CSV::InvalidEncodingErrorand stops. Later calls returnnil,eof?returns true andlinereturns"", for String, File and IO input alike.parse_quotable_robustreads the next chunk before it returns the current row.InputsScanner#scannow defers the error from that read until the scanner next reads from the input that failed, so data thatkeep_backpushed back is read first."\r\n", the rest of it at the start of the invalid chunk ends the previous row first.read_chunkadjusts the keeps only just before it replaces the scanner, soCSV#linefor the last row isn't doubled.IO#getsare raised asCSV::InvalidEncodingError, in the sample and in later chunks, with the originalEncodingerror as thecause. The message names the IO's encodings, and an undefined conversion is reported as one, through a new optionalmessage:keyword onCSV::InvalidEncodingError.new.CSV#shift,CSV#eof?,CSV::MalformedCSVErrorandCSV::InvalidEncodingErrordescribe the stop, and how to read past invalid data by replacing it first.Considerations
parse_quotable_looseandparse_no_quote(the default for files without escaped quotes, andquote_char: nil) used to skip the bad chunk and continue, as didparse_quotable_robustwith CRLF orskip_lines. They now stop, so rows after the bad line are no longer returned, andeof?is true although input is unread. Withquote_char: nilno field can span lines, so resuming there could be kept if you prefer, as could a way to tell "stopped" from "finished".encoding:used to raiseEncoding::InvalidByteSequenceErrororEncoding::UndefinedConversionError(EncodingErrors). It now raisesCSV::InvalidEncodingError(aCSV::MalformedCSVError) with the original error as itscause, sorescue Encoding::...around such reads needsrescue CSV::InvalidEncodingError. String input with a transcodingencoding:still raises theEncodingerror fromCSV.new, and the writer is unchanged.IO#getsdiscards 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.