From: lum Date: Fri, 26 Feb 2021 01:17:21 +0000 (+0000) Subject: Some improvements from Joachim Wiberg's version of mg. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=24323129f44582d31bf69d4f6d31b3a70d4dc421;p=openbsd Some improvements from Joachim Wiberg's version of mg. check value of adjustname() use int for return value of snprintf --- diff --git a/usr.bin/mg/dired.c b/usr.bin/mg/dired.c index 90d9a638373..778a88d4da5 100644 --- a/usr.bin/mg/dired.c +++ b/usr.bin/mg/dired.c @@ -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);