From: Antoine Pitrou Date: Sat, 4 Sep 2010 20:53:29 +0000 (+0000) Subject: Issue #8734: Avoid crash in msvcrt.get_osfhandle() when an invalid file X-Git-Tag: v3.2a2~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0049249d63a067aaffbbf3172a6e8cd8c956b643;p=thirdparty%2FPython%2Fcpython.git 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 63bc1d9781e1..4b48d41f6cae 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -309,6 +309,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 f413da2e5a61..5d3656066e24 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -100,6 +100,9 @@ Core and Builtins Extensions ---------- +- 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 e173fea8b033..166df036f2da 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);