Skip to content

Commit d5d745f

Browse files
Olivier Maringitster
authored andcommitted
parse-options: fix segmentation fault when a required value is missing
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 <[email protected]> Acked-by: Pierre Habouzit <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d4ede9 commit d5d745f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
2222
p->opt = NULL;
2323
} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
2424
*arg = (const char *)opt->defval;
25-
} else if (p->argc) {
25+
} else if (p->argc > 1) {
2626
p->argc--;
2727
*arg = *++p->argv;
2828
} else

t/t0040-parse-options.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ test_expect_success 'long options' '
7878
test_cmp expect output
7979
'
8080

81+
test_expect_success 'missing required value' '
82+
test-parse-options -s;
83+
test $? = 129 &&
84+
test-parse-options --string;
85+
test $? = 129
86+
'
87+
8188
cat > expect << EOF
8289
boolean: 1
8390
integer: 13

0 commit comments

Comments
 (0)