gh-134744: Fix fcntl error handling (#134748)

Fix also reference leak on buffer overflow.
This commit is contained in:
Victor Stinner 2025-05-27 15:09:46 +02:00 committed by GitHub
parent 176b059fa0
commit 9300a596d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 4 deletions

View file

@ -11,7 +11,7 @@
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.
@ -274,6 +274,17 @@ def test_fcntl_small_buffer(self):
def test_fcntl_large_buffer(self):
self._check_fcntl_not_mutate_len(2024)
@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' * 10)
with self.assertRaises(OSError):
fcntl.fcntl(fd, fcntl.F_DUPFD, b'\0' * 2048)
if __name__ == '__main__':
unittest.main()

View file

@ -5,7 +5,7 @@
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')
@ -201,6 +201,17 @@ def test_ioctl_set_window_size(self):
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' * 10)
with self.assertRaises(OSError):
fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 2048)
if __name__ == "__main__":
unittest.main()