]> git.ipfire.org Git - thirdparty/git.git/commitdiff
log: fix a memory leak in "git show <revision>..."
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Tue, 2 Aug 2022 15:33:13 +0000 (17:33 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Aug 2022 17:16:28 +0000 (10:16 -0700)
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>
builtin/log.c
t/t0203-gettext-setlocale-sanity.sh
t/t1020-subdirectory.sh
t/t3307-notes-man.sh
t/t3920-crlf-messages.sh
t/t4069-remerge-diff.sh
t/t7007-show.sh
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
t/t9122-git-svn-author.sh
t/t9162-git-svn-dcommit-interactive.sh

index 88a5e98875adb0398b2855460bddd5fa43e073ec..b4b1d9746178a988735cbaab9c7ea199d35ca0d9 100644 (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);
                }
index 0ce1f22eff66285ee0da9b1830de961555fbad2b..86cff324ff181110cf1ef8a44af6c4295b7f5d40 100755 (executable)
@@ -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' '
index 9fdbb2af80e0a82429289d06b24d4dcfac3f263d..45eef9457fe13d9eef6e463405190ca84c67bda5 100755 (executable)
@@ -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
 
index 1aa366a410e9a3e2ad4c2fa84431198fbb553a5f..ae316502c4531b7cdadfddff12e2f95ea7c9797c 100755 (executable)
@@ -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' '
index 0276edbe3d389b70cace45906626276cce3db44b..4c661d4d54a779f8bc056c62e5d9e864401420c7 100755 (executable)
@@ -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=""
index 35f94957fceb2b02df9846a256f925833c6b4ba0..9e7cac68b1ce3735237661e14e6c2c8c84cf4665 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='remerge-diff handling'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # This test is ort-specific
index d6cc69e0f2cbd576ff6ff81720de63f1e9cf86bf..f908a4d1abc5dc6608b4473937a614267013f3a2 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git show'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
index ad1eb64ba0db16d4f1452961c0a226fdf2e5cce0..aa004b70a8d1f1ab093b962a232524eaf4e851a1 100755 (executable)
@@ -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' '
index 527ba3d29322a14d609ea57e3740672f144f3182..0fc289ae0f02c064586b5ff9f1cab0ff0976814f 100755 (executable)
@@ -2,7 +2,6 @@
 
 test_description='git svn authorship'
 
-TEST_FAILS_SANITIZE_LEAK=true
 . ./lib-git-svn.sh
 
 test_expect_success 'setup svn repository' '
index e2aa8ed88a96ec0b50fb33f31e2e6d232895067c..b3ce033a0d3dec9c27b3a5e9114e07f18f20f04d 100755 (executable)
@@ -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' '