… 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)
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)