Skip to content

Commit 14da262

Browse files
pks-tgitster
authored andcommitted
parse-options: fix leaks for users of OPT_FILENAME
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56931c4 commit 14da262

21 files changed

+41
-11
lines changed

apply.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ void clear_apply_state(struct apply_state *state)
135135
strset_clear(&state->removed_symlinks);
136136
strset_clear(&state->kept_symlinks);
137137
strbuf_release(&state->root);
138+
FREE_AND_NULL(state->fake_ancestor);
138139

139140
/* &state->fn_table is cleared at the end of apply_patch() */
140141
}

apply.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct apply_state {
5959
struct repository *repo;
6060
const char *index_file;
6161
enum apply_verbosity apply_verbosity;
62-
const char *fake_ancestor;
62+
char *fake_ancestor;
6363
const char *patch_input_file;
6464
int line_termination;
6565
struct strbuf root;

builtin/archive.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
9292
N_("path to the remote git-upload-archive command")),
9393
OPT_END()
9494
};
95+
int ret;
9596

9697
argc = parse_options(argc, argv, prefix, local_opts, NULL,
9798
PARSE_OPT_KEEP_ALL);
@@ -106,6 +107,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
106107

107108
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
108109

109-
UNLEAK(output);
110-
return write_archive(argc, argv, prefix, the_repository, output, 0);
110+
ret = write_archive(argc, argv, prefix, the_repository, output, 0);
111+
112+
free(output);
113+
return ret;
111114
}

builtin/commit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ static enum {
106106
COMMIT_PARTIAL
107107
} commit_style;
108108

109-
static const char *logfile, *force_author;
109+
static const char *force_author;
110+
static char *logfile;
110111
static char *template_file;
111112
/*
112113
* 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[],
13091310
!!use_message, "-C",
13101311
!!logfile, "-F");
13111312
if (use_message || edit_message || logfile ||fixup_message || have_option_m)
1312-
template_file = NULL;
1313+
FREE_AND_NULL(template_file);
13131314
if (edit_message)
13141315
use_message = edit_message;
13151316
if (amend && !use_message && !fixup_message)
@@ -1892,5 +1893,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18921893
strbuf_release(&author_ident);
18931894
strbuf_release(&err);
18941895
strbuf_release(&sb);
1896+
free(logfile);
1897+
free(template_file);
18951898
return ret;
18961899
}

builtin/fmt-merge-msg.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static const char * const fmt_merge_msg_usage[] = {
1111

1212
int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
1313
{
14-
const char *inpath = NULL;
14+
char *inpath = NULL;
1515
const char *message = NULL;
1616
char *into_name = NULL;
1717
int shortlog_len = -1;
@@ -66,5 +66,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
6666
if (ret)
6767
return ret;
6868
write_in_full(STDOUT_FILENO, output.buf, output.len);
69+
70+
free(inpath);
6971
return 0;
7072
}

builtin/log.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
20212021
const char *rfc = NULL;
20222022
int creation_factor = -1;
20232023
const char *signature = git_version_string;
2024-
const char *signature_file_arg = NULL;
2024+
char *signature_file_arg = NULL;
20252025
struct keep_callback_data keep_callback_data = {
20262026
.cfg = &cfg,
20272027
.revs = &rev,
@@ -2559,6 +2559,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
25592559
strbuf_release(&rdiff1);
25602560
strbuf_release(&rdiff2);
25612561
strbuf_release(&rdiff_title);
2562+
free(description_file);
2563+
free(signature_file_arg);
25622564
free(to_free);
25632565
free(rev.message_id);
25642566
if (rev.ref_message_ids)

builtin/multi-pack-index.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static char const * const builtin_multi_pack_index_usage[] = {
5050
static struct opts_multi_pack_index {
5151
char *object_dir;
5252
const char *preferred_pack;
53-
const char *refs_snapshot;
53+
char *refs_snapshot;
5454
unsigned long batch_size;
5555
unsigned flags;
5656
int stdin_packs;
@@ -135,6 +135,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
135135
N_("refs snapshot for selecting bitmap commits")),
136136
OPT_END(),
137137
};
138+
int ret;
138139

139140
opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
140141

@@ -157,7 +158,6 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
157158

158159
if (opts.stdin_packs) {
159160
struct string_list packs = STRING_LIST_INIT_DUP;
160-
int ret;
161161

162162
read_packs_from_stdin(&packs);
163163

@@ -166,12 +166,17 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
166166
opts.refs_snapshot, opts.flags);
167167

168168
string_list_clear(&packs, 0);
169+
free(opts.refs_snapshot);
169170

170171
return ret;
171172

172173
}
173-
return write_midx_file(opts.object_dir, opts.preferred_pack,
174-
opts.refs_snapshot, opts.flags);
174+
175+
ret = write_midx_file(opts.object_dir, opts.preferred_pack,
176+
opts.refs_snapshot, opts.flags);
177+
178+
free(opts.refs_snapshot);
179+
return ret;
175180
}
176181

177182
static int cmd_multi_pack_index_verify(int argc, const char **argv,

builtin/sparse-checkout.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
10111011

10121012
ret = check_rules(&pl, check_rules_opts.null_termination);
10131013
clear_pattern_list(&pl);
1014+
free(check_rules_opts.rules_file);
10141015
return ret;
10151016
}
10161017

t/helper/test-parse-options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ int cmd__parse_options(int argc, const char **argv)
207207
expect.strdup_strings = 1;
208208
string_list_clear(&expect, 0);
209209
string_list_clear(&list, 0);
210+
free(file);
210211

211212
return ret;
212213
}

t/t1512-rev-parse-disambiguation.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ one tagged as v1.0.0. They all have one regular file each.
2323
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
2424
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
2525

26+
TEST_PASSES_SANITIZE_LEAK=true
2627
. ./test-lib.sh
2728

2829
test_cmp_failed_rev_parse () {

0 commit comments

Comments
 (0)