]> granicus.if.org Git - python/commitdiff
don't segfault when trying to fdopen() a fd for a dir (closes #22259)
authorBenjamin Peterson <benjamin@python.org>
Sun, 24 Aug 2014 15:37:12 +0000 (10:37 -0500)
committerBenjamin Peterson <benjamin@python.org>
Sun, 24 Aug 2014 15:37:12 +0000 (10:37 -0500)
Patch from Brian Kearns.

Lib/test/test_posix.py
Misc/NEWS
Modules/posixmodule.c

index df122f712ca1c9288c7857ea53ee85995fc1d7f0..3f44aa37fa74a3a8a0282aa51ef6b03125325757 100644 (file)
@@ -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()')
index 61aab20984a5ae885e205f6e9bf814f0b9cccefb..2ad993b7832be4106c7bcc67a4e859e50b4ffe57 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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__.
index cd4672cbb159d5a5b2e1aa3330ab20fcf22f80c5..4d0c8fabd06f0e207eaf92306977d6e5097a0072 100644 (file)
@@ -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);