Skip to content

read_csv: reject data rows wider than the CSV header - #435

Open
ziyziy89 wants to merge 1 commit into
WizardMac:devfrom
ziyziy89:csv-extra-columns-bound
Open

ziyziy89 wants to merge 1 commit into
WizardMac:devfrom
ziyziy89:csv-extra-columns-bound

Conversation

@ziyziy89

Copy link
Copy Markdown
Contributor

readstat_parse_csv sizes column_width, variables, is_date and is_date_time from the number of cells in the header row, but csv_metadata_cell indexes all of them with the cell number of every data row and never compares it to the header count. A CSV whose data row has more cells than the header (a single trailing comma is enough, e.g. a then 1,) makes pass 1 read and write column_width[1] past an 8-byte heap allocation (ASan heap-buffer-overflow at read_csv.c:85/86 when running readstat in.csv meta.json out.csv), and in pass 2 the csv_value callbacks in mod_csv/mod_dta/mod_sav would read variables[] and is_date[] out of bounds as well.

Skip such cells in csv_metadata_cell before any per-column array is touched, and fail the parse with READSTAT_ERROR_COLUMN_COUNT_MISMATCH once libcsv is done, since its callbacks can't stop the parse themselves. Pass 1 fails before any output is written, and rows with fewer cells than the header behave as before. Checked under ASan with the CLI's two-pass flow against libcsv: the overflow is gone, and well-formed and short-row CSVs parse the same as before.

@hpoettker hpoettker mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant