From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 28 May 2025 10:54:53 +0000 (+0200) Subject: [3.13] gh-134744: Fix fcntl error handling (GH-134748) (GH-134795) (#134798) X-Git-Tag: v3.13.4~30 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=741040236e05a3789e5d9756530b5ff92e0bc52d;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-134744: Fix fcntl error handling (GH-134748) (GH-134795) (#134798) [3.14] gh-134744: Fix fcntl error handling (GH-134748) (GH-134795) gh-134744: Fix fcntl error handling (GH-134748) Fix also reference leak on buffer overflow. (cherry picked from commit 8a6a6f39c869a6601babc31592e156ce22478a47) (cherry picked from commit 9300a596d37d058e6e58d00a2ad70617c863a3de) Co-authored-by: Victor Stinner --- diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index b84c98ef3a29..0570257c5230 100644 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -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() diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py index 980a20aadc0b..9531282d6bab 100644 --- a/Lib/test/test_ioctl.py +++ b/Lib/test/test_ioctl.py @@ -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') @@ -208,6 +208,15 @@ class IoctlTestsPty(unittest.TestCase): new_winsz = fcntl.ioctl(self.master_fd, set_winsz_opcode_pos, our_winsz) new_winsz = fcntl.ioctl(self.master_fd, set_winsz_opcode_maybe_neg, our_winsz) + @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()