summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-01-19 00:08:08 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-01-19 00:08:08 (GMT)
commit5848d1ff35f8606b1678ff8cc29011b8fd4ef486 (patch)
treed1607015545ad0de2967543397e0e33d9ee5d81e /Modules
parentb6e112bd952c2023b95212364ed07ad9c235da41 (diff)
downloadcpython-5848d1ff35f8606b1678ff8cc29011b8fd4ef486.zip
cpython-5848d1ff35f8606b1678ff8cc29011b8fd4ef486.tar.gz
cpython-5848d1ff35f8606b1678ff8cc29011b8fd4ef486.tar.bz2
raise an OSError for invalid fds #4991
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_fileio.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 2dc3d74..0f09ecd 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -119,6 +119,24 @@ dircheck(PyFileIOObject* self, char *name)
return 0;
}
+static int
+check_fd(int fd)
+{
+#if defined(HAVE_FSTAT)
+ struct stat buf;
+ if (fstat(fd, &buf) < 0 && errno == EBADF) {
+ PyObject *exc;
+ char *msg = strerror(EBADF);
+ exc = PyObject_CallFunction(PyExc_OSError, "(is)",
+ EBADF, msg);
+ PyErr_SetObject(PyExc_OSError, exc);
+ Py_XDECREF(exc);
+ return -1;
+ }
+#endif
+ return 0;
+}
+
static int
fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
@@ -151,6 +169,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
"Negative filedescriptor");
return -1;
}
+ if (check_fd(fd))
+ return -1;
}
else {
PyErr_Clear();