git: 2d0b4eef72ef - stable/14 - tftp: Simplify URI handling
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 27 May 2026 09:03:41 UTC
The branch stable/14 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=2d0b4eef72ef8180a51108c279a81462fcfa289d
commit 2d0b4eef72ef8180a51108c279a81462fcfa289d
Author: Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-05-22 17:57:08 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-05-27 09:03:29 +0000
tftp: Simplify URI handling
* No need to copy our argument into a new buffer; it is writeable and
will not be reused after we return.
* Instead of constructing the string "get path" and then splitting it
into an argument vector, just construct the vector directly. This
avoid potentially overrunning the buffer.
* Call settftpmode() just once, with either the default mode or the
user-provided value we already validated.
* Use errx() instead of fprintf(stderr) + exit().
Reported by: Moyao, Minghao Fu
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D57070
(cherry picked from commit a4b17594181502cea38ab0d8b2a9a10782286334)
---
usr.bin/tftp/main.c | 32 ++++++++++++--------------------
1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/usr.bin/tftp/main.c b/usr.bin/tftp/main.c
index 15ca7e03eb70..32eee2fb9afd 100644
--- a/usr.bin/tftp/main.c
+++ b/usr.bin/tftp/main.c
@@ -238,23 +238,18 @@ main(int argc, char *argv[])
static void
urihandling(char *URI)
{
- char uri[ARG_MAX];
+ char meth[] = "get";
char *host = NULL;
char *path = NULL;
char *opts = NULL;
const char *tmode = "octet";
char *s;
- char line[MAXLINE];
int i;
- strlcpy(uri, URI, ARG_MAX);
- host = uri + 7;
+ host = URI + 7;
- if ((s = strchr(host, '/')) == NULL) {
- fprintf(stderr,
- "Invalid URI: Couldn't find / after hostname\n");
- exit(1);
- }
+ if ((s = strchr(host, '/')) == NULL)
+ errx(1, "Invalid URI: Couldn't find / after hostname");
*s = '\0';
path = s + 1;
@@ -266,24 +261,21 @@ urihandling(char *URI)
tmode = opts;
tmode += 5;
- for (i = 0; modes[i].m_name != NULL; i++) {
+ for (i = 0; modes[i].m_name != NULL; i++)
if (strcmp(modes[i].m_name, tmode) == 0)
break;
- }
- if (modes[i].m_name == NULL) {
- fprintf(stderr, "Invalid mode: '%s'\n", mode);
- exit(1);
- }
- settftpmode(modes[i].m_mode);
+ if (modes[i].m_name == NULL)
+ errx(1, "Invalid mode: '%s'", mode);
}
- } else {
- settftpmode("octet");
}
+ settftpmode(tmode);
setpeer0(host, NULL);
- sprintf(line, "get %s", path);
- makeargv(line);
+ margc = 0;
+ margv[margc++] = meth;
+ margv[margc++] = path;
+ margv[margc] = NULL;
get(margc, margv);
}