{"thread":{"id":"14574","subject":"[PATCH] parse-options: fix segmentation fault when a required value is missing","startedAt":"2008-07-21T18:30:36Z","lastAt":"2008-07-22T10:11:33Z","messageCount":3,"participants":["Olivier Marin","Pierre Habouzit"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"84218","messageId":"4884D5CC.2070007@free.fr","threadId":"14574","inReplyTo":null,"subject":"[PATCH] parse-options: fix segmentation fault when a required value is missing","fromName":"Olivier Marin","fromEmail":"dkr+ml.git@free.fr","sentAt":"2008-07-21T18:30:36Z","receivedAt":"2008-07-21T18:30:36Z","isPatch":true,"sender":{"key":"dkr+ml.git@free.fr","avatar":null},"body":"From: Olivier Marin <dkr@freesurf.fr>\n\np->argc represent the number of arguments that have not been parsed yet,\n_including_ the one we are currently parsing. If it is not greater than\none then there is no more argument.\n\nSigned-off-by: Olivier Marin <dkr@freesurf.fr>\n---\n I hope this is the right fix.\n\n parse-options.c          |    2 +-\n t/t0040-parse-options.sh |    7 +++++++\n 2 files changed, 8 insertions(+), 1 deletions(-)\n\ndiff --git a/parse-options.c b/parse-options.c\nindex 987b015..71a7acf 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,\n                p->opt = NULL;\n        } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {\n                *arg = (const char *)opt->defval;\n-       } else if (p->argc) {\n+       } else if (p->argc > 1) {\n                p->argc--;\n                *arg = *++p->argv;\n        } else\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex 6309aed..03dbe00 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -78,6 +78,13 @@ test_expect_success 'long options' '\n        test_cmp expect output\n '\n \n+test_expect_success 'missing required value' '\n+       test-parse-options -s;\n+       test $? = 129 &&\n+       test-parse-options --string;\n+       test $? = 129\n+'\n+\n cat > expect << EOF\n boolean: 1\n integer: 13\n-- \n1.6.0.rc0.1.g75f42\n"},{"id":"84220","messageId":"20080721190709.GD2718@artemis.madism.org","threadId":"14574","inReplyTo":"4884D5CC.2070007@free.fr","subject":"Re: [PATCH] parse-options: fix segmentation fault when a required value is missing","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2008-07-21T19:07:10Z","receivedAt":"2008-07-21T19:07:10Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"On Mon, Jul 21, 2008 at 06:30:36PM +0000, Olivier Marin wrote:\n> From: Olivier Marin <dkr@freesurf.fr>\n> \n> p->argc represent the number of arguments that have not been parsed yet,\n> _including_ the one we are currently parsing. If it is not greater than\n> one then there is no more argument.\n> \n> Signed-off-by: Olivier Marin <dkr@freesurf.fr>\nAcked-by: Pierre Habouzit <madcoder@debian.org>\n\n> ---\n>  I hope this is the right fix.\n> \n>  parse-options.c          |    2 +-\n>  t/t0040-parse-options.sh |    7 +++++++\n>  2 files changed, 8 insertions(+), 1 deletions(-)\n> \n> diff --git a/parse-options.c b/parse-options.c\n> index 987b015..71a7acf 100644\n> --- a/parse-options.c\n> +++ b/parse-options.c\n> @@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,\n>                 p->opt = NULL;\n>         } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {\n>                 *arg = (const char *)opt->defval;\n> -       } else if (p->argc) {\n> +       } else if (p->argc > 1) {\n>                 p->argc--;\n>                 *arg = *++p->argv;\n>         } else\n\n  Gasp thanks, Junio pointed it to me already, and for some reason this\nhasn't made it.\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"84316","messageId":"1216721493-17836-1-git-send-email-dkr+ml.git@free.fr","threadId":"14574","inReplyTo":"20080721190709.GD2718@artemis.madism.org","subject":"[PATCH v2] parse-options: fix segmentation fault when a required value is missing","fromName":"Olivier Marin","fromEmail":"dkr+ml.git@free.fr","sentAt":"2008-07-22T10:11:33Z","receivedAt":"2008-07-22T10:11:33Z","isPatch":true,"sender":{"key":"dkr+ml.git@free.fr","avatar":null},"body":"From: Olivier Marin <dkr@freesurf.fr>\n\np->argc represent the number of arguments that have not been parsed yet,\n_including_ the one we are currently parsing. If it is not greater than\none then there is no more argument.\n\nSigned-off-by: Olivier Marin <dkr@freesurf.fr>\nAcked-by: Pierre Habouzit <madcoder@debian.org>\n---\n\n The same, not whitespace damaged! Sorry for the noise.\n\n parse-options.c          |    2 +-\n t/t0040-parse-options.sh |    7 +++++++\n 2 files changed, 8 insertions(+), 1 deletions(-)\n\ndiff --git a/parse-options.c b/parse-options.c\nindex 987b015..71a7acf 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -22,7 +22,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,\n \t\tp->opt = NULL;\n \t} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {\n \t\t*arg = (const char *)opt->defval;\n-\t} else if (p->argc) {\n+\t} else if (p->argc > 1) {\n \t\tp->argc--;\n \t\t*arg = *++p->argv;\n \t} else\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex 6309aed..03dbe00 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -78,6 +78,13 @@ test_expect_success 'long options' '\n \ttest_cmp expect output\n '\n \n+test_expect_success 'missing required value' '\n+\ttest-parse-options -s;\n+\ttest $? = 129 &&\n+\ttest-parse-options --string;\n+\ttest $? = 129\n+'\n+\n cat > expect << EOF\n boolean: 1\n integer: 13\n-- \n1.6.0.rc0.136.g55999\n"}]}