]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-134744: Fix fcntl error handling (#134748) (#134795)
authorVictor Stinner <vstinner@python.org>
Tue, 27 May 2025 16:13:20 +0000 (18:13 +0200)
committerGitHub <noreply@github.com>
Tue, 27 May 2025 16:13:20 +0000 (16:13 +0000)
gh-134744: Fix fcntl error handling (#134748)

Fix also reference leak on buffer overflow.

(cherry picked from commit 9300a596d37d058e6e58d00a2ad70617c863a3de)

Lib/test/test_fcntl.py
Lib/test/test_ioctl.py

index b84c98ef3a2972d41f02d74729ed49968d806c1d..0570257c5230d5102d6a1bded9ff4af34c18b9bc 100644 (file)
@@ -11,7 +11,7 @@ from test.support import (
     cpython_only, get_pagesize, is_apple, requires_subprocess, verbose
 )
 from test.support.import_helper import import_module
-from test.support.os_helper import TESTFN, unlink
+from test.support.os_helper import TESTFN, unlink, make_bad_fd
 
 
 # Skip test if no fcntl module.
@@ -228,6 +228,15 @@ class TestFcntl(unittest.TestCase):
             os.close(test_pipe_r)
             os.close(test_pipe_w)
 
+    @unittest.skipUnless(hasattr(fcntl, 'F_DUPFD'), 'need fcntl.F_DUPFD')
+    def test_bad_fd(self):
+        # gh-134744: Test error handling
+        fd = make_bad_fd()
+        with self.assertRaises(OSError):
+            fcntl.fcntl(fd, fcntl.F_DUPFD, 0)
+        with self.assertRaises(OSError):
+            fcntl.fcntl(fd, fcntl.F_DUPFD, b'\0' * 1024)
+
 
 if __name__ == '__main__':
     unittest.main()
index 7a986048bdac2ac813193c585df56c24e4460e69..01e1bfb42dd98640440e2a9fbe6f9958cc29b727 100644 (file)
@@ -5,7 +5,7 @@ import sys
 import threading
 import unittest
 from test import support
-from test.support import threading_helper
+from test.support import os_helper, threading_helper
 from test.support.import_helper import import_module
 fcntl = import_module('fcntl')
 termios = import_module('termios')
@@ -202,6 +202,15 @@ class IoctlTestsPty(unittest.TestCase):
         new_winsz = struct.unpack("HHHH", result)
         self.assertEqual(new_winsz[:2], (20, 40))
 
+    @unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE')
+    def test_bad_fd(self):
+        # gh-134744: Test error handling
+        fd = os_helper.make_bad_fd()
+        with self.assertRaises(OSError):
+            fcntl.ioctl(fd, fcntl.FICLONE, fd)
+        with self.assertRaises(OSError):
+            fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 1024)
+
 
 if __name__ == "__main__":
     unittest.main()