Git fork

log: fix a memory leak in "git show <revision>..."

Fix a memory leak in code added in 5d7eeee2ac6 (git-show: grok blobs,
trees and tags, too, 2006-12-14). As we iterate over a "<revision>..."
command-line and encounter ad OBJ_COMMIT we want to use our "struct
rev_info", but with a "pending" array of one element: the one commit
we're showing in the loop.

To do this 5d7eeee2ac6 saved away a pointer to rev.pending.objects and
rev.pending.nr for its iteration. We'd then clobber those (and alloc)
when we needed to show an OBJ_COMMIT.

We'd therefore leak the "rev.pending" we started out with, and only
free the new "rev.pending" in the "OBJ_COMMIT" case arm as
prepare_revision_walk() would draw it down.

Let's fix this memory leak. Now when we encounter an OBJ_COMMIT we
save away the "rev.pending" before clearing it. We then add a single
commit to it, which our indirect invocation of prepare_revision_walk()
will remove. After that we restore the "rev.pending".

Our "rev.pending" will then get free'd by the release_revisions()
added in f6bfea0ad01 (revisions API users: use release_revisions() in
builtin/log.c, 2022-04-13)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Ævar Arnfjörð Bjarmason and committed by
Junio C Hamano
055e57b7 c541e77c

+14 -2
+6
builtin/log.c
··· 743 743 rev.shown_one = 1; 744 744 break; 745 745 case OBJ_COMMIT: 746 + { 747 + struct object_array old; 748 + 749 + memcpy(&old, &rev.pending, sizeof(old)); 746 750 rev.pending.nr = rev.pending.alloc = 0; 747 751 rev.pending.objects = NULL; 748 752 add_object_array(o, name, &rev.pending); 749 753 ret = cmd_log_walk_no_free(&rev); 754 + memcpy(&rev.pending, &old, sizeof(rev.pending)); 750 755 break; 756 + } 751 757 default: 752 758 ret = error(_("unknown type: %d"), o->type); 753 759 }
+1
t/t0203-gettext-setlocale-sanity.sh
··· 5 5 6 6 test_description="The Git C functions aren't broken by setlocale(3)" 7 7 8 + TEST_PASSES_SANITIZE_LEAK=true 8 9 . ./lib-gettext.sh 9 10 10 11 test_expect_success 'git show a ISO-8859-1 commit under C locale' '
+1
t/t1020-subdirectory.sh
··· 6 6 test_description='Try various core-level commands in subdirectory. 7 7 ' 8 8 9 + TEST_PASSES_SANITIZE_LEAK=true 9 10 . ./test-lib.sh 10 11 . "$TEST_DIRECTORY"/lib-read-tree.sh 11 12
+1
t/t3307-notes-man.sh
··· 4 4 5 5 Make sure the manual is not full of lies.' 6 6 7 + TEST_PASSES_SANITIZE_LEAK=true 7 8 . ./test-lib.sh 8 9 9 10 test_expect_success 'setup' '
+2
t/t3920-crlf-messages.sh
··· 1 1 #!/bin/sh 2 2 3 3 test_description='Test ref-filter and pretty APIs for commit and tag messages using CRLF' 4 + 5 + TEST_PASSES_SANITIZE_LEAK=true 4 6 . ./test-lib.sh 5 7 6 8 LIB_CRLF_BRANCHES=""
+1
t/t4069-remerge-diff.sh
··· 2 2 3 3 test_description='remerge-diff handling' 4 4 5 + TEST_PASSES_SANITIZE_LEAK=true 5 6 . ./test-lib.sh 6 7 7 8 # This test is ort-specific
+1
t/t7007-show.sh
··· 2 2 3 3 test_description='git show' 4 4 5 + TEST_PASSES_SANITIZE_LEAK=true 5 6 . ./test-lib.sh 6 7 7 8 test_expect_success setup '
+1
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
··· 5 5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 6 6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 7 7 8 + TEST_PASSES_SANITIZE_LEAK=true 8 9 . ./test-lib.sh 9 10 10 11 test_expect_success 'root commit' '
-1
t/t9122-git-svn-author.sh
··· 2 2 3 3 test_description='git svn authorship' 4 4 5 - TEST_FAILS_SANITIZE_LEAK=true 6 5 . ./lib-git-svn.sh 7 6 8 7 test_expect_success 'setup svn repository' '
-1
t/t9162-git-svn-dcommit-interactive.sh
··· 4 4 5 5 test_description='git svn dcommit --interactive series' 6 6 7 - TEST_FAILS_SANITIZE_LEAK=true 8 7 . ./lib-git-svn.sh 9 8 10 9 test_expect_success 'initialize repo' '