From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 10/11] cat-file: fix a common "struct object_context" memory leak
Date: Thu, 30 Jun 2022 20:00:20 +0200 [thread overview]
Message-ID: <patch-10.11-9474e2bcf93-20220630T175714Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-00.11-00000000000-20220630T175714Z-avarab@gmail.com>
Fix a memory leak where "cat-file" will leak the "path" member. See
e5fba602e59 (textconv: support for cat_file, 2010-06-15) for the code
that introduced the offending get_oid_with_context() call (called
get_sha1_with_context() at the time).
As a result we can mark several tests as passing with SANITIZE=leak
using "TEST_PASSES_SANITIZE_LEAK=true".
As noted in dc944b65f1d (get_sha1_with_context: dynamically allocate
oc->path, 2017-05-19) callers must free the "path" member. That same
commit added the relevant free() to this function, but we weren't
catching cases where we'd return early.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
builtin/cat-file.c | 32 +++++++++++++++++++---------
t/t0028-working-tree-encoding.sh | 1 +
t/t1051-large-conversion.sh | 2 ++
t/t3304-notes-mixed.sh | 1 +
t/t4044-diff-index-unique-abbrev.sh | 2 ++
t/t4140-apply-ita.sh | 1 +
t/t5314-pack-cycle-detection.sh | 4 ++--
t/t6422-merge-rename-corner-cases.sh | 1 +
t/t8007-cat-file-textconv.sh | 2 ++
t/t8010-cat-file-filters.sh | 2 ++
t/t9104-git-svn-follow-parent.sh | 1 -
t/t9132-git-svn-broken-symlink.sh | 1 -
t/t9301-fast-import-notes.sh | 1 +
13 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index ac0459f96be..f42782e955f 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -71,6 +71,7 @@ static int stream_blob(const struct object_id *oid)
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
int unknown_type)
{
+ int ret;
struct object_id oid;
enum object_type type;
char *buf;
@@ -106,7 +107,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
if (sb.len) {
printf("%s\n", sb.buf);
strbuf_release(&sb);
- return 0;
+ ret = 0;
+ goto cleanup;
}
break;
@@ -115,7 +117,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
die("git cat-file: could not get object info");
printf("%"PRIuMAX"\n", (uintmax_t)size);
- return 0;
+ ret = 0;
+ goto cleanup;
case 'e':
return !has_object_file(&oid);
@@ -123,8 +126,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
case 'w':
if (filter_object(path, obj_context.mode,
- &oid, &buf, &size))
- return -1;
+ &oid, &buf, &size)) {
+ ret = -1;
+ goto cleanup;
+ }
break;
case 'c':
@@ -143,11 +148,14 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
const char *ls_args[3] = { NULL };
ls_args[0] = "ls-tree";
ls_args[1] = obj_name;
- return cmd_ls_tree(2, ls_args, NULL);
+ ret = cmd_ls_tree(2, ls_args, NULL);
+ goto cleanup;
}
- if (type == OBJ_BLOB)
- return stream_blob(&oid);
+ if (type == OBJ_BLOB) {
+ ret = stream_blob(&oid);
+ goto cleanup;
+ }
buf = read_object_file(&oid, &type, &size);
if (!buf)
die("Cannot read object %s", obj_name);
@@ -172,8 +180,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
} else
oidcpy(&blob_oid, &oid);
- if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
- return stream_blob(&blob_oid);
+ if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) {
+ ret = stream_blob(&blob_oid);
+ goto cleanup;
+ }
/*
* we attempted to dereference a tag to a blob
* and failed; there may be new dereference
@@ -193,9 +203,11 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
die("git cat-file %s: bad file", obj_name);
write_or_die(1, buf, size);
+ ret = 0;
+cleanup:
free(buf);
free(obj_context.path);
- return 0;
+ return ret;
}
struct expand_data {
diff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh
index 82905a2156f..416eeabdb94 100755
--- a/t/t0028-working-tree-encoding.sh
+++ b/t/t0028-working-tree-encoding.sh
@@ -5,6 +5,7 @@ test_description='working-tree-encoding conversion via gitattributes'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
. "$TEST_DIRECTORY/lib-encoding.sh"
diff --git a/t/t1051-large-conversion.sh b/t/t1051-large-conversion.sh
index 042b0e44292..f6709c9f569 100755
--- a/t/t1051-large-conversion.sh
+++ b/t/t1051-large-conversion.sh
@@ -1,6 +1,8 @@
#!/bin/sh
test_description='test conversion filters on large files'
+
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
set_attr() {
diff --git a/t/t3304-notes-mixed.sh b/t/t3304-notes-mixed.sh
index 03dfcd3954c..2c3a2452668 100755
--- a/t/t3304-notes-mixed.sh
+++ b/t/t3304-notes-mixed.sh
@@ -5,6 +5,7 @@ test_description='Test notes trees that also contain non-notes'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
number_of_commits=100
diff --git a/t/t4044-diff-index-unique-abbrev.sh b/t/t4044-diff-index-unique-abbrev.sh
index 4701796d10e..29e49d22902 100755
--- a/t/t4044-diff-index-unique-abbrev.sh
+++ b/t/t4044-diff-index-unique-abbrev.sh
@@ -1,6 +1,8 @@
#!/bin/sh
test_description='test unique sha1 abbreviation on "index from..to" line'
+
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup' '
diff --git a/t/t4140-apply-ita.sh b/t/t4140-apply-ita.sh
index c614eaf04cc..b375aca0d74 100755
--- a/t/t4140-apply-ita.sh
+++ b/t/t4140-apply-ita.sh
@@ -2,6 +2,7 @@
test_description='git apply of i-t-a file'
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success setup '
diff --git a/t/t5314-pack-cycle-detection.sh b/t/t5314-pack-cycle-detection.sh
index 0aec8619e22..73a241743aa 100755
--- a/t/t5314-pack-cycle-detection.sh
+++ b/t/t5314-pack-cycle-detection.sh
@@ -49,9 +49,9 @@ Then no matter which order we start looking at the packs in, we know that we
will always find a delta for "file", because its lookup will always come
immediately after the lookup for "dummy".
'
-. ./test-lib.sh
-
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
# Create a pack containing the tree $1 and blob $1:file, with
# the latter stored as a delta against $2:file.
diff --git a/t/t6422-merge-rename-corner-cases.sh b/t/t6422-merge-rename-corner-cases.sh
index bf4ce3c63d4..9b65768aed6 100755
--- a/t/t6422-merge-rename-corner-cases.sh
+++ b/t/t6422-merge-rename-corner-cases.sh
@@ -6,6 +6,7 @@ test_description="recursive merge corner cases w/ renames but not criss-crosses"
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-merge.sh
diff --git a/t/t8007-cat-file-textconv.sh b/t/t8007-cat-file-textconv.sh
index b067983ba1c..c8266f17f14 100755
--- a/t/t8007-cat-file-textconv.sh
+++ b/t/t8007-cat-file-textconv.sh
@@ -1,6 +1,8 @@
#!/bin/sh
test_description='git cat-file textconv support'
+
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
cat >helper <<'EOF'
diff --git a/t/t8010-cat-file-filters.sh b/t/t8010-cat-file-filters.sh
index 31de4b64dc0..ca04242ca01 100755
--- a/t/t8010-cat-file-filters.sh
+++ b/t/t8010-cat-file-filters.sh
@@ -1,6 +1,8 @@
#!/bin/sh
test_description='git cat-file filters support'
+
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup ' '
diff --git a/t/t9104-git-svn-follow-parent.sh b/t/t9104-git-svn-follow-parent.sh
index 5cf2ef4b8b0..85d735861fc 100755
--- a/t/t9104-git-svn-follow-parent.sh
+++ b/t/t9104-git-svn-follow-parent.sh
@@ -5,7 +5,6 @@
test_description='git svn fetching'
-TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh
test_expect_success 'initialize repo' '
diff --git a/t/t9132-git-svn-broken-symlink.sh b/t/t9132-git-svn-broken-symlink.sh
index 4d8d0584b79..aeceffaf7b0 100755
--- a/t/t9132-git-svn-broken-symlink.sh
+++ b/t/t9132-git-svn-broken-symlink.sh
@@ -2,7 +2,6 @@
test_description='test that git handles an svn repository with empty symlinks'
-TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh
test_expect_success 'load svn dumpfile' '
svnadmin load "$rawsvnrepo" <<EOF
diff --git a/t/t9301-fast-import-notes.sh b/t/t9301-fast-import-notes.sh
index 1ae4d7c0d37..58413221e6a 100755
--- a/t/t9301-fast-import-notes.sh
+++ b/t/t9301-fast-import-notes.sh
@@ -7,6 +7,7 @@ test_description='test git fast-import of notes objects'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
--
2.37.0.874.g7d3439f13c4
next prev parent reply other threads:[~2022-06-30 18:00 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-30 18:00 [PATCH 00/11] built-ins: fix common memory leaks Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 01/11] check-ref-format: fix trivial memory leak Ævar Arnfjörð Bjarmason
2022-06-30 21:55 ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 02/11] clone: fix memory leak in copy_ref() call Ævar Arnfjörð Bjarmason
2022-06-30 22:03 ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 03/11] submodule.c: free() memory from xgetcwd() Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 04/11] revert: free "struct replay_opts" members Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 05/11] cat-file: fix a memory leak in --batch-command mode Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 06/11] merge-file: refactor for subsequent memory leak fix Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 07/11] merge-file: fix memory leaks on error path Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 08/11] checkout: add a missing clear_unpack_trees_porcelain() Ævar Arnfjörð Bjarmason
2022-06-30 22:20 ` Junio C Hamano
2022-06-30 23:34 ` Ævar Arnfjörð Bjarmason
2022-06-30 23:45 ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 09/11] gc: fix a memory leak Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` Ævar Arnfjörð Bjarmason [this message]
2022-06-30 18:00 ` [PATCH 11/11] pull: fix a "struct oid_array" " Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 00/11] built-ins: fix common memory leaks Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 01/11] check-ref-format: fix trivial memory leak Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 02/11] clone: fix memory leak in wanted_peer_refs() Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 03/11] submodule.c: free() memory from xgetcwd() Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 04/11] revert: free "struct replay_opts" members Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 05/11] cat-file: fix a memory leak in --batch-command mode Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 06/11] merge-file: refactor for subsequent memory leak fix Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 07/11] merge-file: fix memory leaks on error path Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 08/11] checkout: avoid "struct unpack_trees_options" leak Ævar Arnfjörð Bjarmason
2022-07-01 20:25 ` Junio C Hamano
2022-07-01 10:42 ` [PATCH v2 09/11] gc: fix a memory leak Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 10/11] cat-file: fix a common "struct object_context" " Ævar Arnfjörð Bjarmason
2022-07-01 10:43 ` [PATCH v2 11/11] pull: fix a "struct oid_array" " Ævar Arnfjörð Bjarmason
2022-07-01 18:58 ` [PATCH v2 00/11] built-ins: fix common memory leaks Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=patch-10.11-9474e2bcf93-20220630T175714Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).