]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/upload-archive: fix leaking args passed to `write_archive()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 22 Aug 2024 09:17:27 +0000 (11:17 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2024 16:18:04 +0000 (09:18 -0700)
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>
archive.c
builtin/upload-archive.c

index 7bd60d0632a41c7cd029497185ca75e93570ccba..9ba96aae4f742def878768548907b65f5e32c692 100644 (file)
--- a/archive.c
+++ b/archive.c
@@ -736,6 +736,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
        struct pretty_print_describe_status describe_status = {0};
        struct pretty_print_context ctx = {0};
        struct archiver_args args;
+       const char **argv_copy;
        int rc;
 
        git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
@@ -749,6 +750,14 @@ int write_archive(int argc, const char **argv, const char *prefix,
        args.repo = repo;
        args.prefix = prefix;
        string_list_init_dup(&args.extra_files);
+
+       /*
+        * `parse_archive_args()` modifies contents of `argv`, which is what we
+        * want. Our callers may not want it though, so we create a copy here.
+        */
+       DUP_ARRAY(argv_copy, argv, argc);
+       argv = argv_copy;
+
        argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
        if (!startup_info->have_repository) {
                /*
@@ -767,6 +776,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
        string_list_clear_func(&args.extra_files, extra_file_info_clear);
        free(args.refname);
        clear_pathspec(&args.pathspec);
+       free(argv_copy);
 
        return rc;
 }
index 1b09e5e1aa3f08e91bf6de731b111864ef405573..313a8dfa81c9941f2c705d06201e6ca2f4ed4e64 100644 (file)
@@ -22,6 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
 {
        struct strvec sent_argv = STRVEC_INIT;
        const char *arg_cmd = "argument ";
+       int ret;
 
        if (argc != 2 || !strcmp(argv[1], "-h"))
                usage(upload_archive_usage);
@@ -46,8 +47,11 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
        }
 
        /* parse all options sent by the client */
-       return write_archive(sent_argv.nr, sent_argv.v, prefix,
-                            the_repository, NULL, 1);
+       ret = write_archive(sent_argv.nr, sent_argv.v, prefix,
+                           the_repository, NULL, 1);
+
+       strvec_clear(&sent_argv);
+       return ret;
 }
 
 __attribute__((format (printf, 1, 2)))