From 5b4b58aad436e6ca92cf1314794ccb878994ed91 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 29 Oct 2019 13:00:20 -0700 Subject: [PATCH] Take a getopt_long() patch from upstream FreeBSD. r342757 | kevans | 2019-01-03 19:13:24 -0800 (Thu, 03 Jan 2019) | 19 lines getopt_long(3): fix case of malformed long opt When presented with an arg string like '-l-', getopt_long will successfully parse out the 'l' short option, then proceed to match '--' against the first longopts entry as it later does a strncmp with len=0. This latter bit is arguably another bug in itself, but presumably not a practical issue as all callers of parse_long_options are already doing the right thing (except this one pointed out). An opt string like '-l-' should be considered malformed and throw a bad argument rather than behaving as if '--' were passed. It cannot possibly do what the invoker expects, and it's probably the result of a typo (ls -l- a) rather than any intent. Reported by: Tony Overfield Reviewed by: imp MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D18616 Test: treehugger Change-Id: I593713bc35d70eb1975c9d7587528f2b3f9731af --- libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c b/libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c index 9534a2aff..6a3067c42 100644 --- a/libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c +++ b/libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c @@ -55,7 +55,7 @@ static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert #endif /* LIBC_SCCS and not lint */ #endif #include -__FBSDID("$FreeBSD$"); +__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt_long.c 342757 2019-01-04 03:13:24Z kevans $"); #include #include @@ -481,6 +481,8 @@ start: #endif if (*place == '-') { place++; /* --foo long option */ + if (*place == '\0') + return (BADARG); /* malformed option */ #ifdef GNU_COMPATIBLE dash_prefix = DD_PREFIX; #endif