Skip to contentSkip to conversation

parse-options: fix segmentation fault when a required value is missing

PATCH In review Olivier Marin 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 parse-options: fix segmentation fault when a required value is missing

From: Olivier Marin <[email protected]>

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.

---
I hope this is the right fix.

 parse-options.c          |    2 +-
 t/t0040-parse-options.sh |    7 +++++++
 2 files changed, 8 insertions(+), 1 deletions(-)
parse-options.c +1 -1
index 987b015..71a7acf 100644
@@ -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
t/t0040-parse-options.sh +7 -0
index 6309aed..03dbe00 100755
@@ -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
Signature · 1 line
1.6.0.rc0.1.g75f42
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Olivier Marin

From: Olivier Marin <[email protected]>

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.

Quoted 12 lines

---
I hope this is the right fix.

 parse-options.c          |    2 +-
 t/t0040-parse-options.sh |    7 +++++++
 2 files changed, 8 insertions(+), 1 deletions(-)
parse-options.c +0 -0
index 987b015..71a7acf 100644

parse-options.c static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, · line 28

Quoted 8 lines
parse-options.c +1 -1
@@ -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

Gasp thanks, Junio pointed it to me already, and for some reason this
hasn't made it.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#2 lore raw

PATCH v2 1/1 parse-options: fix segmentation fault when a required value is missing

From: Olivier Marin <[email protected]>

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.

---

The same, not whitespace damaged! Sorry for the noise.

 parse-options.c          |    2 +-
 t/t0040-parse-options.sh |    7 +++++++
 2 files changed, 8 insertions(+), 1 deletions(-)
parse-options.c +1 -1
index 987b015..71a7acf 100644
@@ -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
t/t0040-parse-options.sh +7 -0
index 6309aed..03dbe00 100755
@@ -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
Signature · 1 line
1.6.0.rc0.136.g55999