Skip to content

Commit 891cb09

Browse files
avargitster
authored andcommitted
bundle: don't segfault on "git bundle <subcmd>"
Since aef7d75 (builtin/bundle.c: let parse-options parse subcommands, 2022-08-19) we've been segfaulting if no argument was provided. The fix is easy, as all of the "git bundle" subcommands require a non-option argument we can check that we have arguments left after calling parse-options(). This makes use of code added in 73c3253 (bundle: framework for options before bundle file, 2019-11-10), before this change that code has always been unreachable. In 73c3253 we'd never reach it as we already checked "argc < 2" in cmd_bundle() itself. Then when aef7d75 (whose segfault we're fixing here) migrated this code to the subcommand API it removed that "argc < 2" check, but we were still checking the wrong "argc" in parse_options_cmd_bundle(), we need to check the "newargc". The "argc" will always be >= 1, as it will necessarily contain at least the subcommand name itself (e.g. "create"). As an aside, this could be safely squashed into this, but let's not do that for this minimal segfault fix, as it's an unrelated refactoring: --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -55,13 +55,12 @@ static int parse_options_cmd_bundle(int argc, const char * const usagestr[], const struct option options[], char **bundle_file) { - int newargc; - newargc = parse_options(argc, argv, NULL, options, usagestr, + argc = parse_options(argc, argv, NULL, options, usagestr, PARSE_OPT_STOP_AT_NON_OPTION); - if (!newargc) + if (!argc) usage_with_options(usagestr, options); *bundle_file = prefix_filename(prefix, argv[0]); - return newargc; + return argc; } static int cmd_bundle_create(int argc, const char **argv, const char *prefix) { Reported-by: Hubert Jasudowicz <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Tested-by: Hubert Jasudowicz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8706a59 commit 891cb09

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

builtin/bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int parse_options_cmd_bundle(int argc,
4848
int newargc;
4949
newargc = parse_options(argc, argv, NULL, options, usagestr,
5050
PARSE_OPT_STOP_AT_NON_OPTION);
51-
if (argc < 1)
51+
if (!newargc)
5252
usage_with_options(usagestr, options);
5353
*bundle_file = prefix_filename(prefix, argv[0]);
5454
return newargc;

t/t6020-bundle-misc.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1111
. ./test-lib.sh
1212
. "$TEST_DIRECTORY"/lib-bundle.sh
1313

14+
for cmd in create verify list-heads unbundle
15+
do
16+
test_expect_success "usage: git bundle $cmd needs an argument" '
17+
test_expect_code 129 git bundle $cmd
18+
'
19+
done
20+
1421
# Create a commit or tag and set the variable with the object ID.
1522
test_commit_setvar () {
1623
notick=

0 commit comments

Comments
 (0)