Skip to content

Commit 0d260f9

Browse files
René Scharfegitster
authored andcommitted
parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used together
As suggested by Junio, disallow the flags PARSE_OPT_KEEP_UNKNOWN and PARSE_OPT_STOP_AT_NON_OPTION to be turned on at the same time, as a value of an unknown option could be mistakenly classified as a non-option, stopping the parser early. E.g.: git cmd --known --unknown value arg0 arg1 The parser should have stopped at "arg0", but it already stops at "value". This patch makes parse_options() die if the two flags are used in combination. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ad7e6e commit 0d260f9

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ Flags are the bitwise-or of:
9696
`PARSE_OPT_STOP_AT_NON_OPTION` is set, the second argument in
9797
`--unknown value` will be mistakenly interpreted as a
9898
non-option, not as a value belonging to the unknown option,
99-
stopping the parser early.
99+
the parser early. That's why parse_options() errors out if
100+
both options are set.
100101

101102
`PARSE_OPT_NO_INTERNAL_HELP`::
102103
By default, parse_options() handles `-h`, `--help` and

parse-options.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
244244
ctx->out = argv;
245245
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
246246
ctx->flags = flags;
247+
if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
248+
(flags & PARSE_OPT_STOP_AT_NON_OPTION))
249+
die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
247250
}
248251

249252
static int usage_with_options_internal(const char * const *,

0 commit comments

Comments
 (0)