summaryrefslogtreecommitdiff
path: root/Modules/_io/fileio.c
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2017-02-04 15:05:40 -0800
committerSteve Dower <steve.dower@microsoft.com>2017-02-04 15:05:40 -0800
commitb2fa705fd3887c326e811c418469c784353027f4 (patch)
treeb3428f73de91453edbfd4df1a5d4a212d182eb44 /Modules/_io/fileio.c
parent134e58fd3aaa2e91390041e143f3f0a21a60142b (diff)
parentb53654b6dbfce8318a7d4d1cdaddca7a7fec194b (diff)
downloadcpython-b2fa705fd3887c326e811c418469c784353027f4.tar.gz
Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.
Diffstat (limited to 'Modules/_io/fileio.c')
-rw-r--r--Modules/_io/fileio.c49
1 files changed, 19 insertions, 30 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 919cf502dc..6854a44e2d 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -9,6 +9,9 @@
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
@@ -92,8 +95,7 @@ fileio_dealloc_warn(fileio *self, PyObject *source)
if (self->fd >= 0 && self->closefd) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
- if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
- "unclosed file %R", source)) {
+ if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning))
PyErr_WriteUnraisable((PyObject *) self);
@@ -118,18 +120,13 @@ internal_close(fileio *self)
int fd = self->fd;
self->fd = -1;
/* fd is accessible and someone else may have closed it */
- if (_PyVerify_fd(fd)) {
- Py_BEGIN_ALLOW_THREADS
- _Py_BEGIN_SUPPRESS_IPH
- err = close(fd);
- if (err < 0)
- save_errno = errno;
- _Py_END_SUPPRESS_IPH
- Py_END_ALLOW_THREADS
- } else {
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+ err = close(fd);
+ if (err < 0)
save_errno = errno;
- err = -1;
- }
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
}
if (err < 0) {
errno = save_errno;
@@ -546,7 +543,7 @@ err_closed(void)
}
static PyObject *
-err_mode(char *action)
+err_mode(const char *action)
{
_PyIO_State *state = IO_STATE();
if (state != NULL)
@@ -701,8 +698,6 @@ _io_FileIO_readall_impl(fileio *self)
if (self->fd < 0)
return err_closed();
- if (!_PyVerify_fd(self->fd))
- return PyErr_SetFromErrno(PyExc_IOError);
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
@@ -915,18 +910,15 @@ portable_lseek(int fd, PyObject *posobj, int whence)
return NULL;
}
- if (_PyVerify_fd(fd)) {
- Py_BEGIN_ALLOW_THREADS
- _Py_BEGIN_SUPPRESS_IPH
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
- res = _lseeki64(fd, pos, whence);
+ res = _lseeki64(fd, pos, whence);
#else
- res = lseek(fd, pos, whence);
+ res = lseek(fd, pos, whence);
#endif
- _Py_END_SUPPRESS_IPH
- Py_END_ALLOW_THREADS
- } else
- res = -1;
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
if (res < 0)
return PyErr_SetFromErrno(PyExc_IOError);
@@ -1049,7 +1041,7 @@ _io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
}
#endif /* HAVE_FTRUNCATE */
-static char *
+static const char *
mode_string(fileio *self)
{
if (self->created) {
@@ -1117,10 +1109,7 @@ _io_FileIO_isatty_impl(fileio *self)
return err_closed();
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
- if (_PyVerify_fd(self->fd))
- res = isatty(self->fd);
- else
- res = 0;
+ res = isatty(self->fd);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
return PyBool_FromLong(res);