summaryrefslogtreecommitdiff
path: root/usr.bin/ftp/fetch.c
diff options
context:
space:
mode:
authorChristian Weisgerber <naddy@cvs.openbsd.org>2020-10-18 20:35:19 +0000
committerChristian Weisgerber <naddy@cvs.openbsd.org>2020-10-18 20:35:19 +0000
commitc8fecc44cd64d87a4708ab2a11488d323da24703 (patch)
tree36fe80ad0016b55e50bda6c9e8f8f73f3a2c9eac /usr.bin/ftp/fetch.c
parentb99fd65d5fe9a2671d050ac1dcbc2290767d423e (diff)
Accommodate POSIX basename(3) that takes a non-const parameter and
may modify the string buffer. improved and ok jca@
Diffstat (limited to 'usr.bin/ftp/fetch.c')
-rw-r--r--usr.bin/ftp/fetch.c23
1 files changed, 17 insertions, 6 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);