On Fri, Jul 11, 2014 at 11:41 AM, Doug Hogan <[email protected]> wrote:

> 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 09:13:43 -0000
> @@ -815,9 +815,12 @@ 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)
> +       if ((fd = mkstemp(tmpfil)) == -1 ||
> +           (fp = fdopen(fd, "w")) == NULL) {
> +               if (fd != -1) {
> +                       unlink(tmpfil);
>                         close(fd);
> +               }
>                 warn("%s", tmpfil);
>

This should call warn() before unlink() or close() to guarantee that the
correct errno value is reported.




> -       if ( (edit_file = fdopen(fd, "w+")) == 0)
> +       if ( (edit_file = fdopen(fd, "w+")) == 0) {
> +               unlink(edit_name);
> +               close(fd);
>                 err(1, "fdopen");
> +       }
>

This and several other need to save errno and use errc(), ala:
    if ( (edit_file = fdopen(fd, "w+")) == 0) {
        int saved_errno  = errno;
        unlink(edit_name);
        close(fd);
        errc(1, saved_errno, "fdopen");
    }


Philip Guenther

Reply via email to