git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] rev-parse parseopt: do not search help text for flag chars

From
Brandon Casey <drafnel@gmail.com>
Date
Sep 17, 2017, 22:28 UTC
Message-ID
<1505687297-31183-2-git-send-email-drafnel@gmail.com>
In-Reply-To
<1505687297-31183-1-git-send-email-drafnel@gmail.com>

When searching for flag characters in the option spec, we should ensure the search stays within the bounds of the option spec and does not enter the help text portion of the spec. So when we find the boundary white space marking the start of the help text, let's mark it with a nul character. Then when we search for flag characters starting from the beginning of the string we'll stop at the nul and won't enter the help text.

Now, the following option spec:
    exclame this does something!
will produce this 'set' expression when --exclame is specified:
    set -- --exclame --
instead of this one:
    set -- --exclame this does something --
Mark t1502.4 and t1502.5 as fixed.
Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 builtin/rev-parse.c           | 6 ++++--
 t/t1502-rev-parse-parseopt.sh | 4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 2bd28d3..b19f677 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -434,7 +434,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 	/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
 	while (strbuf_getline(&sb, stdin) != EOF) {
 		const char *s;
-		const char *help;
+		char *help;
 		struct option *o;
 
 		if (!sb.len)
@@ -451,8 +451,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 			continue;
 		}
 
+		*help = '\0';
+
 		o->type = OPTION_CALLBACK;
-		o->help = xstrdup(skipspaces(help));
+		o->help = xstrdup(skipspaces(help+1));
 		o->value = &parsed;
 		o->flags = PARSE_OPT_NOARG;
 		o->callback = &parseopt_dump;
diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh
index 910fc56..3d895e0 100755
--- a/t/t1502-rev-parse-parseopt.sh
+++ b/t/t1502-rev-parse-parseopt.sh
@@ -85,12 +85,12 @@ set -- --foo --bar 'ham' -b --aswitch -- 'arg'
 EOF
 "
 
-test_expect_failure 'test --parseopt' '
+test_expect_success 'test --parseopt' '
 	git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
 	test_cmp expect output
 '
 
-test_expect_failure 'test --parseopt with mixed options and arguments' '
+test_expect_success 'test --parseopt with mixed options and arguments' '
 	git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
 	test_cmp expect output
 '
-- 
2.2.0.rc3
Previous: Brandon CaseyNext: Brandon Casey
Message 2 of 4 in “t1502: demonstrate rev-parse --parseopt option mis-parsing”
  1. 1/4 t1502: demonstrate rev-parse --parseopt option mis-parsingBrandon Casey, Sep 17, 2017
  2. 2/4 rev-parse parseopt: do not search help text for flag charsBrandon Casey, Sep 17, 2017
  3. 3/4 rev-parse parseopt: interpret any whitespace as start of help textBrandon Casey, Sep 17, 2017
  4. 4/4 git-rebase: don't ignore unexpected command line argumentsBrandon Casey, Sep 17, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.