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>

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