diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 930b6543b614..2760b733c514 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -312,6 +312,9 @@ def testBytesOpen(self): 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 8ac6168da5c0..b205fa5aff86 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -228,6 +228,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 68b7d2432e64..057900d98057 100755 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -141,6 +141,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);