Skip to content

Commit 4d9005f

Browse files
carenasgitster
authored andcommitted
bisect--helper: avoid segfault with bad syntax in start --term-*
06f5608 (bisect--helper: `bisect_start` shell function partially in C, 2019-01-02) adds a lax parser for `git bisect start` which could result in a segfault under a bad syntax call for start with custom terms. Detect if there are enough arguments left in the command line to use for --term-{old,good,new,bad} and abort with the same syntax error the original implementation will show if not. While at it, remove an unnecessary (and incomplete) check for unknown arguments and make sure to add a test to avoid regressions. Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Acked-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06f5608 commit 4d9005f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

builtin/bisect--helper.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,26 +452,31 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
452452
no_checkout = 1;
453453
} else if (!strcmp(arg, "--term-good") ||
454454
!strcmp(arg, "--term-old")) {
455+
i++;
456+
if (argc <= i)
457+
return error(_("'' is not a valid term"));
455458
must_write_terms = 1;
456459
free((void *) terms->term_good);
457-
terms->term_good = xstrdup(argv[++i]);
460+
terms->term_good = xstrdup(argv[i]);
458461
} else if (skip_prefix(arg, "--term-good=", &arg) ||
459462
skip_prefix(arg, "--term-old=", &arg)) {
460463
must_write_terms = 1;
461464
free((void *) terms->term_good);
462465
terms->term_good = xstrdup(arg);
463466
} else if (!strcmp(arg, "--term-bad") ||
464467
!strcmp(arg, "--term-new")) {
468+
i++;
469+
if (argc <= i)
470+
return error(_("'' is not a valid term"));
465471
must_write_terms = 1;
466472
free((void *) terms->term_bad);
467-
terms->term_bad = xstrdup(argv[++i]);
473+
terms->term_bad = xstrdup(argv[i]);
468474
} else if (skip_prefix(arg, "--term-bad=", &arg) ||
469475
skip_prefix(arg, "--term-new=", &arg)) {
470476
must_write_terms = 1;
471477
free((void *) terms->term_bad);
472478
terms->term_bad = xstrdup(arg);
473-
} else if (starts_with(arg, "--") &&
474-
!one_of(arg, "--term-good", "--term-bad", NULL)) {
479+
} else if (starts_with(arg, "--")) {
475480
return error(_("unrecognized option: '%s'"), arg);
476481
} else {
477482
char *commit_id = xstrfmt("%s^{commit}", arg);

t/t6030-bisect-porcelain.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,9 @@ test_expect_success 'bisect cannot mix terms' '
858858

859859
test_expect_success 'bisect terms rejects invalid terms' '
860860
git bisect reset &&
861+
test_must_fail git bisect start --term-good &&
861862
test_must_fail git bisect start --term-good invalid..term &&
863+
test_must_fail git bisect start --term-bad &&
862864
test_must_fail git bisect terms --term-bad invalid..term &&
863865
test_must_fail git bisect terms --term-good bad &&
864866
test_must_fail git bisect terms --term-good old &&

0 commit comments

Comments
 (0)