@matheusbsbs / matheus-bagliano / commits / 4d9005ff5d
Fork 0

bisect--helper: avoid segfault with bad syntax in `start --term-*`

06f5608c14 (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 <carenas@gmail.com> Acked-by: Christian Couder <christian.couder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlo Marcelo Arenas Belón committed May 20, 2020 at 16:26 UTC 4d9005ff5d1de0b4e82402320aab4ff27aa23b43
2 files changed +11 -4
builtin/bisect--helper.c
+9 -4
@@ -452,9 +452,12 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
452 no_checkout = 1;
453 } else if (!strcmp(arg, "--term-good") ||
454 !strcmp(arg, "--term-old")) {
455 + i++;
456 + if (argc <= i)
457 + return error(_("'' is not a valid term"));
458 must_write_terms = 1;
459 free((void *) terms->term_good);
457 - terms->term_good = xstrdup(argv[++i]);
460 + terms->term_good = xstrdup(argv[i]);
461 } else if (skip_prefix(arg, "--term-good=", &arg) ||
462 skip_prefix(arg, "--term-old=", &arg)) {
463 must_write_terms = 1;
@@ -462,16 +465,18 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
465 terms->term_good = xstrdup(arg);
466 } else if (!strcmp(arg, "--term-bad") ||
467 !strcmp(arg, "--term-new")) {
468 + i++;
469 + if (argc <= i)
470 + return error(_("'' is not a valid term"));
471 must_write_terms = 1;
472 free((void *) terms->term_bad);
467 - terms->term_bad = xstrdup(argv[++i]);
473 + terms->term_bad = xstrdup(argv[i]);
474 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
475 skip_prefix(arg, "--term-new=", &arg)) {
476 must_write_terms = 1;
477 free((void *) terms->term_bad);
478 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, "--")) {
480 return error(_("unrecognized option: '%s'"), arg);
481 } else {
482 char *commit_id = xstrfmt("%s^{commit}", arg);
t/t6030-bisect-porcelain.sh
+2
@@ -858,7 +858,9 @@ test_expect_success 'bisect cannot mix terms' '
858
859 test_expect_success 'bisect terms rejects invalid terms' '
860 git bisect reset &&
861 + test_must_fail git bisect start --term-good &&
862 test_must_fail git bisect start --term-good invalid..term &&
863 + test_must_fail git bisect start --term-bad &&
864 test_must_fail git bisect terms --term-bad invalid..term &&
865 test_must_fail git bisect terms --term-good bad &&
866 test_must_fail git bisect terms --term-good old &&