Skip to content

Commit af070ea

Browse files
committed
fix: apply suggestions
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 1eb62a4 commit af070ea

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Modules/_io/textio.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,22 +2416,13 @@ typedef struct {
24162416
#endif
24172417

24182418
static int
2419-
textiowrapper_parse_cookie(textio *self, cookie_type *cookie, PyObject *cookieObj)
2419+
textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
24202420
{
24212421
unsigned char buffer[COOKIE_BUF_LEN];
24222422
PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
24232423
if (cookieLong == NULL)
24242424
return -1;
24252425

2426-
// gh-143007: PyNumber_Long can call arbitrary code through __int__
2427-
// which may detach the underlying buffer.
2428-
if (self->detached) {
2429-
Py_DECREF(cookieLong);
2430-
PyErr_SetString(PyExc_ValueError,
2431-
"underlying buffer has been detached");
2432-
return -1;
2433-
}
2434-
24352426
if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
24362427
PY_LITTLE_ENDIAN, 0, 1) < 0) {
24372428
Py_DECREF(cookieLong);
@@ -2646,13 +2637,17 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
26462637
/* The strategy of seek() is to go back to the safe start point
26472638
* and replay the effect of read(chars_to_skip) from there.
26482639
*/
2649-
if (textiowrapper_parse_cookie(self, &cookie, cookieObj) < 0)
2640+
if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
26502641
goto fail;
26512642

26522643
/* Seek back to the safe start point. */
26532644
posobj = PyLong_FromOff_t(cookie.start_pos);
26542645
if (posobj == NULL)
26552646
goto fail;
2647+
2648+
// gh-143007 PyNumber_Long can call arbitrary code through __int__ which may detach the underlying buffer.
2649+
// So we need to re-check that the TextIOWrapper is still attached.
2650+
CHECK_ATTACHED(self);
26562651
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj);
26572652
Py_DECREF(posobj);
26582653
if (res == NULL)
@@ -2691,7 +2686,6 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
26912686
goto fail;
26922687
}
26932688
Py_XSETREF(self->snapshot, snapshot);
2694-
26952689
decoded = PyObject_CallMethodObjArgs(self->decoder, &_Py_ID(decode),
26962690
input_chunk, cookie.need_eof ? Py_True : Py_False, NULL);
26972691

0 commit comments

Comments
 (0)