builtin/upload-archive: fix leaking args passed to `write_archive()`
In git-upload-archive(1), we pass an array of arguments to `write_archive()` to tell it what exactly to do. We don't ever clear the vector though, causing a memory leak. Furthermore though, the call to `write_archive()` may cause contents of the array to be modified, which would cause us to leak memory to allocated strings held by it. Fix the issue by having `write_archive()` create a shallow copy of `argv` before parsing the arguments. Like this, we won't modify the caller's array and can easily `strvec_clear()` it to plug these memory leaks. 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
149c9e200c18316432a99a9b5edfa0d0b4d50bc5
2 files changed
+16
-2
archive.c
+10
@@ -736,6 +736,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
736
struct pretty_print_describe_status describe_status = {0};
737
struct pretty_print_context ctx = {0};
738
struct archiver_args args;
739
+ const char **argv_copy;
740
int rc;
741
742
git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
@@ -749,6 +750,14 @@ int write_archive(int argc, const char **argv, const char *prefix,
750
args.repo = repo;
751
args.prefix = prefix;
752
string_list_init_dup(&args.extra_files);
753
+
754
+ /*
755
+ * `parse_archive_args()` modifies contents of `argv`, which is what we
756
+ * want. Our callers may not want it though, so we create a copy here.
757
+ */
758
+ DUP_ARRAY(argv_copy, argv, argc);
759
+ argv = argv_copy;
760
+
761
argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
762
if (!startup_info->have_repository) {
763
/*
@@ -767,6 +776,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
776
string_list_clear_func(&args.extra_files, extra_file_info_clear);
777
free(args.refname);
778
clear_pathspec(&args.pathspec);
779
+ free(argv_copy);
780
781
return rc;
782
}
builtin/upload-archive.c
+6
-2
@@ -22,6 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
22
{
23
struct strvec sent_argv = STRVEC_INIT;
24
const char *arg_cmd = "argument ";
25
+ int ret;
26
27
if (argc != 2 || !strcmp(argv[1], "-h"))
28
usage(upload_archive_usage);
@@ -46,8 +47,11 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
47
}
48
49
/* parse all options sent by the client */
49
- return write_archive(sent_argv.nr, sent_argv.v, prefix,
50
- the_repository, NULL, 1);
50
+ ret = write_archive(sent_argv.nr, sent_argv.v, prefix,
51
+ the_repository, NULL, 1);
52
+
53
+ strvec_clear(&sent_argv);
54
+ return ret;
55
}
56
57
__attribute__((format (printf, 1, 2)))