From: Thomas Roessler Date: Sun, 28 Feb 1999 08:05:54 +0000 (+0000) Subject: Fix a $TMPDIR race condition. X-Git-Tag: mutt-0-96-1-rel~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec42e01f8fe4623178a747c551dcfb3ebc5d397c;p=mutt Fix a $TMPDIR race condition. --- diff --git a/attach.c b/attach.c index 81b9c561..f4a0d3b0 100644 --- a/attach.c +++ b/attach.c @@ -712,9 +712,11 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr) memset (&s, 0, sizeof (s)); if (flags == M_SAVE_APPEND) - s.fpout = safe_fopen (path, "a"); - else + s.fpout = fopen (path, "a"); + else if (flags == M_SAVE_OVERWRITE) s.fpout = fopen (path, "w"); + else + s.fpout = safe_fopen (path, "w"); if (s.fpout == NULL) { mutt_perror ("fopen"); @@ -776,9 +778,12 @@ int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path, s.flags = (displaying ? M_DISPLAY : 0); if (flags == M_SAVE_APPEND) - s.fpout = safe_fopen (path, "a"); - else + s.fpout = fopen (path, "a"); + else if (flags == M_SAVE_OVERWRITE) s.fpout = fopen (path, "w"); + else + s.fpout = safe_fopen (path, "w"); + if (s.fpout == NULL) { perror ("fopen"); diff --git a/lib.c b/lib.c index cd21ba76..047a5e45 100644 --- a/lib.c +++ b/lib.c @@ -807,8 +807,10 @@ int mutt_check_overwrite (const char *attname, const char *path, case 2: /* append */ *append = M_SAVE_APPEND; + break; case 1: /* overwrite */ - ; + *append = M_SAVE_OVERWRITE; + break; } } return 0; diff --git a/mutt.h b/mutt.h index 46b6667a..a4bd8127 100644 --- a/mutt.h +++ b/mutt.h @@ -229,7 +229,8 @@ enum M_NEW_SOCKET, /* Options for mutt_save_attachment */ - M_SAVE_APPEND + M_SAVE_APPEND, + M_SAVE_OVERWRITE }; /* possible arguments to set_quadoption() */ diff --git a/rfc1524.c b/rfc1524.c index 4914cb59..6f803dbc 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -29,11 +29,14 @@ #include "mutt.h" #include "rfc1524.h" -#include +#include #include -#include +#include + +#include #include -#include +#include +#include /* The command semantics include the following: * %s is the filename that contains the mail body data @@ -429,6 +432,7 @@ void mutt_adv_mktemp (char *s, size_t l) char tmp[_POSIX_PATH_MAX]; char *period; size_t sl; + struct stat sb; strfcpy (buf, NONULL (Tempdir), sizeof (buf)); mutt_expand_path (buf, sizeof (buf)); @@ -441,7 +445,7 @@ void mutt_adv_mktemp (char *s, size_t l) { strfcpy (tmp, s, sizeof (tmp)); snprintf (s, l, "%s/%s", buf, tmp); - if (access (s, F_OK) != 0) + if (lstat (s, &sb) == -1 && errno == ENOENT) return; if ((period = strrchr (tmp, '.')) != NULL) *period = 0; @@ -594,6 +598,11 @@ int rfc1524_expand_filename (char *nametemplate, * This function returns 0 on successful move, 1 on old file doesn't exist, * 2 on new file already exists, and 3 on other failure. */ + +/* note on access(2) use: No dangling symlink problems here due to + * safe_fopen(). + */ + int mutt_rename_file (char *oldfile, char *newfile) { FILE *ofp, *nfp;