Skip to content

Commit

Permalink
parse-options: fix segmentation fault when a required value is missing
Browse files Browse the repository at this point in the history
p->argc represent the number of arguments that have not been parsed yet,
_including_ the one we are currently parsing. If it is not greater than
one then there is no more argument.

Signed-off-by: Olivier Marin <dkr@freesurf.fr>
Acked-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Olivier Marin authored and Junio C Hamano committed Jul 22, 2008
1 parent 0d4ede9 commit d5d745f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
p->opt = NULL;
} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
*arg = (const char *)opt->defval;
} else if (p->argc) {
} else if (p->argc > 1) {
p->argc--;
*arg = *++p->argv;
} else
Expand Down
7 changes: 7 additions & 0 deletions t/t0040-parse-options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ test_expect_success 'long options' '
test_cmp expect output
'

test_expect_success 'missing required value' '
test-parse-options -s;
test $? = 129 &&
test-parse-options --string;
test $? = 129
'

cat > expect << EOF
boolean: 1
integer: 13
Expand Down

0 comments on commit d5d745f

Please sign in to comment.