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
@hpoettker

hpoettker commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

The build of your PR failed on time-outs within the Linux builds. This should be fixed with #437.

Please rebase on dev and push the commit at a time outside of the European afternoon/evening. Otherwise, the build will probably fail due to a shortage of ARM MacOS runners. I guess, a lot of iOS to-do-list apps need to be built at that time. 😄

@ziyziy89
ziyziy89 force-pushed the csv-extra-columns-bound branch from dff6ffa to 15d9949 Compare November 20, 2026 06:25
@ziyziy89

Copy link
Copy Markdown
Contributor Author

Rebased on dev so it picks up #437, and pushed early in the European morning this time, so hopefully the runners are free. I re-ran the reproducer against the rebased branch before pushing and it still rejects the wider rows before column_width is touched, while normal and short-row CSVs parse the same as before.

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.

2 participants