blame: fix object casting regression
authorJeff King <peff@peff.net>
Tue, 17 Nov 2015 23:22:37 +0000 (18:22 -0500)
committerJeff King <peff@peff.net>
Tue, 24 Nov 2015 22:07:04 +0000 (17:07 -0500)
Commit 1b0d400 refactored the prepare_final() function so
that it could be reused in multiple places. Originally, the
loop had two outputs: a commit to stuff into sb->final, and
the name of the commit from the rev->pending array.

After the refactor, that loop is put in its own function
with a single return value: the object_array_entry from the
rev->pending array. This contains both the name and the object,
but with one important difference: the object is the
_original_ object found by the revision parser, not the
dereferenced commit. If one feeds a tag to "git blame", we
end up casting the tag object to a "struct commit", which
causes a segfault.

Instead, let's return the commit (properly casted) directly
from the function, and take the "name" as an optional
out-parameter. This does the right thing, and actually
simplifies the callers, who no longer need to cast or
dereference the object_array_entry themselves.

[test case by Max Kirillov <max@max630.net>]

Signed-off-by: Jeff King <peff@peff.net>
builtin/blame.c
t/annotate-tests.sh
index 861a5372b9194b71d3f315c72965fb171d4e3392..eaa2c4260375370cb5fb15bf2ea600b39d4d8cf7 100644 (file)
@@ -2396,10 +2396,12 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        return commit;
 }
 
-static struct object_array_entry *find_single_final(struct rev_info *revs)
+static struct commit *find_single_final(struct rev_info *revs,
+                                       const char **name_p)
 {
        int i;
-       struct object_array_entry *found = NULL;
+       struct commit *found = NULL;
+       const char *name = NULL;
 
        for (i = 0; i < revs->pending.nr; i++) {
                struct object *obj = revs->pending.objects[i].item;
@@ -2411,22 +2413,20 @@ static struct object_array_entry *find_single_final(struct rev_info *revs)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (found)
                        die("More than one commit to dig from %s and %s?",
-                           revs->pending.objects[i].name,
-                           found->name);
-               found = &(revs->pending.objects[i]);
+                           revs->pending.objects[i].name, name);
+               found = (struct commit *)obj;
+               name = revs->pending.objects[i].name;
        }
+       if (name_p)
+               *name_p = name;
        return found;
 }
 
 static char *prepare_final(struct scoreboard *sb)
 {
-       struct object_array_entry *found = find_single_final(sb->revs);
-       if (found) {
-               sb->final = (struct commit *) found->item;
-               return xstrdup(found->name);
-       } else {
-               return NULL;
-       }
+       const char *name;
+       sb->final = find_single_final(sb->revs, &name);
+       return xstrdup_or_null(name);
 }
 
 static char *prepare_initial(struct scoreboard *sb)
@@ -2712,11 +2712,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                die("Cannot use --contents with final commit object name");
 
        if (reverse && revs.first_parent_only) {
-               struct object_array_entry *entry = find_single_final(sb.revs);
-               if (!entry)
+               final_commit = find_single_final(sb.revs, NULL);
+               if (!final_commit)
                        die("--reverse and --first-parent together require specified latest commit");
-               else
-                       final_commit = (struct commit*) entry->item;
        }
 
        /*
index b1673b3e8f38d9961b8b2783e1f5c47a865638fb..093832fef1541906b735d162a76c8ffa773d236c 100644 (file)
@@ -68,6 +68,13 @@ test_expect_success 'blame 1 author' '
        check_count A 2
 '
 
+test_expect_success 'blame by tag objects' '
+       git tag -m "test tag" testTag &&
+       git tag -m "test tag #2" testTag2 testTag &&
+       check_count -h testTag A 2 &&
+       check_count -h testTag2 A 2
+'
+
 test_expect_success 'setup B lines' '
        echo "2A quick brown fox jumps over the" >>file &&
        echo "lazy dog" >>file &&