Some improvements from Joachim Wiberg's version of mg.
authorlum <lum@openbsd.org>
Fri, 26 Feb 2021 01:17:21 +0000 (01:17 +0000)
committerlum <lum@openbsd.org>
Fri, 26 Feb 2021 01:17:21 +0000 (01:17 +0000)
check value of adjustname()
use int for return value of snprintf

usr.bin/mg/dired.c

index 90d9a63..778a88d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: dired.c,v 1.94 2021/02/24 13:58:46 lum Exp $  */
+/*     $OpenBSD: dired.c,v 1.95 2021/02/26 01:17:21 lum Exp $  */
 
 /* This file is in the public domain. */
 
@@ -489,6 +489,8 @@ d_copy(int f, int n)
                        topath = adjustname(toname, TRUE);
                }
        }
+       if (topath == NULL)
+               return (FALSE);
        if (strcmp(frname, topath) == 0) {
                ewprintf("Cannot copy to same file: %s", frname);
                return (TRUE);
@@ -523,7 +525,7 @@ d_rename(int f, int n)
        off = strlcpy(toname, curbp->b_fname, sizeof(toname));
        if (off >= sizeof(toname) - 1) {        /* can't happen, really */
                dobeep();
-               ewprintf("Directory name too long");
+               ewprintf("Name too long");
                return (FALSE);
        }
        (void)xbasename(sname, frname, NFILEN);
@@ -537,9 +539,9 @@ d_rename(int f, int n)
        topath = adjustname(toname, TRUE);
        if (stat(topath, &statbuf) == 0) {
                if (S_ISDIR(statbuf.st_mode)) {
-                       off = snprintf(toname, sizeof(toname), "%s/%s",
+                       ret = snprintf(toname, sizeof(toname), "%s/%s",
                            topath, sname);
-                       if (off < 0 || off >= sizeof(toname) - 1) {
+                       if (ret < 0 || ret >= sizeof(toname) - 1) {
                                dobeep();
                                ewprintf("Directory name too long");
                                return (FALSE);
@@ -547,6 +549,8 @@ d_rename(int f, int n)
                        topath = adjustname(toname, TRUE);
                }
        }
+       if (topath == NULL)
+               return (FALSE);
        if (strcmp(frname, topath) == 0) {
                ewprintf("Cannot move to same file: %s", frname);
                return (TRUE);