From 7bd355e3d0896e05a5c83b1726903b9b79346375 Mon Sep 17 00:00:00 2001 From: Shourya Singh Date: Fri, 27 Nov 2026 14:10:45 +0530 Subject: [PATCH] Report the right line for invalid encoding in an IO sample GitHub: fix GH-385 With row_sep: :auto, a File or another IO is sampled to detect the row separator. build_scanner computed the line of an invalid byte only when the sample was the whole input. Otherwise the sample went to InputsScanner, which raised InvalidEncoding for the whole sample before any row was parsed, so the error always said line 1. Check the samples the same way before building InputsScanner. They're joined only when one of them is invalid, so valid input isn't copied. --- lib/csv/parser.rb | 23 +++++++++++++++-------- test/csv/interface/test_read.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb index f14be3af..5b995ba2 100644 --- a/lib/csv/parser.rb +++ b/lib/csv/parser.rb @@ -891,16 +891,12 @@ def build_scanner string = @samples[0] end if string - unless string.valid_encoding? - index = string.lines(@row_separator).index do |line| - !line.valid_encoding? - end - if index - raise InvalidEncodingError.new(@encoding, @lineno + index + 1) - end - end + check_encoding(string) Scanner.new(string) else + unless @samples.all?(&:valid_encoding?) + check_encoding(@samples.join("")) + end inputs = @samples.collect do |sample| StringIO.new(sample) end @@ -908,6 +904,17 @@ def build_scanner InputsScanner.new(inputs, @encoding, @row_separator) end end + + def check_encoding(string) + return if string.valid_encoding? + + index = string.lines(@row_separator).index do |line| + !line.valid_encoding? + end + if index + raise InvalidEncodingError.new(@encoding, @lineno + index + 1) + end + end end def skip_needless_lines diff --git a/test/csv/interface/test_read.rb b/test/csv/interface/test_read.rb index bd55ae7d..c150ef57 100644 --- a/test/csv/interface/test_read.rb +++ b/test/csv/interface/test_read.rb @@ -145,6 +145,35 @@ def test_open_encoding_invalid end end + def test_open_encoding_invalid_line_number + File.open(@input.path, "wb") do |file| + file << "a,b\nc,d\n\xFF,1\n" + end + CSV.open(@input.path, encoding: "UTF-8") do |csv| + error = assert_raise(CSV::InvalidEncodingError) do + csv.read + end + assert_equal([Encoding::UTF_8, "Invalid byte sequence in UTF-8 in line 3."], + [error.encoding, error.message]) + end + end + + def test_open_encoding_invalid_line_number_in_partial_sample + # The row separator is detected from the first 32 KiB of a File. The + # invalid byte is in that sample, but the file is longer than it. + File.open(@input.path, "wb") do |file| + file << "a,b\n\x80,1\n" + file << "x,y\n" * 16384 + end + CSV.open(@input.path, encoding: "UTF-8") do |csv| + error = assert_raise(CSV::InvalidEncodingError) do + csv.read + end + assert_equal([Encoding::UTF_8, "Invalid byte sequence in UTF-8 in line 2."], + [error.encoding, error.message]) + end + end + def test_open_encoding_nonexistent _output, error = capture_output do CSV.open(@input.path, encoding: "nonexistent") do