FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143007: fix crash .TextIOWrapper.seek with a custom __int__ method detaches by yihong0618 · Pull Request #143043 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions Lib/test/test_io/test_textio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,23 @@ def closed(self):
wrapper = self.TextIOWrapper(raw)
wrapper.close() # should not crash

def test_issue143007(self):
# gh-143007: Null pointer dereference in TextIOWrapper.seek
# via re-entrant __int__
wrapper = self.TextIOWrapper(self.BytesIO(b"x"))

class Cookie(int):
def __new__(cls, wrapper):
obj = int.__new__(cls, 0)
obj.wrapper = wrapper
return obj
def __int__(self):
self.wrapper.detach()
return 0

with self.assertRaises(ValueError):
wrapper.seek(Cookie(wrapper), 0) # should not crash


class PyTextIOWrapperTest(TextIOWrapperTest, PyTestCase):
shutdown_error = "LookupError: unknown encoding: ascii"
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in :meth:`io.TextIOWrapper.seek` when a custom cookie's
``__int__`` method detaches the underlying buffer.
4 changes: 4 additions & 0 deletions Modules/_io/textio.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,10 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
posobj = PyLong_FromOff_t(cookie.start_pos);
if (posobj == NULL)
goto fail;

// gh-143007 PyNumber_Long can call arbitrary code through __int__ which may detach the underlying buffer.
// So we need to re-check that the TextIOWrapper is still attached.
CHECK_ATTACHED(self);
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj);
Py_DECREF(posobj);
if (res == NULL)
Expand Down
Loading

Back | FazBrowse Home | New Git URL