Skip to content

Commit 149c9e2

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ff0935b commit 149c9e2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

archive.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
736736
struct pretty_print_describe_status describe_status = {0};
737737
struct pretty_print_context ctx = {0};
738738
struct archiver_args args;
739+
const char **argv_copy;
739740
int rc;
740741

741742
git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
@@ -749,6 +750,14 @@ int write_archive(int argc, const char **argv, const char *prefix,
749750
args.repo = repo;
750751
args.prefix = prefix;
751752
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+
752761
argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
753762
if (!startup_info->have_repository) {
754763
/*
@@ -767,6 +776,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
767776
string_list_clear_func(&args.extra_files, extra_file_info_clear);
768777
free(args.refname);
769778
clear_pathspec(&args.pathspec);
779+
free(argv_copy);
770780

771781
return rc;
772782
}

builtin/upload-archive.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
2222
{
2323
struct strvec sent_argv = STRVEC_INIT;
2424
const char *arg_cmd = "argument ";
25+
int ret;
2526

2627
if (argc != 2 || !strcmp(argv[1], "-h"))
2728
usage(upload_archive_usage);
@@ -46,8 +47,11 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
4647
}
4748

4849
/* 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;
5155
}
5256

5357
__attribute__((format (printf, 1, 2)))

0 commit comments

Comments
 (0)