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

Fix a memory leak in code added in 5d7eeee2ac (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 5d7eeee2ac 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 f6bfea0ad0 (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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2022-08-02 17:33:13 +02:00 committed by Junio C Hamano
parent c541e77cf8
commit 055e57b7b2
10 changed files with 14 additions and 2 deletions

View File

@ -743,11 +743,17 @@ int cmd_show(int argc, const char **argv, const char *prefix)
rev.shown_one = 1;
break;
case OBJ_COMMIT:
{
struct object_array old;
memcpy(&old, &rev.pending, sizeof(old));
rev.pending.nr = rev.pending.alloc = 0;
rev.pending.objects = NULL;
add_object_array(o, name, &rev.pending);
ret = cmd_log_walk_no_free(&rev);
memcpy(&rev.pending, &old, sizeof(rev.pending));
break;
}
default:
ret = error(_("unknown type: %d"), o->type);
}

View File

@ -5,6 +5,7 @@
test_description="The Git C functions aren't broken by setlocale(3)"
TEST_PASSES_SANITIZE_LEAK=true
. ./lib-gettext.sh
test_expect_success 'git show a ISO-8859-1 commit under C locale' '

View File

@ -6,6 +6,7 @@
test_description='Try various core-level commands in subdirectory.
'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-read-tree.sh

View File

@ -4,6 +4,7 @@ test_description='Examples from the git-notes man page
Make sure the manual is not full of lies.'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup' '

View File

@ -1,6 +1,8 @@
#!/bin/sh
test_description='Test ref-filter and pretty APIs for commit and tag messages using CRLF'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
LIB_CRLF_BRANCHES=""

View File

@ -2,6 +2,7 @@
test_description='remerge-diff handling'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
# This test is ort-specific

View File

@ -2,6 +2,7 @@
test_description='git show'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success setup '

View File

@ -5,6 +5,7 @@ test_description='pre-commit and pre-merge-commit hooks'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'root commit' '

View File

@ -2,7 +2,6 @@
test_description='git svn authorship'
TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh
test_expect_success 'setup svn repository' '

View File

@ -4,7 +4,6 @@
test_description='git svn dcommit --interactive series'
TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh
test_expect_success 'initialize repo' '