Kaydet (Commit) e3737540 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

don't segfault when trying to fdopen() a fd for a dir (closes #22259)

Patch from Brian Kearns.
üst d3ea0653
......@@ -194,6 +194,18 @@ class PosixTester(unittest.TestCase):
self.fdopen_helper('r')
self.fdopen_helper('r', 100)
@unittest.skipUnless(hasattr(posix, 'fdopen'),
'test needs posix.fdopen()')
def test_fdopen_directory(self):
try:
fd = os.open('.', os.O_RDONLY)
except OSError as e:
self.assertEqual(e.errno, errno.EACCES)
self.skipTest("system cannot open directories")
with self.assertRaises(IOError) as cm:
os.fdopen(fd, 'r')
self.assertEqual(cm.exception.errno, errno.EISDIR)
@unittest.skipUnless(hasattr(posix, 'fdopen') and
not sys.platform.startswith("sunos"),
'test needs posix.fdopen()')
......
......@@ -19,6 +19,9 @@ Core and Builtins
Library
-------
- Issue #22259: Fix segfault when attempting to fopen a file descriptor
corresponding to a directory.
- Issue #22236: Fixed Tkinter images copying operations in NoDefaultRoot mode.
- Issue #22191: Fixed warnings.__all__.
......
......@@ -6861,7 +6861,7 @@ posix_fdopen(PyObject *self, PyObject *args)
if (fstat(fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
PyMem_FREE(mode);
msg = strerror(EISDIR);
exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
EISDIR, msg, "<fdopen>");
if (exc) {
PyErr_SetObject(PyExc_IOError, exc);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment