builtin/archive: fix leaking `OPT_FILENAME()` value

The "--output" switch is an `OPT_FILENAME()` option, which allocates memory when specified by the user. But while we free the string when executed without the "--remote" switch, we don't otherwise because we return via a separate exit path that doesn't know to free it. Fix this by creating a common exit path. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 22, 2024 at 11:17 UTC 479601e9f4708a921e9bfad3a73e931cbed8817a
3 files changed +7 -2
builtin/archive.c
+5 -2
@@ -100,13 +100,16 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
100 if (output)
101 create_output_file(output);
102
103 - if (remote)
104 - return run_remote_archiver(argc, argv, remote, exec, output);
103 + if (remote) {
104 + ret = run_remote_archiver(argc, argv, remote, exec, output);
105 + goto out;
106 + }
107
108 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
109
110 ret = write_archive(argc, argv, prefix, the_repository, output, 0);
111
112 +out:
113 free(output);
114 return ret;
115 }
t/t5000-tar-tree.sh
+1
@@ -25,6 +25,7 @@ commit id embedding:
25 '
26
27 TEST_CREATE_REPO_NO_TEMPLATE=1
28 +TEST_PASSES_SANITIZE_LEAK=true
29 . ./test-lib.sh
30
31 SUBSTFORMAT=%H%n
t/t5003-archive-zip.sh
+1
@@ -3,6 +3,7 @@
3 test_description='git archive --format=zip test'
4
5 TEST_CREATE_REPO_NO_TEMPLATE=1
6 +TEST_PASSES_SANITIZE_LEAK=true
7 . ./test-lib.sh
8
9 SUBSTFORMAT=%H%n