[Python-checkins] r43501 - python/trunk/Modules/posixmodule.c

georg.brandl python-checkins at python.org
Fri Mar 31 22:00:12 CEST 2006


Author: georg.brandl
Date: Fri Mar 31 22:00:11 2006
New Revision: 43501

Modified:
   python/trunk/Modules/posixmodule.c
Log:
bug #1461855: make os.fdopen() add the O_APPEND flag if using "a" mode.
glibc, for example, does this already on its own, but it seems that
the solaris libc doesn't. This leads to Python code being able to over-
write file contents even though having specified "a" mode.


Modified: python/trunk/Modules/posixmodule.c
==============================================================================
--- python/trunk/Modules/posixmodule.c	(original)
+++ python/trunk/Modules/posixmodule.c	Fri Mar 31 22:00:11 2006
@@ -5768,9 +5768,20 @@
 			     "invalid file mode '%s'", mode);
 		return NULL;
 	}
-
 	Py_BEGIN_ALLOW_THREADS
-	fp = fdopen(fd, mode);
+	if (mode[0] == 'a') {
+		/* try to make sure the O_APPEND flag is set */
+		int flags;
+		flags = fcntl(fd, F_GETFL);
+		if (flags != -1)
+			fcntl(fd, F_SETFL, flags | O_APPEND);
+		fp = fdopen(fd, mode);
+		if (fp == NULL)
+			/* restore old mode if fdopen failed */
+			fcntl(fd, F_SETFL, flags);
+	} else {
+		fp = fdopen(fd, mode);
+	}
 	Py_END_ALLOW_THREADS
 	if (fp == NULL)
 		return posix_error();


More information about the Python-checkins mailing list