Skip to content

[3.14] gh-157364: Fix use-after-free in io.TextIOWrapper during reentrant detach - #159600

Merged
vstinner merged 1 commit into
python:3.14from
vstinner:textio14
Oct 16, 2026
Merged

vstinner merged 1 commit into
python:3.14from
vstinner:textio14

Conversation

@vstinner

Copy link
Copy Markdown
Member

TextIOWrapper keeps its underlying stream in a member called self->buffer. That stream can be detached by user code, such as custom .flush implementations resulting in self->buffer being set to NULL. The implementation often checked at the start of functions if self->buffer is in a good state, but did not always recheck after other Python code was called which could modify self->buffer.

The cases which need to be re-checked are hard to spot so rather than rely on reviewer effort create better safety by making all self->buffer access go through helper functions.

Thank you yihong0618 for the test, NEWS and initial implementation in gh-143041.

(cherry picked from commit db4b194)

(cherry picked from commit 8440a6a)

(cherry picked from commit e19dc47)


(cherry picked from commit ea1312b)

… reentrant detach (python#159591)

* pythongh-143008: Fix Null pointer dereferences in TextIOWrapper underlying stream access (python#145957)

TextIOWrapper keeps its underlying stream in a member called
`self->buffer`. That stream can be detached by user code, such as custom
`.flush` implementations resulting in `self->buffer` being set to NULL.
The implementation often checked at the start of functions if
`self->buffer` is in a good state, but did not always recheck after
other Python code was called which could modify `self->buffer`.

The cases which need to be re-checked are hard to spot so rather than
rely on reviewer effort create better safety by making all self->buffer
access go through helper functions.

Thank you yihong0618 for the test, NEWS and initial implementation in
pythongh-143041.

Co-authored-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit db4b194)

* pythongh-154523: Fix data-race in `TextIOWrapper.detach()` (python#154565)

(cherry picked from commit 8440a6a)

* pythongh-157364: Fix use-after-free in io.TextIOWrapper during reentrant detach (python#157370)

Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit e19dc47)

---------

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Brij Kapadia <97006829+brijkapadia@users.noreply.github.com>
Co-authored-by: Shamil <ashm.tech@proton.me>
(cherry picked from commit ea1312b)
@vstinner
vstinner enabled auto-merge (squash) October 16, 2026 23:26
@vstinner
vstinner merged commit 13fc16c into python:3.14 Oct 16, 2026
51 checks passed
@vstinner
vstinner deleted the textio14 branch October 16, 2026 23:53
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