rev-parse parseopt: do not search help text for flag chars

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Sep 17, 2017 at 15:28 UTC 28a8d0f77a27646dcbd87f0718b79501113cb82c
2 files changed +6 -4
builtin/rev-parse.c
+4 -2
@@ -434,7 +434,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
434 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
435 while (strbuf_getline(&sb, stdin) != EOF) {
436 const char *s;
437 - const char *help;
437 + char *help;
438 struct option *o;
439
440 if (!sb.len)
@@ -451,8 +451,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
451 continue;
452 }
453
454 + *help = '\0';
455 +
456 o->type = OPTION_CALLBACK;
455 - o->help = xstrdup(skipspaces(help));
457 + o->help = xstrdup(skipspaces(help+1));
458 o->value = &parsed;
459 o->flags = PARSE_OPT_NOARG;
460 o->callback = &parseopt_dump;
t/t1502-rev-parse-parseopt.sh
+2 -2
@@ -85,12 +85,12 @@ set -- --foo --bar 'ham' -b --aswitch -- 'arg'
85 EOF
86 "
87
88 -test_expect_failure 'test --parseopt' '
88 +test_expect_success 'test --parseopt' '
89 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
90 test_cmp expect output
91 '
92
93 -test_expect_failure 'test --parseopt with mixed options and arguments' '
93 +test_expect_success 'test --parseopt with mixed options and arguments' '
94 git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
95 test_cmp expect output
96 '