]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bug #1461855: make os.fdopen() add the O_APPEND flag if using "a" mode.
authorGeorg Brandl <georg@python.org>
Fri, 31 Mar 2006 20:00:15 +0000 (20:00 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 31 Mar 2006 20:00:15 +0000 (20:00 +0000)
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.
 (backport from rev. 43501)

Modules/posixmodule.c

index a65fb4b162ea15c5e4a3898bdf9bfe5a2a76fec2..b4cab361ff786eae4ae3b4c1cdd79442ae957717 100644 (file)
@@ -5458,9 +5458,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();