Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1145,11 +1145,9 @@ def parse_unquoted_column_value
if @first_column_separators
while true
@scanner.keep_start
is_column_end = @column_ends.all? do |column_end|
@scanner.scan(column_end)
end
is_separator_end = parse_column_end || parse_row_end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling parse_column_end / parse_row_end here nests a second keep_start/keep_back (the multi-character branch of each) inside the outer keep_start. InputsScanner loses data when a nested keep crosses a chunk edge, so values get cut on IO input.

Repro: same line, read from a pipe so the 8 KiB chunks are used:

require "csv"
n = 8190
line = %Q{""""::#{"a" * n}:b::c\n}
rd, wr = IO.pipe; wr.write(line); wr.close
p CSV.new(rd, col_sep: "::").read.map { |r| r.map { |f| f && f.size } }
# main:     [[1, 8192, 1]]   ("aaa...:b", "c")
# this PR:  [[1, 8190]]      (":b" and the "c" column are gone)

With n from 8170 to 8200, 28 of 31 lengths are wrong on this branch and 0 on main. It also shows with the CI scanner mode, e.g. CSV_PARSER_SCANNER_TEST=yes CSV_PARSER_SCANNER_TEST_CHUNK_SIZE=1: CSV.parse(%Q{"""\"::a:b::c\n}, col_sep: "::") gives [["\"", "a"]] instead of [["\"", "a:b", "c"]].

Suggested fix: keep one keep level per check, like the old loop, e.g.

def at_separator?(separator, separator_characters)
  @scanner.keep_start
  if separator_characters
    found = separator_characters.all? {|c| @scanner.scan(c)}
  else
    found = @scanner.scan(separator)
  end
  @scanner.keep_back
  found
end

and break if at_separator?(@column_end, @column_ends) or at_separator?(@row_end, @row_ends) instead of the nested calls. A with_chunk_size("1") test would catch it.

@scanner.keep_back
break if is_column_end
break if is_separator_end
sub_separator = @scanner.scan_all(@first_column_separators)
break if sub_separator.nil?
value << sub_separator
Expand Down