From: Antoine Pitrou <solipsis@pitrou.net>
Date: Sat, 4 Sep 2010 21:02:41 +0000 (+0000)
Subject: Merged revisions 84506 via svnmerge from
X-Git-Tag: v3.1.3rc1~275
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0bb502dcac9c687201aa58632e3dcbee1ac1168c;p=python

Merged revisions 84506 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r84506 | antoine.pitrou | 2010-09-04 22:53:29 +0200 (sam., 04 sept. 2010) | 5 lines

  Issue #8734: Avoid crash in msvcrt.get_osfhandle() when an invalid file
  descriptor is provided.  Patch by Pascal Chambon.
........
---

diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index f89c542d19..2f299dc32e 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -313,6 +313,9 @@ class OtherFileTests(unittest.TestCase):
     def testInvalidFd(self):
         self.assertRaises(ValueError, _FileIO, -10)
         self.assertRaises(OSError, _FileIO, make_bad_fd())
+        if sys.platform == 'win32':
+            import msvcrt
+            self.assertRaises(IOError, msvcrt.get_osfhandle, make_bad_fd())
 
     def testBadModeArgument(self):
         # verify that we get a sensible error message for bad mode argument
diff --git a/Misc/NEWS b/Misc/NEWS
index 871396baea..2ff4ff44eb 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -477,6 +477,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #8734: Avoid crash in msvcrt.get_osfhandle() when an invalid file
+  descriptor is provided.  Patch by Pascal Chambon.
+
 - Issue #7736: Release the GIL around calls to opendir() and closedir()
   in the posix module.  Patch by Marcin Bachry.
 
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index 9a86d5e438..37631a7792 100755
--- a/PC/msvcrtmodule.c
+++ b/PC/msvcrtmodule.c
@@ -143,6 +143,9 @@ msvcrt_get_osfhandle(PyObject *self, PyObject *args)
     if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
         return NULL;
 
+    if (!_PyVerify_fd(fd))
+        return PyErr_SetFromErrno(PyExc_IOError);
+
     handle = _get_osfhandle(fd);
     if (handle == -1)
         return PyErr_SetFromErrno(PyExc_IOError);