Strip NUL bytes from CSV input to prevent crash

When a CSV file contains NUL bytes the csv module raises
_csv.Error: line contains NUL, crashing the insert command without a
useful error message. Stripping \x00 from each line before passing it
to the reader preserves all meaningful data while matching the
behaviour users expect. Fixes #582.
This commit is contained in:
ikatyal2110 2026-07-17 14:35:13 +00:00
commit e1d0fba30f
No known key found for this signature in database
2 changed files with 21 additions and 1 deletions

View file

@ -1237,7 +1237,10 @@ def insert_upsert_implementation(
csv_reader_args["delimiter"] = delimiter
if quotechar:
csv_reader_args["quotechar"] = quotechar
reader = csv_std.reader(decoded, **csv_reader_args) # type: ignore
reader = csv_std.reader( # type: ignore
(line.replace("\x00", "") for line in decoded),
**csv_reader_args,
)
first_row = next(reader)
if no_headers:
headers = ["untitled_{}".format(i + 1) for i in range(len(first_row))]