Skip to content

Commit 055e57b

Browse files
avargitster
authored andcommitted
log: fix a memory leak in "git show <revision>..."
Fix a memory leak in code added in 5d7eeee (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 5d7eeee 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 f6bfea0 (revisions API users: use release_revisions() in builtin/log.c, 2022-04-13) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c541e77 commit 055e57b

10 files changed

+14
-2
lines changed

builtin/log.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,17 @@ int cmd_show(int argc, const char **argv, const char *prefix)
743743
rev.shown_one = 1;
744744
break;
745745
case OBJ_COMMIT:
746+
{
747+
struct object_array old;
748+
749+
memcpy(&old, &rev.pending, sizeof(old));
746750
rev.pending.nr = rev.pending.alloc = 0;
747751
rev.pending.objects = NULL;
748752
add_object_array(o, name, &rev.pending);
749753
ret = cmd_log_walk_no_free(&rev);
754+
memcpy(&rev.pending, &old, sizeof(rev.pending));
750755
break;
756+
}
751757
default:
752758
ret = error(_("unknown type: %d"), o->type);
753759
}

t/t0203-gettext-setlocale-sanity.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description="The Git C functions aren't broken by setlocale(3)"
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./lib-gettext.sh
910

1011
test_expect_success 'git show a ISO-8859-1 commit under C locale' '

t/t1020-subdirectory.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
test_description='Try various core-level commands in subdirectory.
77
'
88

9+
TEST_PASSES_SANITIZE_LEAK=true
910
. ./test-lib.sh
1011
. "$TEST_DIRECTORY"/lib-read-tree.sh
1112

t/t3307-notes-man.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='Examples from the git-notes man page
44
55
Make sure the manual is not full of lies.'
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
test_expect_success 'setup' '

t/t3920-crlf-messages.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='Test ref-filter and pretty APIs for commit and tag messages using CRLF'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
LIB_CRLF_BRANCHES=""

t/t4069-remerge-diff.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='remerge-diff handling'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
# This test is ort-specific

t/t7007-show.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git show'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success setup '

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='pre-commit and pre-merge-commit hooks'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
test_expect_success 'root commit' '

t/t9122-git-svn-author.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
test_description='git svn authorship'
44

5-
TEST_FAILS_SANITIZE_LEAK=true
65
. ./lib-git-svn.sh
76

87
test_expect_success 'setup svn repository' '

t/t9162-git-svn-dcommit-interactive.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
test_description='git svn dcommit --interactive series'
66

7-
TEST_FAILS_SANITIZE_LEAK=true
87
. ./lib-git-svn.sh
98

109
test_expect_success 'initialize repo' '

0 commit comments

Comments
 (0)