summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>2002-03-22 20:38:57 (GMT)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>2002-03-22 20:38:57 (GMT)
commit398b9f6d6d75628efe03fb4d8dc1aa2df6c35a67 (patch)
tree4d85786da1088075f900d214b5904b5542652062 /Objects
parent07c57d4e60083981a3ddcd8ca56b85dbc62c4dbb (diff)
downloadcpython-398b9f6d6d75628efe03fb4d8dc1aa2df6c35a67.zip
cpython-398b9f6d6d75628efe03fb4d8dc1aa2df6c35a67.tar.gz
cpython-398b9f6d6d75628efe03fb4d8dc1aa2df6c35a67.tar.bz2
Disallow open()ing of directories. Closes SF bug 487277.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/fileobject.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index c8fb214..924ddcf 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -56,6 +56,32 @@ PyFile_Name(PyObject *f)
return ((PyFileObject *)f)->f_name;
}
+/* On Unix, fopen will succeed for directories.
+ In Python, there should be no file objects referring to
+ directories, so we need a check. */
+
+static PyFileObject*
+dircheck(PyFileObject* f)
+{
+#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
+ struct stat buf;
+ if (f->f_fp == NULL)
+ return f;
+ if (fstat(fileno(f->f_fp), &buf) == 0 &&
+ S_ISDIR(buf.st_mode)) {
+#ifdef HAVE_STRERROR
+ char *msg = strerror(EISDIR);
+#else
+ char *msg = "Is a directory";
+#endif
+ PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)", EISDIR, msg);
+ PyErr_SetObject(PyExc_IOError, exc);
+ return NULL;
+ }
+#endif
+ return f;
+}
+
static PyObject *
fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
@@ -77,6 +103,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
if (f->f_name == NULL || f->f_mode == NULL)
return NULL;
f->f_fp = fp;
+ f = dircheck(f);
return (PyObject *) f;
}
@@ -130,6 +157,7 @@ open_the_file(PyFileObject *f, char *name, char *mode)
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
f = NULL;
}
+ f = dircheck(f);
return (PyObject *)f;
}