Skip to content

gh-157379: Fix crash in csv.reader with re-entrant iterator at end of input - #157511

Open
JohannesGezachew wants to merge 1 commit into
python:mainfrom
JohannesGezachew:gh-157379-csv-reader-reentrant-eof
Open

JohannesGezachew wants to merge 1 commit into
python:mainfrom
JohannesGezachew:gh-157379-csv-reader-reentrant-eof

Conversation

@JohannesGezachew

@JohannesGezachew JohannesGezachew commented Sep 14, 2026

Copy link
Copy Markdown

Reader_iternext_lock_held() saves a pending field when the input iterator is exhausted while a quoted field is still open. If the iterator re-entered the same reader, the inner call has already returned its row and set self->fields to NULL, so parse_save_field() calls PyList_Append(NULL, ...) and crashes.

gh-145105 added a self->fields == NULL check, but only on the path where the iterator returns a line. This adds the same check (and the same csv.Error message) to the end-of-input branch, before parse_save_field() is called. With strict=True the existing "unexpected end of data" error is still raised first.

The new test in test_csv.py is the reproducer from the issue: it segfaults without this change and raises csv.Error with it.

…end of input

The pythongh-145105 guard only runs after the input iterator returns a line.
When a re-entrant call leaves a quoted field open and the outer call then
reaches the end of input, parse_save_field() appended to the NULL fields
list. Raise csv.Error in that branch too.
@python-cla-bot

python-cla-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant