* [PATCH] hash-object: fix a trivial leak in --path
@ 2022-02-05 0:04 Ævar Arnfjörð Bjarmason
2022-02-07 1:57 ` Junio C Hamano
0 siblings, 1 reply; 2+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-02-05 0:04 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Dmitry Potapov,
Ævar Arnfjörð Bjarmason
Fix a memory leak that happened when the --path option was
provided. This leak has been with us ever since the option was added
in 39702431500 (add --path option to git hash-object, 2008-08-03).
We can now mark "t1007-hash-object.sh" as passing when git is compiled
with SANITIZE=leak. It'll now run in the the
"GIT_TEST_PASSING_SANITIZE_LEAK=true" test mode (the "linux-leaks" CI
target).
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
builtin/hash-object.c | 9 +++++++--
t/t1007-hash-object.sh | 1 +
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index c7b3ad74c60..db9b2535271 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
int nongit = 0;
unsigned flags = HASH_FORMAT_CHECK;
const char *vpath = NULL;
+ char *vpath_free = NULL;
const struct option hash_object_options[] = {
OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
@@ -114,8 +115,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
else
prefix = setup_git_directory_gently(&nongit);
- if (vpath && prefix)
- vpath = prefix_filename(prefix, vpath);
+ if (vpath && prefix) {
+ vpath_free = prefix_filename(prefix, vpath);
+ vpath = vpath_free;
+ }
git_config(git_default_config, NULL);
@@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
if (stdin_paths)
hash_stdin_paths(type, no_filters, flags, literally);
+ free(vpath_free);
+
return 0;
}
diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh
index 64b340f2272..ac5ad8c7402 100755
--- a/t/t1007-hash-object.sh
+++ b/t/t1007-hash-object.sh
@@ -2,6 +2,7 @@
test_description="git hash-object"
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
echo_without_newline() {
--
2.35.1.945.g180f8b8dd92
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] hash-object: fix a trivial leak in --path
2022-02-05 0:04 [PATCH] hash-object: fix a trivial leak in --path Ævar Arnfjörð Bjarmason
@ 2022-02-07 1:57 ` Junio C Hamano
0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2022-02-07 1:57 UTC (permalink / raw)
To: Ævar Arnfjörð Bjarmason; +Cc: git, Dmitry Potapov
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> diff --git a/builtin/hash-object.c b/builtin/hash-object.c
> index c7b3ad74c60..db9b2535271 100644
> --- a/builtin/hash-object.c
> +++ b/builtin/hash-object.c
> @@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
> int nongit = 0;
> unsigned flags = HASH_FORMAT_CHECK;
> const char *vpath = NULL;
> + char *vpath_free = NULL;
> const struct option hash_object_options[] = {
> OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
> OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
> @@ -114,8 +115,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
> else
> prefix = setup_git_directory_gently(&nongit);
>
> - if (vpath && prefix)
> - vpath = prefix_filename(prefix, vpath);
> + if (vpath && prefix) {
> + vpath_free = prefix_filename(prefix, vpath);
> + vpath = vpath_free;
> + }
>
> git_config(git_default_config, NULL);
>
> @@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
> if (stdin_paths)
> hash_stdin_paths(type, no_filters, flags, literally);
>
> + free(vpath_free);
> +
Heh.
This is not wrong per-se, but we are about to exit and clean-up, so
having leak-checker complain about this and having us spend brain
cycles to worry about it is a sad thing.
Will queue. Thanks.
> return 0;
> }
> diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh
> index 64b340f2272..ac5ad8c7402 100755
> --- a/t/t1007-hash-object.sh
> +++ b/t/t1007-hash-object.sh
> @@ -2,6 +2,7 @@
>
> test_description="git hash-object"
>
> +TEST_PASSES_SANITIZE_LEAK=true
> . ./test-lib.sh
>
> echo_without_newline() {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-02-07 1:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-05 0:04 [PATCH] hash-object: fix a trivial leak in --path Ævar Arnfjörð Bjarmason
2022-02-07 1:57 ` Junio C Hamano
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).