Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9300a59

Browse files
authored
gh-134744: Fix fcntl error handling (#134748)
Fix also reference leak on buffer overflow.
1 parent176b059 commit9300a59

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

‎Lib/test/test_fcntl.py

Lines changed: 12 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
fromtest.support.import_helperimportimport_module
14-
fromtest.support.os_helperimportTESTFN,unlink
14+
fromtest.support.os_helperimportTESTFN,unlink,make_bad_fd
1515

1616

1717
# Skip test if no fcntl module.
@@ -274,6 +274,17 @@ def test_fcntl_small_buffer(self):
274274
deftest_fcntl_large_buffer(self):
275275
self._check_fcntl_not_mutate_len(2024)
276276

277+
@unittest.skipUnless(hasattr(fcntl,'F_DUPFD'),'need fcntl.F_DUPFD')
278+
deftest_bad_fd(self):
279+
# gh-134744: Test error handling
280+
fd=make_bad_fd()
281+
withself.assertRaises(OSError):
282+
fcntl.fcntl(fd,fcntl.F_DUPFD,0)
283+
withself.assertRaises(OSError):
284+
fcntl.fcntl(fd,fcntl.F_DUPFD,b'\0'*10)
285+
withself.assertRaises(OSError):
286+
fcntl.fcntl(fd,fcntl.F_DUPFD,b'\0'*2048)
287+
277288

278289
if__name__=='__main__':
279290
unittest.main()

‎Lib/test/test_ioctl.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
importthreading
66
importunittest
77
fromtestimportsupport
8-
fromtest.supportimportthreading_helper
8+
fromtest.supportimportos_helper,threading_helper
99
fromtest.support.import_helperimportimport_module
1010
fcntl=import_module('fcntl')
1111
termios=import_module('termios')
@@ -201,6 +201,17 @@ def test_ioctl_set_window_size(self):
201201
new_winsz=struct.unpack("HHHH",result)
202202
self.assertEqual(new_winsz[:2], (20,40))
203203

204+
@unittest.skipUnless(hasattr(fcntl,'FICLONE'),'need fcntl.FICLONE')
205+
deftest_bad_fd(self):
206+
# gh-134744: Test error handling
207+
fd=os_helper.make_bad_fd()
208+
withself.assertRaises(OSError):
209+
fcntl.ioctl(fd,fcntl.FICLONE,fd)
210+
withself.assertRaises(OSError):
211+
fcntl.ioctl(fd,fcntl.FICLONE,b'\0'*10)
212+
withself.assertRaises(OSError):
213+
fcntl.ioctl(fd,fcntl.FICLONE,b'\0'*2048)
214+
204215

205216
if__name__=="__main__":
206217
unittest.main()

‎Modules/fcntlmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
128128
Py_END_ALLOW_THREADS
129129
}while (ret==-1&&errno==EINTR&& !(async_err=PyErr_CheckSignals()));
130130
if (ret<0) {
131-
if (async_err) {
131+
if (!async_err) {
132132
PyErr_SetFromErrno(PyExc_OSError);
133133
}
134134
Py_DECREF(result);
135135
returnNULL;
136136
}
137137
if (ptr[len]!='\0') {
138138
PyErr_SetString(PyExc_SystemError,"buffer overflow");
139+
Py_DECREF(result);
139140
returnNULL;
140141
}
141142
returnresult;
@@ -310,14 +311,15 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
310311
Py_END_ALLOW_THREADS
311312
}while (ret==-1&&errno==EINTR&& !(async_err=PyErr_CheckSignals()));
312313
if (ret<0) {
313-
if (async_err) {
314+
if (!async_err) {
314315
PyErr_SetFromErrno(PyExc_OSError);
315316
}
316317
Py_DECREF(result);
317318
returnNULL;
318319
}
319320
if (ptr[len]!='\0') {
320321
PyErr_SetString(PyExc_SystemError,"buffer overflow");
322+
Py_DECREF(result);
321323
returnNULL;
322324
}
323325
returnresult;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp