]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-options: fix leaks for users of OPT_FILENAME
authorPatrick Steinhardt <ps@pks.im>
Tue, 11 Jun 2024 09:19:22 +0000 (11:19 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Jun 2024 20:15:04 +0000 (13:15 -0700)
The `OPT_FILENAME()` option will, if set, put an allocated string into
the user-provided variable. Consequently, that variable thus needs to be
free'd by the caller of `parse_options()`. Some callsites don't though
and thus leak memory. Fix those.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
21 files changed:
apply.c
apply.h
builtin/archive.c
builtin/commit.c
builtin/fmt-merge-msg.c
builtin/log.c
builtin/multi-pack-index.c
builtin/sparse-checkout.c
t/helper/test-parse-options.c
t/t1512-rev-parse-disambiguation.sh
t/t2500-untracked-overwriting.sh
t/t3406-rebase-message.sh
t/t3407-rebase-abort.sh
t/t3428-rebase-signoff.sh
t/t4131-apply-fake-ancestor.sh
t/t4151-am-abort.sh
t/t4253-am-keep-cr-dos.sh
t/t4255-am-submodule.sh
t/t5407-post-rewrite-hook.sh
t/t6427-diff3-conflict-markers.sh
t/t7512-status-help.sh

diff --git a/apply.c b/apply.c
index 901b67e6255af4174a1e5a00cf01de4d2545d659..d8d26a48f13974af4655d74d010b0a159e7270ce 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -135,6 +135,7 @@ void clear_apply_state(struct apply_state *state)
        strset_clear(&state->removed_symlinks);
        strset_clear(&state->kept_symlinks);
        strbuf_release(&state->root);
+       FREE_AND_NULL(state->fake_ancestor);
 
        /* &state->fn_table is cleared at the end of apply_patch() */
 }
diff --git a/apply.h b/apply.h
index 7cd38b1443c67e448bdc9f004f94a841a12eb747..36d7c3f70b45cf65255269b6f75ca10f6a1d6b85 100644 (file)
--- a/apply.h
+++ b/apply.h
@@ -59,7 +59,7 @@ struct apply_state {
        struct repository *repo;
        const char *index_file;
        enum apply_verbosity apply_verbosity;
-       const char *fake_ancestor;
+       char *fake_ancestor;
        const char *patch_input_file;
        int line_termination;
        struct strbuf root;
index 15ee1ec7bb765fe6307dc3f17cde238242169cc4..f29c0ef6ad3d4a375b107dd01a622d881e69f6e8 100644 (file)
@@ -92,6 +92,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
                        N_("path to the remote git-upload-archive command")),
                OPT_END()
        };
+       int ret;
 
        argc = parse_options(argc, argv, prefix, local_opts, NULL,
                             PARSE_OPT_KEEP_ALL);
@@ -106,6 +107,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 
        setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
 
-       UNLEAK(output);
-       return write_archive(argc, argv, prefix, the_repository, output, 0);
+       ret = write_archive(argc, argv, prefix, the_repository, output, 0);
+
+       free(output);
+       return ret;
 }
index f53e7e86ff61191266da0fc779e2ed8d24c93abe..dcaf4efa035abc8f16e2c46c1b00f13f1183d453 100644 (file)
@@ -106,7 +106,8 @@ static enum {
        COMMIT_PARTIAL
 } commit_style;
 
-static const char *logfile, *force_author;
+static const char *force_author;
+static char *logfile;
 static char *template_file;
 /*
  * The _message variables are commit names from which to take
@@ -1309,7 +1310,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
                                  !!use_message, "-C",
                                  !!logfile, "-F");
        if (use_message || edit_message || logfile ||fixup_message || have_option_m)
-               template_file = NULL;
+               FREE_AND_NULL(template_file);
        if (edit_message)
                use_message = edit_message;
        if (amend && !use_message && !fixup_message)
@@ -1892,5 +1893,7 @@ cleanup:
        strbuf_release(&author_ident);
        strbuf_release(&err);
        strbuf_release(&sb);
+       free(logfile);
+       free(template_file);
        return ret;
 }
index 0f9855b680eb7bf4e534af40847e32af835cfa13..957786d1b3ae7ba9b9d51b0b403c42bb00aba064 100644 (file)
@@ -11,7 +11,7 @@ static const char * const fmt_merge_msg_usage[] = {
 
 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 {
-       const char *inpath = NULL;
+       char *inpath = NULL;
        const char *message = NULL;
        char *into_name = NULL;
        int shortlog_len = -1;
@@ -66,5 +66,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
        if (ret)
                return ret;
        write_in_full(STDOUT_FILENO, output.buf, output.len);
+
+       free(inpath);
        return 0;
 }
index 78a247d8a94d077a4bd829b65228afb64e03e6dc..4e4b645a21de4ea5f398737cbe5635b688df3314 100644 (file)
@@ -2021,7 +2021,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        const char *rfc = NULL;
        int creation_factor = -1;
        const char *signature = git_version_string;
-       const char *signature_file_arg = NULL;
+       char *signature_file_arg = NULL;
        struct keep_callback_data keep_callback_data = {
                .cfg = &cfg,
                .revs = &rev,
@@ -2559,6 +2559,8 @@ done:
        strbuf_release(&rdiff1);
        strbuf_release(&rdiff2);
        strbuf_release(&rdiff_title);
+       free(description_file);
+       free(signature_file_arg);
        free(to_free);
        free(rev.message_id);
        if (rev.ref_message_ids)
index 8360932d2e75577dbb416c5851d33556d3688233..9cf1a32d6534074c00b21bb40b99e2e9a7dca24b 100644 (file)
@@ -50,7 +50,7 @@ static char const * const builtin_multi_pack_index_usage[] = {
 static struct opts_multi_pack_index {
        char *object_dir;
        const char *preferred_pack;
-       const char *refs_snapshot;
+       char *refs_snapshot;
        unsigned long batch_size;
        unsigned flags;
        int stdin_packs;
@@ -135,6 +135,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
                             N_("refs snapshot for selecting bitmap commits")),
                OPT_END(),
        };
+       int ret;
 
        opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
 
@@ -157,7 +158,6 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
        if (opts.stdin_packs) {
                struct string_list packs = STRING_LIST_INIT_DUP;
-               int ret;
 
                read_packs_from_stdin(&packs);
 
@@ -166,12 +166,17 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
                                           opts.refs_snapshot, opts.flags);
 
                string_list_clear(&packs, 0);
+               free(opts.refs_snapshot);
 
                return ret;
 
        }
-       return write_midx_file(opts.object_dir, opts.preferred_pack,
-                              opts.refs_snapshot, opts.flags);
+
+       ret = write_midx_file(opts.object_dir, opts.preferred_pack,
+                             opts.refs_snapshot, opts.flags);
+
+       free(opts.refs_snapshot);
+       return ret;
 }
 
 static int cmd_multi_pack_index_verify(int argc, const char **argv,
index 0f52e252493d0d56fd0bcf5a7b9f92a416e86348..84a6adf681fc8491720ac9c04cc0c80985e364b9 100644 (file)
@@ -1011,6 +1011,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
 
        ret = check_rules(&pl, check_rules_opts.null_termination);
        clear_pattern_list(&pl);
+       free(check_rules_opts.rules_file);
        return ret;
 }
 
index ded8116cc5119f9d22d2d083cb8fa189e3783904..5250913d99eba18a28878d3904cb7b2399670d02 100644 (file)
@@ -207,6 +207,7 @@ int cmd__parse_options(int argc, const char **argv)
        expect.strdup_strings = 1;
        string_list_clear(&expect, 0);
        string_list_clear(&list, 0);
+       free(file);
 
        return ret;
 }
index 70f1e0a998e103704373e2cc4ffd21e09c7c64b0..f9d68ce74ea0950455300c761b5345e9db300a09 100755 (executable)
@@ -23,6 +23,7 @@ one tagged as v1.0.0.  They all have one regular file each.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_cmp_failed_rev_parse () {
index 5c0bf4d21fcbb2a55e2c41c184ccb4c188c88ca4..714feb83be5b36640008a96921b4b091e798df88 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='Test handling of overwriting untracked files'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_setup_reset () {
index a1d7fa7f7c6965f49cc017303aca52a658211b10..82108b67e67c18230c385dfef03f7bdee37f9eb2 100755 (executable)
@@ -5,6 +5,7 @@ test_description='messages from rebase operation'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
index 9f49c4228b629589d7bfcf3435fda8e7c4b308be..2c3f38d45a848260995967cfce91a38cacfe82ee 100755 (executable)
@@ -5,6 +5,7 @@ test_description='git rebase --abort tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
index 6f57aed9fac68f1b5d670b2e3c35bcc84690a525..365436ebfc36415333d01bd231f7f89e5223477d 100755 (executable)
@@ -5,6 +5,7 @@ test_description='git rebase --signoff
 This test runs git rebase --signoff and make sure that it works.
 '
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-rebase.sh
 
index b1361ce54693a07486f5837b0fe477f828b80a4e..40c92115a66e83593ef4f9dcb879a0eabe1e625a 100755 (executable)
@@ -5,6 +5,7 @@
 
 test_description='git apply --build-fake-ancestor handling.'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
index edb38da7010d33315f02a345a076ef803d460a32..1825a89d6a98cbeb2e7e0ee9489c36bc57aaac03 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='am --abort'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
index 0ee69d2a0c4e0d21c14290f166eebd563f81be7c..2bcdd9f34fad676af2d0287c16964d2159cbbd5f 100755 (executable)
@@ -9,6 +9,7 @@ test_description='git-am mbox with dos line ending.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # Three patches which will be added as files with dos line ending.
index a7ba08f728c0b870bf899ee82220a1708699dcb0..04f3ccfc41cc6646027a57eaa1808c9ff76c0c3b 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git am handling submodules'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-submodule-update.sh
 
index ad7f8c6f00202c5ec844108b14a2c1301c185223..e99e728236ad07ed011d127fbfbb13126e7ea82c 100755 (executable)
@@ -7,6 +7,7 @@ test_description='Test the post-rewrite hook.'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
index dd5fe6a402196270fba03fadff55d1e39fa64f02..a13271b34902bcca25cb78e273233d1300ee0a27 100755 (executable)
@@ -5,6 +5,7 @@ test_description='recursive merge diff3 style conflict markers'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # Setup:
index 802f8f704c62eb11192bcc82ecd35f0ae53b5a6a..cdd5f2c697938839f0d15987a12edb656ee0a462 100755 (executable)
@@ -10,6 +10,7 @@ test_description='git status advice'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 . "$TEST_DIRECTORY"/lib-rebase.sh