Skip to content

gh-145105: Fix crash in csv.reader with re-entrant iterator#145106

Merged
gpshead merged 1 commit intopython:mainfrom
raminfp:fix-gh-145105-csv-reader-reentrant-crash
Apr 11, 2026
Merged

gh-145105: Fix crash in csv.reader with re-entrant iterator#145106
gpshead merged 1 commit intopython:mainfrom
raminfp:fix-gh-145105-csv-reader-reentrant-crash

Conversation

@raminfp
Copy link
Copy Markdown
Contributor

@raminfp raminfp commented Feb 22, 2026

The fix adds a guard right after PyIter_Next returns: if
self->fields has been set to NULL by a re-entrant call, raise
csv.Error instead of continuing into parse_process_char.

When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
Copy link
Copy Markdown
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

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

esoteric seeming but true. thanks!

@gpshead gpshead added the needs backport to 3.14 bugs and security fixes label Apr 11, 2026
@gpshead gpshead enabled auto-merge (squash) April 11, 2026 22:14
@gpshead gpshead disabled auto-merge April 11, 2026 22:17
@gpshead gpshead merged commit 20994b1 into python:main Apr 11, 2026
58 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @raminfp for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 11, 2026
…thonGH-145106)

When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
(cherry picked from commit 20994b1809f9c162e4cae01a5af08bd492ede9f9)

Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 11, 2026

GH-148404 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Apr 11, 2026
@gpshead gpshead added the needs backport to 3.13 bugs and security fixes label Apr 11, 2026
@miss-islington-app
Copy link
Copy Markdown

Thanks @raminfp for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 11, 2026
…thonGH-145106)

When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
(cherry picked from commit 20994b1)

Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 11, 2026

GH-148405 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Apr 11, 2026
gpshead pushed a commit that referenced this pull request Apr 11, 2026
…H-145106) (#148405)

gh-145105: Fix crash in csv.reader with re-entrant iterator (GH-145106)

When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
(cherry picked from commit 20994b1)

Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
gpshead pushed a commit that referenced this pull request Apr 11, 2026
…H-145106) (#148404)

gh-145105: Fix crash in csv.reader with re-entrant iterator (GH-145106)

When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
(cherry picked from commit 20994b1)

Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
@raminfp raminfp deleted the fix-gh-145105-csv-reader-reentrant-crash branch April 12, 2026 04:55
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