@matheusbsbs / matheus-bagliano / commits / d5d745f90b
Fork 0

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 <dkr@freesurf.fr> Acked-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olivier Marin committed Jul 21, 2008 at 20:30 UTC d5d745f90b929310f3893bb8b3766cc1745140c8
2 files changed +8 -1
parse-options.c
+1 -1
@@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
22 p->opt = NULL;
23 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
24 *arg = (const char *)opt->defval;
25 - } else if (p->argc) {
25 + } else if (p->argc > 1) {
26 p->argc--;
27 *arg = *++p->argv;
28 } else
t/t0040-parse-options.sh
+7
@@ -78,6 +78,13 @@ test_expect_success 'long options' '
78 test_cmp expect output
79 '
80
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 +
88 cat > expect << EOF
89 boolean: 1
90 integer: 13