summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-03-31 20:00:11 (GMT)
committerGeorg Brandl <georg@python.org>2006-03-31 20:00:11 (GMT)
commit54a188aed8cc8333927a679bb00eda4e25aa0cba (patch)
treed7e8d7a2b743c63927216d8060bae4625734b134 /Modules/posixmodule.c
parentdcdfd22bb411ebf0d58bba135455c4abf2bc9ce1 (diff)
downloadcpython-54a188aed8cc8333927a679bb00eda4e25aa0cba.zip
cpython-54a188aed8cc8333927a679bb00eda4e25aa0cba.tar.gz
cpython-54a188aed8cc8333927a679bb00eda4e25aa0cba.tar.bz2
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.
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index abf69a9..631833f 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5768,9 +5768,20 @@ posix_fdopen(PyObject *self, PyObject *args)
"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();