From c8fecc44cd64d87a4708ab2a11488d323da24703 Mon Sep 17 00:00:00 2001 From: Christian Weisgerber Date: Sun, 18 Oct 2020 20:35:19 +0000 Subject: Accommodate POSIX basename(3) that takes a non-const parameter and may modify the string buffer. improved and ok jca@ --- usr.bin/ftp/fetch.c | 23 +++++++++++++++++------ usr.bin/ftp/util.c | 9 +++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index e9da609043d..e1ee0ca32ad 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fetch.c,v 1.197 2020/07/04 11:23:35 kn Exp $ */ +/* $OpenBSD: fetch.c,v 1.198 2020/10/18 20:35:18 naddy Exp $ */ /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */ /*- @@ -192,7 +192,7 @@ file_get(const char *path, const char *outfile) int fd, out = -1, rval = -1, save_errno; volatile sig_t oldintr, oldinti; const char *savefile; - char *buf = NULL, *cp; + char *buf = NULL, *cp, *pathbuf = NULL; const size_t buflen = 128 * 1024; off_t hashbytes; ssize_t len, wlen; @@ -215,8 +215,12 @@ file_get(const char *path, const char *outfile) else { if (path[strlen(path) - 1] == '/') /* Consider no file */ savefile = NULL; /* after dir invalid. */ - else - savefile = basename(path); + else { + pathbuf = strdup(path); + if (pathbuf == NULL) + errx(1, "Can't allocate memory for filename"); + savefile = basename(pathbuf); + } } if (EMPTYSTRING(savefile)) { @@ -294,6 +298,7 @@ file_get(const char *path, const char *outfile) cleanup_copy: free(buf); + free(pathbuf); if (out >= 0 && out != fileno(stdout)) close(out); close(fd); @@ -315,6 +320,7 @@ url_get(const char *origline, const char *proxyenv, const char *outfile, int las int isunavail = 0, retryafter = -1; struct addrinfo hints, *res0, *res; const char *savefile; + char *pathbuf = NULL; char *proxyurl = NULL; char *credentials = NULL, *proxy_credentials = NULL; int fd = -1, out = -1; @@ -412,8 +418,12 @@ noslash: else { if (path[strlen(path) - 1] == '/') /* Consider no file */ savefile = NULL; /* after dir invalid. */ - else - savefile = basename(path); + else { + pathbuf = strdup(path); + if (pathbuf == NULL) + errx(1, "Can't allocate memory for filename"); + savefile = basename(pathbuf); + } } if (EMPTYSTRING(savefile)) { @@ -1106,6 +1116,7 @@ cleanup_url_get: if (out >= 0 && out != fileno(stdout)) close(out); free(buf); + free(pathbuf); free(proxyhost); free(proxyurl); free(newline); diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c index d04e650df5c..930402c8c55 100644 --- a/usr.bin/ftp/util.c +++ b/usr.bin/ftp/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.93 2020/07/06 17:11:29 deraadt Exp $ */ +/* $OpenBSD: util.c,v 1.94 2020/10/18 20:35:18 naddy Exp $ */ /* $NetBSD: util.c,v 1.12 1997/08/18 10:20:27 lukem Exp $ */ /*- @@ -763,7 +763,7 @@ progressmeter(int flag, const char *filename) off_t cursize, abbrevsize; double elapsed; int ratio, barlength, i, remaining, overhead = 30; - char buf[512]; + char buf[512], *filenamebuf; if (flag == -1) { clock_gettime(CLOCK_MONOTONIC, &start); @@ -782,11 +782,12 @@ progressmeter(int flag, const char *filename) ratio = MAXIMUM(ratio, 0); ratio = MINIMUM(ratio, 100); if (!verbose && flag == -1) { - filename = basename(filename); - if (filename != NULL) { + if ((filenamebuf = strdup(filename)) != NULL && + (filename = basename(filenamebuf)) != NULL) { free(title); title = strdup(filename); } + free(filenamebuf); } buf[0] = 0; -- cgit v1.2.3