git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/6] log: fix common "rev.pending" memory leak in "git show"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 13, 2022, 13:10 UTC
Message-ID
<patch-4.6-9bff7b10197-20220713T130511Z-avarab@gmail.com>
In-Reply-To
<cover-0.6-00000000000-20220713T130511Z-avarab@gmail.com>

Fix a very common memory leak introduced in 5d7eeee2ac6 (git-show: grok blobs, trees and tags, too, 2006-12-14).

When "git show" displays commits it needs to temporarily clobbers the "rev.pending" array, but by doing so we'll fail to release_revisions(), as we have for most other uses of "struct rev_info" since 2da81d1efb0 (Merge branch 'ab/plug-leak-in-revisions', 2022-06-07).

In the preceding commit this code was made to use a more extendable pattern, which we can now complete. Once we've clobbered our "rev.pending" and invoked "cmd_log_walk_no_free()" we need to "object_array_clear()" our newly created "rev.pending" to avoid leaking the memory related to the one member array we've created.

But more importantly we need to set "rev.pending" back to the original we squirreled away in the "cp" variable, so that we'll make use of the release_revisions() added in f6bfea0ad01 (revisions API users: use release_revisions() in builtin/log.c, 2022-04-13). In f6bfea0ad01 this memory leak was noted as an outstanding TODO, but it's now been fixed.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/log.c                                    | 2 ++
 t/t0203-gettext-setlocale-sanity.sh              | 1 +
 t/t1020-subdirectory.sh                          | 1 +
 t/t3307-notes-man.sh                             | 1 +
 t/t3920-crlf-messages.sh                         | 2 ++
 t/t4069-remerge-diff.sh                          | 1 +
 t/t7007-show.sh                                  | 1 +
 t/t7503-pre-commit-and-pre-merge-commit-hooks.sh | 1 +
 t/t9122-git-svn-author.sh                        | 1 -
 t/t9162-git-svn-dcommit-interactive.sh           | 1 -
 10 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index e0f40798d45..77ec256a8ae 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -747,6 +747,8 @@ int cmd_show(int argc, const char **argv, const char *prefix)
 			memcpy(&rev.pending, &blank, sizeof(rev.pending));
 			add_object_array(o, name, &rev.pending);
 			ret = cmd_log_walk_no_free(&rev);
+			object_array_clear(&rev.pending);
+			memcpy(&rev.pending, &cp, sizeof(rev.pending));
 			break;
 		default:
 			ret = error(_("unknown type: %d"), o->type);
diff --git a/t/t0203-gettext-setlocale-sanity.sh b/t/t0203-gettext-setlocale-sanity.sh
index 0ce1f22eff6..86cff324ff1 100755
--- a/t/t0203-gettext-setlocale-sanity.sh
+++ b/t/t0203-gettext-setlocale-sanity.sh
@@ -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' '
diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
index 9fdbb2af80e..45eef9457fe 100755
--- a/t/t1020-subdirectory.sh
+++ b/t/t1020-subdirectory.sh
@@ -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
 
diff --git a/t/t3307-notes-man.sh b/t/t3307-notes-man.sh
index 1aa366a410e..ae316502c45 100755
--- a/t/t3307-notes-man.sh
+++ b/t/t3307-notes-man.sh
@@ -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' '
diff --git a/t/t3920-crlf-messages.sh b/t/t3920-crlf-messages.sh
index 0276edbe3d3..4c661d4d54a 100755
--- a/t/t3920-crlf-messages.sh
+++ b/t/t3920-crlf-messages.sh
@@ -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=""
diff --git a/t/t4069-remerge-diff.sh b/t/t4069-remerge-diff.sh
index 35f94957fce..9e7cac68b1c 100755
--- a/t/t4069-remerge-diff.sh
+++ b/t/t4069-remerge-diff.sh
@@ -2,6 +2,7 @@
 
 test_description='remerge-diff handling'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # This test is ort-specific
diff --git a/t/t7007-show.sh b/t/t7007-show.sh
index d6cc69e0f2c..f908a4d1abc 100755
--- a/t/t7007-show.sh
+++ b/t/t7007-show.sh
@@ -2,6 +2,7 @@
 
 test_description='git show'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
diff --git a/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
index ad1eb64ba0d..aa004b70a8d 100755
--- a/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
+++ b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
@@ -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' '
diff --git a/t/t9122-git-svn-author.sh b/t/t9122-git-svn-author.sh
index 527ba3d2932..0fc289ae0f0 100755
--- a/t/t9122-git-svn-author.sh
+++ b/t/t9122-git-svn-author.sh
@@ -2,7 +2,6 @@
 
 test_description='git svn authorship'
 
-TEST_FAILS_SANITIZE_LEAK=true
 . ./lib-git-svn.sh
 
 test_expect_success 'setup svn repository' '
