rev-parse: check lookup'ed commit references for NULL
authorElijah Newren <newren@gmail.com>
Thu, 24 May 2018 06:27:33 +0000 (23:27 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 25 May 2018 01:46:42 +0000 (10:46 +0900)
Commits 2122f8b963d4 ("rev-parse: Add support for the ^! and ^@ syntax",
2008-07-26) and 3dd4e7320d ("Teach rev-parse the ... syntax.", 2006-07-04)
taught rev-parse new syntax, and used lookup_commit_reference() as part of
their logic. Neither usage checked the returned commit to see if it was
non-NULL before using it. Check for NULL and ensure an appropriate error
is reported to the user.

Reported by Florian Weimer and Todd Zullinger.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Elijah Newren <newren@gmail.com>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t6101-rev-parse-parents.sh
index 74aa644cbb37b5741c521eba53d0a040aaf1582e..da5748bdaef376f35fdb49125e959474913c6ad9 100644 (file)
@@ -282,6 +282,10 @@ static int try_difference(const char *arg)
                        struct commit *a, *b;
                        a = lookup_commit_reference(&oid);
                        b = lookup_commit_reference(&end);
+                       if (!a || !b) {
+                               *dotdot = '.';
+                               return 0;
+                       }
                        exclude = get_merge_bases(a, b);
                        while (exclude) {
                                struct commit *commit = pop_commit(&exclude);
@@ -328,12 +332,12 @@ static int try_parent_shorthands(const char *arg)
                return 0;
 
        *dotdot = 0;
-       if (get_oid_committish(arg, &oid)) {
+       if (get_oid_committish(arg, &oid) ||
+           !(commit = lookup_commit_reference(&oid))) {
                *dotdot = '^';
                return 0;
        }
 
-       commit = lookup_commit_reference(&oid);
        if (exclude_parent &&
            exclude_parent > commit_list_count(commit->parents)) {
                *dotdot = '^';
index 8c617981a3a06c1ff49f03a2914bfdc31c0e293c..7683e4a1142a6ee31a1f1fd51b774b19fc2c7401 100755 (executable)
@@ -214,4 +214,12 @@ test_expect_success 'rev-list merge^-1x (garbage after ^-1)' '
        test_must_fail git rev-list merge^-1x
 '
 
+test_expect_success 'rev-parse $garbage^@ does not segfault' '
+       test_must_fail git rev-parse $EMPTY_TREE^@
+'
+
+test_expect_success 'rev-parse $garbage...$garbage does not segfault' '
+       test_must_fail git rev-parse $EMPTY_TREE...$EMPTY_BLOB
+'
+
 test_done