{"thread":{"id":"57368","subject":"[PATCH] hash-object: fix a trivial leak in --path","startedAt":"2022-02-05T00:04:42Z","lastAt":"2022-02-07T01:57:30Z","messageCount":2,"participants":["Ævar Arnfjörð Bjarmason","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"447806","messageId":"patch-1.1-53863df1455-20220205T000422Z-avarab@gmail.com","threadId":"57368","inReplyTo":null,"subject":"[PATCH] hash-object: fix a trivial leak in --path","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-02-05T00:04:29Z","receivedAt":"2022-02-05T00:04:42Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Fix a memory leak that happened when the --path option was\nprovided. This leak has been with us ever since the option was added\nin 39702431500 (add --path option to git hash-object, 2008-08-03).\n\nWe can now mark \"t1007-hash-object.sh\" as passing when git is compiled\nwith SANITIZE=leak. It'll now run in the the\n\"GIT_TEST_PASSING_SANITIZE_LEAK=true\" test mode (the \"linux-leaks\" CI\ntarget).\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n builtin/hash-object.c  | 9 +++++++--\n t/t1007-hash-object.sh | 1 +\n 2 files changed, 8 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/hash-object.c b/builtin/hash-object.c\nindex c7b3ad74c60..db9b2535271 100644\n--- a/builtin/hash-object.c\n+++ b/builtin/hash-object.c\n@@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n \tint nongit = 0;\n \tunsigned flags = HASH_FORMAT_CHECK;\n \tconst char *vpath = NULL;\n+\tchar *vpath_free = NULL;\n \tconst struct option hash_object_options[] = {\n \t\tOPT_STRING('t', NULL, &type, N_(\"type\"), N_(\"object type\")),\n \t\tOPT_BIT('w', NULL, &flags, N_(\"write the object into the object database\"),\n@@ -114,8 +115,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n \telse\n \t\tprefix = setup_git_directory_gently(&nongit);\n \n-\tif (vpath && prefix)\n-\t\tvpath = prefix_filename(prefix, vpath);\n+\tif (vpath && prefix) {\n+\t\tvpath_free = prefix_filename(prefix, vpath);\n+\t\tvpath = vpath_free;\n+\t}\n \n \tgit_config(git_default_config, NULL);\n \n@@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n \tif (stdin_paths)\n \t\thash_stdin_paths(type, no_filters, flags, literally);\n \n+\tfree(vpath_free);\n+\n \treturn 0;\n }\ndiff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh\nindex 64b340f2272..ac5ad8c7402 100755\n--- a/t/t1007-hash-object.sh\n+++ b/t/t1007-hash-object.sh\n@@ -2,6 +2,7 @@\n \n test_description=\"git hash-object\"\n \n+TEST_PASSES_SANITIZE_LEAK=true\n . ./test-lib.sh\n \n echo_without_newline() {\n-- \n2.35.1.945.g180f8b8dd92\n\n"},{"id":"447861","messageId":"xmqq4k5bigl8.fsf@gitster.g","threadId":"57368","inReplyTo":"patch-1.1-53863df1455-20220205T000422Z-avarab@gmail.com","subject":"Re: [PATCH] hash-object: fix a trivial leak in --path","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-02-07T01:57:23Z","receivedAt":"2022-02-07T01:57:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:\n\n> diff --git a/builtin/hash-object.c b/builtin/hash-object.c\n> index c7b3ad74c60..db9b2535271 100644\n> --- a/builtin/hash-object.c\n> +++ b/builtin/hash-object.c\n> @@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n>  \tint nongit = 0;\n>  \tunsigned flags = HASH_FORMAT_CHECK;\n>  \tconst char *vpath = NULL;\n> +\tchar *vpath_free = NULL;\n>  \tconst struct option hash_object_options[] = {\n>  \t\tOPT_STRING('t', NULL, &type, N_(\"type\"), N_(\"object type\")),\n>  \t\tOPT_BIT('w', NULL, &flags, N_(\"write the object into the object database\"),\n> @@ -114,8 +115,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n>  \telse\n>  \t\tprefix = setup_git_directory_gently(&nongit);\n>  \n> -\tif (vpath && prefix)\n> -\t\tvpath = prefix_filename(prefix, vpath);\n> +\tif (vpath && prefix) {\n> +\t\tvpath_free = prefix_filename(prefix, vpath);\n> +\t\tvpath = vpath_free;\n> +\t}\n>  \n>  \tgit_config(git_default_config, NULL);\n>  \n> @@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n>  \tif (stdin_paths)\n>  \t\thash_stdin_paths(type, no_filters, flags, literally);\n>  \n> +\tfree(vpath_free);\n> +\n\nHeh.\n\nThis is not wrong per-se, but we are about to exit and clean-up, so\nhaving leak-checker complain about this and having us spend brain\ncycles to worry about it is a sad thing.\n\nWill queue.  Thanks.\n\n\n>  \treturn 0;\n>  }\n> diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh\n> index 64b340f2272..ac5ad8c7402 100755\n> --- a/t/t1007-hash-object.sh\n> +++ b/t/t1007-hash-object.sh\n> @@ -2,6 +2,7 @@\n>  \n>  test_description=\"git hash-object\"\n>  \n> +TEST_PASSES_SANITIZE_LEAK=true\n>  . ./test-lib.sh\n>  \n>  echo_without_newline() {\n"}]}