branch: don't assume the merge filter ref exists

print_ref_list looks up the merge_filter_ref and assumes that a valid pointer is returned. When the object doesn't exist, it tries to dereference a NULL pointer. This can be the case when git branch --merged is given an argument that isn't a valid commit name. Check whether the lookup returns a NULL pointer and die with an error if it does. Add a test, while we're at it. Signed-off-by: Carlos Martín Nieto <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Feb 27, 2012 at 16:11 UTC 6c41e97557d94df7085e3c0cff247305c9401968
2 files changed +8
builtin/branch.c
+4
@@ -528,6 +528,10 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
528 if (merge_filter != NO_FILTER) {
529 struct commit *filter;
530 filter = lookup_commit_reference_gently(merge_filter_ref, 0);
531 + if (!filter)
532 + die("object '%s' does not point to a commit",
533 + sha1_to_hex(merge_filter_ref));
534 +
535 filter->object.flags |= UNINTERESTING;
536 add_pending_object(&ref_list.revs,
537 (struct object *) filter, "");
t/t3200-branch.sh
+4
@@ -620,4 +620,8 @@ test_expect_success 'use set-upstream on the current branch' '
620
621 '
622
623 +test_expect_success '--merged catches invalid object names' '
624 + test_must_fail git branch --merged 0000000000000000000000000000000000000000
625 +'
626 +
627 test_done