Skip to content

Commit 6f5c111

Browse files
committed
Merge branch 'hy/close_issue_143007' of https://github.com/yihong0618/cpython into hy/close_issue_143007
2 parents af070ea + f7cc7c4 commit 6f5c111

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Modules/_io/textio.c

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

24182418
static int
2419-
textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
2419+
textiowrapper_parse_cookie(textio *self, 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+
24262435
if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
24272436
PY_LITTLE_ENDIAN, 0, 1) < 0) {
24282437
Py_DECREF(cookieLong);
@@ -2637,7 +2646,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
26372646
/* The strategy of seek() is to go back to the safe start point
26382647
* and replay the effect of read(chars_to_skip) from there.
26392648
*/
2640-
if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
2649+
if (textiowrapper_parse_cookie(self, &cookie, cookieObj) < 0)
26412650
goto fail;
26422651

26432652
/* Seek back to the safe start point. */

0 commit comments

Comments
 (0)