[Python-checkins] python/dist/src/Objects fileobject.c, 2.193, 2.194

montanaro@users.sourceforge.net montanaro at users.sourceforge.net
Fri May 20 05:07:38 CEST 2005


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22227/Objects

Modified Files:
	fileobject.c 
Log Message:
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
278. Closes bug 967182.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.193
retrieving revision 2.194
diff -u -d -r2.193 -r2.194
--- fileobject.c	7 Nov 2004 14:15:28 -0000	2.193
+++ fileobject.c	20 May 2005 03:07:05 -0000	2.194
@@ -128,6 +128,54 @@
 	return (PyObject *) f;
 }
 
+/* check for known incorrect mode strings - problem is, platforms are
+   free to accept any mode characters they like and are supposed to
+   ignore stuff they don't understand... write or append mode with
+   universal newline support is expressly forbidden by PEP 278. */
+/* zero return is kewl - one is un-kewl */
+static int
+check_the_mode(char *mode)
+{
+	unsigned int len = strlen(mode);
+
+	switch (len) {
+	case 0:
+		PyErr_SetString(PyExc_ValueError, "empty mode string");
+		return 1;
+
+	/* reject wU, aU */
+	case 2:
+		switch (mode[0]) {
+		case 'w':
+		case 'a':
+			if (mode[1] == 'U') {
+				PyErr_SetString(PyExc_ValueError,
+						"invalid mode string");
+				return 1;
+			}
+			break;
+		}
+		break;
+
+	/* reject w+U, a+U, wU+, aU+ */
+	case 3:
+		switch (mode[0]) {
+		case 'w':
+		case 'a':
+			if ((mode[1] == '+' && mode[2] == 'U') ||
+			    (mode[1] == 'U' && mode[2] == '+')) {
+				PyErr_SetString(PyExc_ValueError,
+						"invalid mode string");
+				return 1;
+			}
+			break;
+		}
+		break;
+	}
+
+	return 0;
+}
+
 static PyObject *
 open_the_file(PyFileObject *f, char *name, char *mode)
 {
@@ -142,6 +190,9 @@
 	assert(mode != NULL);
 	assert(f->f_fp == NULL);
 
+	if (check_the_mode(mode))
+		return NULL;
+
 	/* rexec.py can't stop a user from getting the file() constructor --
 	   all they have to do is get *any* file object f, and then do
 	   type(f).  Here we prevent them from doing damage with it. */



More information about the Python-checkins mailing list