Repository navigation
Description
When File or IO input contains an invalid byte sequence, CSV#shift raises CSV::InvalidEncodingError. Every later call raises the same error again without reading any more input, and CSV#eof? stays false. A loop that rescues CSV::MalformedCSVError to skip bad rows therefore never ends.
require "csv"
File.binwrite("bad.csv", "id,name\n1,bob\xFF\n2,carol\n")
CSV.open("bad.csv") do |csv|
3.times do
p csv.shift
rescue CSV::MalformedCSVError => e
puts "#{e.class}: #{e.message}"
end
p csv.eof?
endOutput with csv 3.3.7 on ruby 4.0.10:
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
false
The same bytes as a String (CSV.new("id,name\n1,bob\xFF\n2,carol\n")) raise once, and then shift returns nil.
I expected the IO case to end the same way: either raise once and then return nil with eof? true, or skip the bad row and continue. The max_field_size documentation on master says "You can continue to read the next record after the error", so rescue-and-continue looks like a supported pattern.
Cause
Parser#parsebuilds the scanner lazily with@scanner ||= build_scanner(lib/csv/parser.rb:424).- For File and IO input,
build_scannerstarts from@samples, the data read earlier to detect the row separator (lib/csv/parser.rb:886-910). That is the first 32 KB of a file, or 1 KB of other IO. - When the sample holds an invalid byte,
build_scannerraises before@scanneris assigned, either directly (:899) or throughInputsScanner#read_chunk(:316,:325). @samplesis never cleared, so the next call builds the scanner from the same data and raises again.
String input ends because @input.read consumes the StringIO, so the next call sees empty input.
A second case, 3.3.6 and later
An invalid byte after the sample, in a file whose quoted fields contain escaped quotes, also loops on 3.3.6, 3.3.7 and master:
require "csv"
data = (1..6000).map { |i| %Q{#{i},"user ""#{i}""",ok\n} }.join.b
data[60_000] = "\xFF".b
File.binwrite("bad2.csv", data)
CSV.open("bad2.csv") do |csv|
rows = errors = 0
10_000.times do
break unless csv.shift
rows += 1
rescue CSV::MalformedCSVError => e
errors += 1
puts "#{e.class}: #{e.message}" if errors <= 3
end
puts "rows=#{rows} errors=#{errors} eof?=#{csv.eof?}"
endCSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2592.
CSV::MalformedCSVError: TODO: Meaningful message in line 2593.
CSV::MalformedCSVError: TODO: Meaningful message in line 2594.
rows=2591 errors=7409 eof?=false
After the first InvalidEncodingError, each call raises "TODO: Meaningful message", and the line number goes up each time without any input being read. Twelve releases from 3.1.2 to 3.3.5 raise once and then finish the file. I haven't found the cause, but 3.3.6 includes c94763a ("Don't use enumerator in CSV"), which changed how shift re-enters the parser, so I'd look there first.
Versions
- The first case loops on every release I tested: 3.1.2, 3.1.5, 3.1.6, 3.1.9, 3.2.5, 3.2.8, 3.3.2, 3.3.5, 3.3.7 and master at 4522dc9. Older releases don't load on ruby 4.0.
CSV.foreach, which stops at the first error, is not affected.- PR Report the right line for invalid encoding in an IO sample #393, which fixes the wrong line number reported in InvalidEncodingError reports line 1 when the invalid byte is in the row separator detection sample (File/IO input) #385, doesn't change this. With it applied, both cases still loop.
Environment: ruby 4.0.10, arm64 macOS.
Activity
[-]CSV#shift raises the same InvalidEncodingError forever on File or IO input[/-][+]`CSV#shift` raises the same InvalidEncodingError forever on File or IO input[/+][-]`CSV#shift` raises the same InvalidEncodingError forever on File or IO input[/-][+]`CSV#shift` raises the same `InvalidEncodingError` forever on File or IO input[/+]Stop parsing after an invalid encoding error
Stop parsing after an invalid encoding error
Recover from an invalid encoding error in File or IO input
Recover from an invalid encoding error in File or IO input
Stop parsing after an invalid encoding error that can't be skipped
Stop parsing after an invalid encoding error that can't be skipped
Stop parsing after an invalid encoding error that can't be skipped
Stop parsing File or IO input after an invalid encoding error
Stop parsing File or IO input after an invalid encoding error
Stop parsing after an invalid encoding error
Stop parsing after an invalid encoding error