Skip to content

Commit f9afb39

Browse files
committed
Fix crash when seeking a closed BufferedWriter
1 parent 228d955 commit f9afb39

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Lib/test/test_io/test_bufferedio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,17 @@ def closed(self):
983983
self.assertRaisesRegex(ValueError, "test", bufio.flush)
984984
self.assertRaisesRegex(ValueError, "test", bufio.close)
985985

986+
def test_gh_143375(self):
987+
bufio = self.tp(self.MockRawIO())
988+
989+
class EvilIndex:
990+
def __index__(self):
991+
bufio.close()
992+
return 0
993+
994+
with self.assertRaisesRegex(ValueError, "seek of closed file"):
995+
bufio.seek(EvilIndex())
996+
986997

987998
class PyBufferedWriterTest(BufferedWriterTest, PyTestCase):
988999
tp = pyio.BufferedWriter

Modules/_io/bufferedio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,10 @@ _io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)
13931393
if (target == -1 && PyErr_Occurred())
13941394
return NULL;
13951395

1396+
// PyNumber_AsOff_t calls user code via __index__, which
1397+
// could have closed the file.
1398+
CHECK_CLOSED(self, "seek of closed file")
1399+
13961400
/* SEEK_SET and SEEK_CUR are special because we could seek inside the
13971401
buffer. Other whence values must be managed without this optimization.
13981402
Some Operating Systems can provide additional values, like

0 commit comments

Comments
 (0)