[Python-checkins] CVS: python/dist/src/Objects fileobject.c,2.147,2.148

Neil Schemenauer nascheme@users.sourceforge.net
Fri, 22 Mar 2002 12:39:00 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv7920/Objects

Modified Files:
	fileobject.c 
Log Message:
Disallow open()ing of directories.  Closes SF bug 487277.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.147
retrieving revision 2.148
diff -C2 -d -r2.147 -r2.148
*** fileobject.c	15 Mar 2002 17:42:16 -0000	2.147
--- fileobject.c	22 Mar 2002 20:38:57 -0000	2.148
***************
*** 57,60 ****
--- 57,86 ----
  }
  
+ /* 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 *
***************
*** 78,81 ****
--- 104,108 ----
  		return NULL;
  	f->f_fp = fp;
+         f = dircheck(f);
  	return (PyObject *) f;
  }
***************
*** 131,134 ****
--- 158,162 ----
  		f = NULL;
  	}
+         f = dircheck(f);
  	return (PyObject *)f;
  }