threads / patch / 14574

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

Subject: [PATCH] parse-options: fix segmentation fault when a required value is missing

## tl;dr

3 messages between Jul 21, 2008 and Jul 22, 2008. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Olivier Marin· Jul 21, 2008, 18:30 UTC · lore
From: Olivier Marin <dkr@freesurf.fr>

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>
---
 I hope this is the right fix.
 parse-options.c          |    2 +-
 t/t0040-parse-options.sh |    7 +++++++
 2 files changed, 8 insertions(+), 1 deletions(-)
Show changes to 2 files +8 −1

parse-options.c, t/t0040-parse-options.sh

diff --git a/parse-options.c b/parse-options.c
index 987b015..71a7acf 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -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
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index 6309aed..03dbe00 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -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
-- 
1.6.0.rc0.1.g75f42
Pierre Habouzit· Jul 21, 2008, 19:07 UTC · re: Olivier Marin · lore

Re: [PATCH] parse-options: fix segmentation fault when a required value is missing

On Mon, Jul 21, 2008 at 06:30:36PM +0000, Olivier Marin wrote:
Show 7 quoted lines
> From: Olivier Marin <dkr@freesurf.fr>
> 
> 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>
Show 20 quoted 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(-)
> 
> diff --git a/parse-options.c b/parse-options.c
> index 987b015..71a7acf 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -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.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Olivier Marin· Jul 22, 2008, 10:11 UTC · re: Pierre Habouzit · lore

[PATCH v2] parse-options: fix segmentation fault when a required value is missing

From: Olivier Marin <dkr@freesurf.fr>

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>
---
 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(-)
Show changes to 2 files +8 −1

parse-options.c, t/t0040-parse-options.sh

diff --git a/parse-options.c b/parse-options.c
index 987b015..71a7acf 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -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
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index 6309aed..03dbe00 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -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
-- 
1.6.0.rc0.136.g55999

← back to recent threads