mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
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.
This commit is contained in:
parent
dcdfd22bb4
commit
54a188aed8
1 changed files with 13 additions and 2 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue