On Fri, Jul 11, 2014 at 12:19:22PM +0200, Philip Guenther wrote:
> This should call warn() before unlink() or close() to guarantee that the
> correct errno value is reported.
...
> This and several other need to save errno and use errc(), ala:

Updated patch.  Updated mktemp.3 this time.


Index: bin/csh/dol.c
===================================================================
RCS file: /cvs/src/bin/csh/dol.c,v
retrieving revision 1.17
diff -u -p -d -r1.17 dol.c
--- bin/csh/dol.c       12 Aug 2010 02:00:27 -0000      1.17
+++ bin/csh/dol.c       11 Jul 2014 16:20:04 -0000
@@ -829,7 +829,8 @@ heredoc(Char *term)
 
     if (mkstemp(tmp) < 0)
        stderror(ERR_SYSTEM, tmp, strerror(errno));
-    (void) unlink(tmp);                /* 0 0 inode! */
+    else
+       (void) unlink(tmp);             /* 0 0 inode! */
     Dv[0] = term;
     Dv[1] = NULL;
     gflag = 0;
Index: lib/libc/stdio/mktemp.3
===================================================================
RCS file: /cvs/src/lib/libc/stdio/mktemp.3,v
retrieving revision 1.51
diff -u -p -d -r1.51 mktemp.3
--- lib/libc/stdio/mktemp.3     5 Jun 2013 03:39:23 -0000       1.51
+++ lib/libc/stdio/mktemp.3     11 Jul 2014 16:20:18 -0000
@@ -147,11 +147,11 @@ int fd;
 strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn));
 if ((fd = mkstemp(sfn)) == -1 ||
     (sfp = fdopen(fd, "w+")) == NULL) {
+       warn("%s", sfn);
        if (fd != -1) {
                unlink(sfn);
                close(fd);
        }
-       warn("%s", sfn);
        return (NULL);
 }
 return (sfp);
Index: sbin/disklabel/disklabel.c
===================================================================
RCS file: /cvs/src/sbin/disklabel/disklabel.c,v
retrieving revision 1.195
diff -u -p -d -r1.195 disklabel.c
--- sbin/disklabel/disklabel.c  5 May 2014 16:33:34 -0000       1.195
+++ sbin/disklabel/disklabel.c  11 Jul 2014 16:20:22 -0000
@@ -815,10 +815,13 @@ edit(struct disklabel *lp, int f)
        FILE *fp;
        u_int64_t total_sectors, starting_sector, ending_sector;
 