diff --git a/t/t9162-git-svn-dcommit-interactive.sh b/t/t9162-git-svn-dcommit-interactive.sh
index e2aa8ed88a9..b3ce033a0d3 100755
--- a/t/t9162-git-svn-dcommit-interactive.sh
+++ b/t/t9162-git-svn-dcommit-interactive.sh
@@ -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' '
-- 
2.37.0.932.g7b7031e73bc
Previous: Jeff KingNext: Jeff King
Message 6 of 52 in “revisions API: fix more memory leaks”
  1. 0/6 revisions API: fix more memory leaksÆvar Arnfjörð Bjarmason, Jul 13, 2022
  2. 1/6 bisect.c: add missing "goto" for release_revisions()Ævar Arnfjörð Bjarmason, Jul 13, 2022
  3. 2/6 test-fast-rebase helper: use release_revisions() (again)Ævar Arnfjörð Bjarmason, Jul 13, 2022
  4. 3/6 log: make the intent of cmd_show()'s "rev.pending" juggling clearerÆvar Arnfjörð Bjarmason, Jul 13, 2022
  5. Jeff KingJul 18, 2022
  6. 4/6 log: fix common "rev.pending" memory leak in "git show"Ævar Arnfjörð Bjarmason, Jul 13, 2022
  7. Jeff KingJul 18, 2022
  8. Ævar Arnfjörð BjarmasonJul 18, 2022
  9. 5/6 bisect.c: partially fix bisect_rev_setup() memory leakÆvar Arnfjörð Bjarmason, Jul 13, 2022
  10. Jeff KingJul 18, 2022
  11. 6/6 revisions API: don't leak memory on argv elements that need free()-ingÆvar Arnfjörð Bjarmason, Jul 13, 2022
  12. Jeff KingJul 18, 2022
  13. Ævar Arnfjörð BjarmasonJul 18, 2022
  14. Jeff KingJul 18, 2022
  15. Ævar Arnfjörð BjarmasonJul 29, 2022
  16. Jeff KingJul 29, 2022
  17. Jeff KingJul 29, 2022
  18. Junio C HamanoJul 29, 2022
  19. Jeff KingJul 29, 2022
  20. Jeff KingJul 18, 2022
  21. 0/6 revisions API: fix more memory leaksÆvar Arnfjörð Bjarmason, Jul 29, 2022
  22. 1/6 bisect.c: add missing "goto" for release_revisions()Ævar Arnfjörð Bjarmason, Jul 29, 2022
  23. 2/6 test-fast-rebase helper: use release_revisions() (again)Ævar Arnfjörð Bjarmason, Jul 29, 2022
  24. Eric SunshineJul 30, 2022
  25. 3/6 log: make the intent of cmd_show()'s "rev.pending" juggling clearerÆvar Arnfjörð Bjarmason, Jul 29, 2022
  26. Junio C HamanoJul 29, 2022
  27. 4/6 log: fix common "rev.pending" memory leak in "git show"Ævar Arnfjörð Bjarmason, Jul 29, 2022
  28. Jeff KingJul 29, 2022
  29. Jeff KingJul 29, 2022
  30. Jeff KingJul 29, 2022
  31. 5/6 bisect.c: partially fix bisect_rev_setup() memory leakÆvar Arnfjörð Bjarmason, Jul 29, 2022
  32. 6/6 revisions API: don't leak memory on argv elements that need free()-ingÆvar Arnfjörð Bjarmason, Jul 29, 2022
  33. Jeff KingJul 29, 2022
  34. Jeff KingJul 29, 2022
  35. 0/6 revisions API: fix more memory leaksÆvar Arnfjörð Bjarmason, Aug 2, 2022
  36. 1/6 bisect.c: add missing "goto" for release_revisions()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  37. Junio C HamanoAug 3, 2022
  38. 2/6 test-fast-rebase helper: use release_revisions() (again)Ævar Arnfjörð Bjarmason, Aug 2, 2022
  39. 3/6 log: fix a memory leak in "git show <revision>..."Ævar Arnfjörð Bjarmason, Aug 2, 2022
  40. Jeff KingAug 3, 2022
  41. Jeff KingAug 3, 2022
  42. Junio C HamanoAug 3, 2022
  43. 5/6 bisect.c: partially fix bisect_rev_setup() memory leakÆvar Arnfjörð Bjarmason, Aug 2, 2022
  44. 4/6 log: refactor "rev.pending" code in cmd_show()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  45. Jeff KingAug 3, 2022
  46. Junio C HamanoAug 3, 2022
  47. Ævar Arnfjörð BjarmasonAug 4, 2022
  48. Junio C HamanoAug 4, 2022
  49. Ævar Arnfjörð BjarmasonAug 5, 2022
  50. 6/6 revisions API: don't leak memory on argv elements that need free()-ingÆvar Arnfjörð Bjarmason, Aug 2, 2022
  51. Junio C HamanoAug 3, 2022
  52. Jeff KingAug 3, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.