Skip to content

Commit 452168c

Browse files
committed
Assert self->buffer is locked, fix __init__ race
1 parent afe79c7 commit 452168c

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix race conditions when re-initializing a :class:`TextIOWrapper` object.

Modules/_io/clinic/textio.c.h

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/textio.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ struct textio
734734
/* Helpers to safely operate on self->buffer.
735735
736736
self->buffer can be detached (set to NULL) by any user code that is called
737-
leading to NULL pointer dereferences (see gh-143008, gh-142594). Protect against
738-
that by using helpers to check self->buffer validity at callsites. */
737+
leading to NULL pointer dereferences (see gh-143008, gh-142594). Protect
738+
against that by using helpers to check self->buffer validity at callsites. */
739739
static PyObject *
740740
buffer_access_safe(textio *self)
741741
{
@@ -752,6 +752,9 @@ buffer_access_safe(textio *self)
752752
}
753753
return NULL;
754754
}
755+
756+
// Returning a borrowed reference is safe since TextIOWrapper
757+
// methods are protected by critical sections.
755758
return self->buffer;
756759
}
757760

@@ -1119,6 +1122,7 @@ io_check_errors(PyObject *errors)
11191122

11201123

11211124
/*[clinic input]
1125+
@critical_section
11221126
_io.TextIOWrapper.__init__
11231127
buffer: object
11241128
encoding: str(accept={str, NoneType}) = None
@@ -1162,7 +1166,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
11621166
const char *encoding, PyObject *errors,
11631167
const char *newline, int line_buffering,
11641168
int write_through)
1165-
/*[clinic end generated code: output=72267c0c01032ed2 input=e6cfaaaf6059d4f5]*/
1169+
/*[clinic end generated code: output=72267c0c01032ed2 input=0f077220214c40a4]*/
11661170
{
11671171
PyObject *raw, *codec_info = NULL;
11681172
PyObject *res;

0 commit comments

Comments
 (0)