Skip to content

Commit 8a6a6f3

Browse files
authored
[3.14] gh-134744: Fix fcntl error handling (#134748) (#134795)
gh-134744: Fix fcntl error handling (#134748) Fix also reference leak on buffer overflow. (cherry picked from commit 9300a59)
1 parent 6493395 commit 8a6a6f3

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Lib/test/test_fcntl.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
cpython_only, get_pagesize, is_apple, requires_subprocess, verbose
1212
)
1313
from test.support.import_helper import import_module
14-
from test.support.os_helper import TESTFN, unlink
14+
from test.support.os_helper import TESTFN, unlink, make_bad_fd
1515

1616

1717
# Skip test if no fcntl module.
@@ -228,6 +228,15 @@ def test_fcntl_f_pipesize(self):
228228
os.close(test_pipe_r)
229229
os.close(test_pipe_w)
230230

231+
@unittest.skipUnless(hasattr(fcntl, 'F_DUPFD'), 'need fcntl.F_DUPFD')
232+
def test_bad_fd(self):
233+
# gh-134744: Test error handling
234+
fd = make_bad_fd()
235+
with self.assertRaises(OSError):
236+
fcntl.fcntl(fd, fcntl.F_DUPFD, 0)
237+
with self.assertRaises(OSError):
238+
fcntl.fcntl(fd, fcntl.F_DUPFD, b'\0' * 1024)
239+
231240

232241
if __name__ == '__main__':
233242
unittest.main()

Lib/test/test_ioctl.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import threading
66
import unittest
77
from test import support
8-
from test.support import threading_helper
8+
from test.support import os_helper, threading_helper
99
from test.support.import_helper import import_module
1010
fcntl = import_module('fcntl')
1111
termios = import_module('termios')
@@ -202,6 +202,15 @@ def test_ioctl_set_window_size(self):
202202
new_winsz = struct.unpack("HHHH", result)
203203
self.assertEqual(new_winsz[:2], (20, 40))
204204

205+
@unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE')
206+
def test_bad_fd(self):
207+
# gh-134744: Test error handling
208+
fd = os_helper.make_bad_fd()
209+
with self.assertRaises(OSError):
210+
fcntl.ioctl(fd, fcntl.FICLONE, fd)
211+
with self.assertRaises(OSError):
212+
fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 1024)
213+
205214

206215
if __name__ == "__main__":
207216
unittest.main()

0 commit comments

Comments
 (0)