-       if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) {
-               if (fd != -1)
-                       close(fd);
+       if ((fd = mkstemp(tmpfil)) == -1 ||
+           (fp = fdopen(fd, "w")) == NULL) {
                warn("%s", tmpfil);
+               if (fd != -1) {
+                       unlink(tmpfil);
+                       close(fd);
+               }
                return (1);
        }
        display(fp, lp, 0, 1);
Index: sbin/scsi/scsi.c
===================================================================
RCS file: /cvs/src/sbin/scsi/scsi.c,v
retrieving revision 1.28
diff -u -p -d -r1.28 scsi.c
--- sbin/scsi/scsi.c    12 Nov 2013 04:59:02 -0000      1.28
+++ sbin/scsi/scsi.c    11 Jul 2014 16:20:22 -0000
@@ -571,8 +571,12 @@ edit_init(void)
        strlcpy(edit_name, "/var/tmp/scXXXXXXXX", sizeof edit_name);
        if ((fd = mkstemp(edit_name)) == -1)
                err(1, "mkstemp");
-       if ( (edit_file = fdopen(fd, "w+")) == 0)
-               err(1, "fdopen");
+       if ( (edit_file = fdopen(fd, "w+")) == 0) {
+               int saved_errno = errno;
+               unlink(edit_name);
+               close(fd);
+               errc(1, saved_errno, "fdopen");
+       }
        edit_opened = 1;
 
        atexit(edit_done);
Index: usr.bin/gzsig/sign.c
===================================================================
RCS file: /cvs/src/usr.bin/gzsig/sign.c,v
retrieving revision 1.13
diff -u -p -d -r1.13 sign.c
--- usr.bin/gzsig/sign.c        10 Mar 2013 10:36:57 -0000      1.13
+++ usr.bin/gzsig/sign.c        11 Jul 2014 16:20:25 -0000
@@ -281,6 +281,7 @@ sign(int argc, char *argv[])
                if ((fout = fdopen(fd, "w")) == NULL) {
                        fprintf(stderr, "Error opening %s: %s\n",
                            tmppath, strerror(errno));
+                       unlink(tmppath);
                        fclose(fin);
                        close(fd);
                        continue;
@@ -288,6 +289,7 @@ sign(int argc, char *argv[])
                if (copy_permissions(fileno(fin), fd) < 0) {
                        fprintf(stderr, "Error initializing %s: %s\n",
                            tmppath, strerror(errno));
+                       unlink(tmppath);
                        fclose(fin);
                        fclose(fout);
                        continue;
Index: usr.bin/htpasswd/htpasswd.c
===================================================================
RCS file: /cvs/src/usr.bin/htpasswd/htpasswd.c,v
retrieving revision 1.10
diff -u -p -d -r1.10 htpasswd.c
--- usr.bin/htpasswd/htpasswd.c 24 Mar 2014 20:33:01 -0000      1.10
+++ usr.bin/htpasswd/htpasswd.c 11 Jul 2014 16:20:25 -0000
@@ -164,8 +164,11 @@ main(int argc, char** argv)
                        if ((fd = mkstemp(tmpl)) == -1)
                                err(1, "mkstemp");
 
-                       if ((out = fdopen(fd, "w+")) == NULL)
-                               err(1, "cannot open tempfile");
+                       if ((out = fdopen(fd, "w+")) == NULL) {
+                               int saved_errno = errno;
+                               unlink(tmpl);
+                               errc(1, saved_errno, "cannot open tempfile");
+                       }
 
                        while ((linelen = getline(&line, &linesize, in))
                            != -1) {
Index: usr.bin/m4/eval.c
===================================================================
RCS file: /cvs/src/usr.bin/m4/eval.c,v
retrieving revision 1.72
diff -u -p -d -r1.72 eval.c
--- usr.bin/m4/eval.c   28 Apr 2014 12:34:11 -0000      1.72
+++ usr.bin/m4/eval.c   11 Jul 2014 16:20:25 -0000
@@ -818,8 +818,12 @@ dodiv(int n)
                char fname[] = _PATH_DIVNAME;
 
                if ((fd = mkstemp(fname)) < 0 || 
-                       (outfile[n] = fdopen(fd, "w+")) == NULL)
-                               err(1, "%s: cannot divert", fname);
+                       (outfile[n] = fdopen(fd, "w+")) == NULL) {
+                               int saved_errno = errno;
+                               if (fd != -1)
+                                       unlink(fname);
+                               errc(1, saved_errno, "%s: cannot divert", 
fname);
+               }
                if (unlink(fname) == -1)
                        err(1, "%s: cannot unlink", fname);
        }
Index: usr.bin/rwall/rwall.c
===================================================================
RCS file: /cvs/src/usr.bin/rwall/rwall.c,v
retrieving revision 1.12
diff -u -p -d -r1.12 rwall.c
--- usr.bin/rwall/rwall.c       27 Oct 2009 23:59:43 -0000      1.12
+++ usr.bin/rwall/rwall.c       11 Jul 2014 16:35:39 -0000
@@ -42,6 +42,7 @@
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <errno.h>
 #include <pwd.h>
 #include <unistd.h>
 #include <paths.h>
@@ -112,8 +113,15 @@ makemsg(char *fname)
        char *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[MAXPATHLEN];
 
        snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
-       if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
-               err(1, "can't open temporary file");
+       if ((fd = mkstemp(tmpname)) == -1 ||
+           (fp = fdopen(fd, "r+")) == NULL) {
+               int saved_errno = errno;
+               if (fd != -1) {
+                       unlink(tmpname);
+                       close(fd);
+               }
+               errc(1, saved_errno, "can't open temporary file");
+       }
        (void)unlink(tmpname);
 
        if (!(whom = getlogin()))
Index: usr.bin/sort/sort.c
===================================================================
RCS file: /cvs/src/usr.bin/sort/sort.c,v
retrieving revision 1.41
diff -u -p -d -r1.41 sort.c
--- usr.bin/sort/sort.c 13 Nov 2013 15:07:27 -0000      1.41
+++ usr.bin/sort/sort.c 11 Jul 2014 16:20:25 -0000
@@ -282,8 +282,14 @@ main(int argc, char *argv[])
                (void)umask(um);
                if ((outfd = mkstemp(toutpath)) == -1 ||
                    fchmod(outfd, DEFFILEMODE & ~um) == -1 ||
-                   (outfp = fdopen(outfd, "w")) == NULL)
-                       err(2, "%s", toutpath);
+                   (outfp = fdopen(outfd, "w")) == NULL) {
+                       int saved_errno = errno;
+                       if (outfd != -1) {
+                               unlink(toutpath);
+                               close(outfd);
+                       }
+                       errc(2, saved_errno, "%s", toutpath);
+               }
                outfile = toutpath;
 
                (void)atexit(cleanup);
Index: usr.bin/sort/tmp.c
===================================================================
RCS file: /cvs/src/usr.bin/sort/tmp.c,v
retrieving revision 1.9
diff -u -p -d -r1.9 tmp.c
--- usr.bin/sort/tmp.c  27 Oct 2009 23:59:43 -0000      1.9
+++ usr.bin/sort/tmp.c  11 Jul 2014 16:20:25 -0000
@@ -65,8 +65,11 @@ ftmp(void)
        (void)sigprocmask(SIG_BLOCK, &set, &oset);
        if ((fd = mkstemp(path)) < 0)
                err(2, "%s", path);
-       if (!(fp = fdopen(fd, "w+")))
-               err(2, "%s", path);
+       if (!(fp = fdopen(fd, "w+"))) {
+               int saved_errno = errno;
+               (void)unlink(path);
+               errc(2, saved_errno, "%s", path);
+       }
        (void)unlink(path);
 
        (void)sigprocmask(SIG_SETMASK, &oset, NULL);
Index: usr.bin/ssh/moduli.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/moduli.c,v
retrieving revision 1.28
diff -u -p -d -r1.28 moduli.c
--- usr.bin/ssh/moduli.c        24 Oct 2013 00:49:49 -0000      1.28
+++ usr.bin/ssh/moduli.c        11 Jul 2014 16:20:26 -0000
@@ -457,6 +457,7 @@ write_checkpoint(char *cpfile, u_int32_t
        }
        if ((fp = fdopen(r, "w")) == NULL) {
                logit("write_checkpoint: fdopen: %s", strerror(errno));
+               unlink(tmp);
                close(r);
                return;
        }
Index: usr.bin/tic/tic.c
===================================================================
RCS file: /cvs/src/usr.bin/tic/tic.c,v
retrieving revision 1.31
diff -u -p -d -r1.31 tic.c
--- usr.bin/tic/tic.c   28 Nov 2013 18:24:55 -0000      1.31
+++ usr.bin/tic/tic.c   11 Jul 2014 16:20:26 -0000
@@ -460,11 +460,18 @@ matches(char **needle, const char *hayst
 static FILE *
 open_tempfile(char *name)
 {
-    FILE *result = 0;
+    FILE *result;
 #if HAVE_MKSTEMP
-    int fd = mkstemp(name);
-    if (fd >= 0)
-       result = fdopen(fd, "w");
+    int fd;
+
+    if ((fd = mkstemp(name)) == -1 ||
+        (result = fdopen(fd, "w")) == NULL) {
+            if (fd != -1) {
+                unlink(name);
+                close(fd);
+            }
+            return (NULL);
+    }
 #else
     if (tmpnam(name) != 0)
        result = fopen(name, "w");
Index: usr.sbin/user/user.c
===================================================================
RCS file: /cvs/src/usr.sbin/user/user.c,v
retrieving revision 1.98
diff -u -p -d -r1.98 user.c
--- usr.sbin/user/user.c        23 Nov 2013 17:14:05 -0000      1.98
+++ usr.sbin/user/user.c        11 Jul 2014 16:20:28 -0000
@@ -721,6 +721,8 @@ setdefaults(user_t *up)
        }
        if ((fp = fdopen(fd, "w")) == NULL) {
                warn("can't fdopen `%s' for writing", CONFFILE);
+               unlink(template);
+               close(fd);
                return 0;
        }
        ret = 1;

Reply via email to