]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143689: Fix BufferedReader.read1 leaving object in reentrant state on error (...
authorYongtao Huang <yongtaoh2022@gmail.com>
Mon, 19 Jan 2026 14:09:30 +0000 (22:09 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Jan 2026 14:09:30 +0000 (15:09 +0100)
BufferedReader.read1() could leave the buffered object in a
reentrant (locked) state when an exception was raised while
allocating the output buffer.

This change ensures the internal buffered lock is always released
on error, keeping the object in a consistent state after failures.

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Lib/test/test_io/test_bufferedio.py
Misc/NEWS.d/next/Library/2026-01-11-14-14-19.gh-issue-143689.fzHJ2W.rst [new file with mode: 0644]
Modules/_io/bufferedio.c

index 3278665bdc9dd3d7583a395e039beda10fc7ca06..e83dd0d4e28d0068950f7ea2a711f187ec236e24 100644 (file)
@@ -10,7 +10,7 @@ import weakref
 from collections import deque, UserList
 from itertools import cycle, count
 from test import support
-from test.support import os_helper, threading_helper
+from test.support import check_sanitizer, os_helper, threading_helper
 from .utils import byteslike, CTestCase, PyTestCase
 
 
@@ -623,6 +623,25 @@ class CBufferedReaderTest(BufferedReaderTest, SizeofTest, CTestCase):
             bufio.readline()
         self.assertIsInstance(cm.exception.__cause__, TypeError)
 
+    @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
+    @unittest.skipIf(check_sanitizer(thread=True),
+                     'ThreadSanitizer aborts on huge allocations (exit code 66).')
+    def test_read1_error_does_not_cause_reentrant_failure(self):
+        self.addCleanup(os_helper.unlink, os_helper.TESTFN)
+        with self.open(os_helper.TESTFN, "wb") as f:
+            f.write(b"hello")
+
+        with self.open(os_helper.TESTFN, "rb", buffering=0) as raw:
+            bufio = self.tp(raw, buffer_size=8)
+            # To request a size that is far too huge to ever be satisfied,
+            # so that the internal buffer allocation reliably fails with MemoryError.
+            huge = sys.maxsize // 2 + 1
+            with self.assertRaises(MemoryError):
+                bufio.read1(huge)
+
+            # Used to crash before gh-143689:
+            self.assertEqual(bufio.read1(1), b"h")
+
 
 class PyBufferedReaderTest(BufferedReaderTest, PyTestCase):
     tp = pyio.BufferedReader
diff --git a/Misc/NEWS.d/next/Library/2026-01-11-14-14-19.gh-issue-143689.fzHJ2W.rst b/Misc/NEWS.d/next/Library/2026-01-11-14-14-19.gh-issue-143689.fzHJ2W.rst
new file mode 100644 (file)
index 0000000..a423b1b
--- /dev/null
@@ -0,0 +1 @@
+Fix :meth:`io.BufferedReader.read1` state cleanup on buffer allocation failure.
index 4602f2b42a601779a13a58994e88e0c2b375cd0f..6d779abd89ca84b4ae27ca6c84798afe47493ee3 100644 (file)
@@ -1073,6 +1073,7 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
 
     PyBytesWriter *writer = PyBytesWriter_Create(n);
     if (writer == NULL) {
+        LEAVE_BUFFERED(self)
         return NULL;
     }