Skip to content

Commit ccafbbf

Browse files
committed
Merge branch 'ab/plug-random-leaks'
Plug random memory leaks. * ab/plug-random-leaks: repository.c: free the "path cache" in repo_clear() range-diff: plug memory leak in read_patches() range-diff: plug memory leak in common invocation lockfile API users: simplify and don't leak "path" commit-graph: stop fill_oids_from_packs() progress on error and free() commit-graph: fix memory leak in misused string_list API submodule--helper: fix trivial leak in module_add() transport: stop needlessly copying bundle header references bundle: call strvec_clear() on allocated strvec remote-curl.c: free memory in cmd_main() urlmatch.c: add and use a *_release() function diff.c: free "buf" in diff_words_flush() merge-base: free() allocated "struct commit **" list index-pack: fix memory leaks
2 parents 4eb845a + 759f340 commit ccafbbf

21 files changed

+113
-62
lines changed

apply.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,18 @@ static void free_fragment_list(struct fragment *list)
219219
}
220220
}
221221

222-
static void free_patch(struct patch *patch)
222+
void release_patch(struct patch *patch)
223223
{
224224
free_fragment_list(patch->fragments);
225225
free(patch->def_name);
226226
free(patch->old_name);
227227
free(patch->new_name);
228228
free(patch->result);
229+
}
230+
231+
static void free_patch(struct patch *patch)
232+
{
233+
release_patch(patch);
229234
free(patch);
230235
}
231236

apply.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ int parse_git_diff_header(struct strbuf *root,
173173
unsigned int size,
174174
struct patch *patch);
175175

176+
void release_patch(struct patch *patch);
177+
176178
/*
177179
* Some aspects of the apply behavior are controlled by the following
178180
* bits in the "options" parameter passed to apply_all_patches().

builtin/bundle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
9393
if (!startup_info->have_repository)
9494
die(_("Need a repository to create a bundle."));
9595
ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
96+
strvec_clear(&pack_opts);
9697
free(bundle_file);
9798
return ret;
9899
}

builtin/commit-graph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static int git_commit_graph_write_config(const char *var, const char *value,
192192

193193
static int graph_write(int argc, const char **argv)
194194
{
195-
struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
195+
struct string_list pack_indexes = STRING_LIST_INIT_DUP;
196196
struct strbuf buf = STRBUF_INIT;
197197
struct oidset commits = OIDSET_INIT;
198198
struct object_directory *odb = NULL;
@@ -273,8 +273,8 @@ static int graph_write(int argc, const char **argv)
273273

274274
if (opts.stdin_packs) {
275275
while (strbuf_getline(&buf, stdin) != EOF)
276-
string_list_append(&pack_indexes,
277-
strbuf_detach(&buf, NULL));
276+
string_list_append_nodup(&pack_indexes,
277+
strbuf_detach(&buf, NULL));
278278
} else if (opts.stdin_commits) {
279279
oidset_init(&commits, 0);
280280
if (opts.progress)

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static int get_urlmatch(const char *var, const char *url)
612612

613613
strbuf_release(&matched->value);
614614
}
615-
string_list_clear(&config.vars, 1);
615+
urlmatch_config_release(&config);
616616
string_list_clear(&values, 1);
617617
free(config.url.url);
618618

builtin/index-pack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ static void *threaded_second_pass(void *data)
11131113
list_add(&child->list, &work_head);
11141114
base_cache_used += child->size;
11151115
prune_base_data(NULL);
1116+
free_base_data(child);
11161117
} else {
11171118
/*
11181119
* This child does not have its own children. It may be
@@ -1135,6 +1136,7 @@ static void *threaded_second_pass(void *data)
11351136

11361137
p = next_p;
11371138
}
1139+
FREE_AND_NULL(child);
11381140
}
11391141
work_unlock();
11401142
}
@@ -1428,6 +1430,7 @@ static void fix_unresolved_deltas(struct hashfile *f)
14281430
* object).
14291431
*/
14301432
append_obj_to_pack(f, d->oid.hash, data, size, type);
1433+
free(data);
14311434
threaded_second_pass(NULL);
14321435

14331436
display_progress(progress, nr_resolved_deltas);
@@ -1707,6 +1710,7 @@ static void show_pack_info(int stat_only)
17071710
i + 1,
17081711
chain_histogram[i]);
17091712
}
1713+
free(chain_histogram);
17101714
}
17111715

17121716
int cmd_index_pack(int argc, const char **argv, const char *prefix)
@@ -1936,6 +1940,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
19361940
if (do_fsck_object && fsck_finish(&fsck_options))
19371941
die(_("fsck error in pack objects"));
19381942

1943+
free(opts.anomaly);
19391944
free(objects);
19401945
strbuf_release(&index_name_buf);
19411946
strbuf_release(&rev_index_name_buf);

builtin/merge-base.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
138138
int rev_nr = 0;
139139
int show_all = 0;
140140
int cmdmode = 0;
141+
int ret;
141142

142143
struct option options[] = {
143144
OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
@@ -186,5 +187,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
186187
ALLOC_ARRAY(rev, argc);
187188
while (argc-- > 0)
188189
rev[rev_nr++] = get_commit_reference(*argv++);
189-
return show_merge_base(rev, rev_nr, show_all);
190+
ret = show_merge_base(rev, rev_nr, show_all);
191+
free(rev);
192+
return ret;
190193
}

builtin/sparse-checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ static int write_patterns_and_update(struct pattern_list *pl)
329329

330330
fd = hold_lock_file_for_update(&lk, sparse_filename,
331331
LOCK_DIE_ON_ERROR);
332+
free(sparse_filename);
332333

333334
result = update_working_directory(pl);
334335
if (result) {
335336
rollback_lock_file(&lk);
336-
free(sparse_filename);
337337
clear_pattern_list(pl);
338338
update_working_directory(NULL);
339339
return result;
@@ -349,7 +349,6 @@ static int write_patterns_and_update(struct pattern_list *pl)
349349
fflush(fp);
350350
commit_lock_file(&lk);
351351

352-
free(sparse_filename);
353352
clear_pattern_list(pl);
354353

355354
return 0;

builtin/submodule--helper.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3309,6 +3309,7 @@ static int module_add(int argc, const char **argv, const char *prefix)
33093309
{
33103310
int force = 0, quiet = 0, progress = 0, dissociate = 0;
33113311
struct add_data add_data = ADD_DATA_INIT;
3312+
char *to_free = NULL;
33123313

33133314
struct option options[] = {
33143315
OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
@@ -3360,7 +3361,8 @@ static int module_add(int argc, const char **argv, const char *prefix)
33603361
"of the working tree"));
33613362

33623363
/* dereference source url relative to parent's url */
3363-
add_data.realrepo = resolve_relative_url(add_data.repo, NULL, 1);
3364+
to_free = resolve_relative_url(add_data.repo, NULL, 1);
3365+
add_data.realrepo = to_free;
33643366
} else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
33653367
add_data.realrepo = add_data.repo;
33663368
} else {
@@ -3413,6 +3415,7 @@ static int module_add(int argc, const char **argv, const char *prefix)
34133415
}
34143416
configure_added_submodule(&add_data);
34153417
free(add_data.sm_path);
3418+
free(to_free);
34163419

34173420
return 0;
34183421
}

commit-graph.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,12 +1679,13 @@ int write_commit_graph_reachable(struct object_directory *odb,
16791679
}
16801680

16811681
static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1682-
struct string_list *pack_indexes)
1682+
const struct string_list *pack_indexes)
16831683
{
16841684
uint32_t i;
16851685
struct strbuf progress_title = STRBUF_INIT;
16861686
struct strbuf packname = STRBUF_INIT;
16871687
int dirlen;
1688+
int ret = 0;
16881689

16891690
strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
16901691
dirlen = packname.len;
@@ -1703,24 +1704,25 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
17031704
strbuf_addstr(&packname, pack_indexes->items[i].string);
17041705
p = add_packed_git(packname.buf, packname.len, 1);
17051706
if (!p) {
1706-
error(_("error adding pack %s"), packname.buf);
1707-
return -1;
1707+
ret = error(_("error adding pack %s"), packname.buf);
1708+
goto cleanup;
17081709
}
17091710
if (open_pack_index(p)) {
1710-
error(_("error opening index for %s"), packname.buf);
1711-
return -1;
1711+
ret = error(_("error opening index for %s"), packname.buf);
1712+
goto cleanup;
17121713
}
17131714
for_each_object_in_pack(p, add_packed_commits, ctx,
17141715
FOR_EACH_OBJECT_PACK_ORDER);
17151716
close_pack(p);
17161717
free(p);
17171718
}
17181719

1720+
cleanup:
17191721
stop_progress(&ctx->progress);
17201722
strbuf_release(&progress_title);
17211723
strbuf_release(&packname);
17221724

1723-
return 0;
1725+
return ret;
17241726
}
17251727

17261728
static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
@@ -1852,6 +1854,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
18521854

18531855
hold_lock_file_for_update_mode(&lk, lock_name,
18541856
LOCK_DIE_ON_ERROR, 0444);
1857+
free(lock_name);
18551858

18561859
fd = git_mkstemp_mode(ctx->graph_name, 0444);
18571860
if (fd < 0) {
@@ -1976,6 +1979,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
19761979
} else {
19771980
char *graph_name = get_commit_graph_filename(ctx->odb);
19781981
unlink(graph_name);
1982+
free(graph_name);
19791983
}
19801984

19811985
ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
@@ -2259,7 +2263,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
22592263
}
22602264

22612265
int write_commit_graph(struct object_directory *odb,
2262-
struct string_list *pack_indexes,
2266+
const struct string_list *const pack_indexes,
22632267
struct oidset *commits,
22642268
enum commit_graph_write_flags flags,
22652269
const struct commit_graph_opts *opts)

commit-graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int write_commit_graph_reachable(struct object_directory *odb,
142142
enum commit_graph_write_flags flags,
143143
const struct commit_graph_opts *opts);
144144
int write_commit_graph(struct object_directory *odb,
145-
struct string_list *pack_indexes,
145+
const struct string_list *pack_indexes,
146146
struct oidset *commits,
147147
enum commit_graph_write_flags flags,
148148
const struct commit_graph_opts *opts);

credential.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static void credential_apply_config(struct credential *c)
130130
git_config(urlmatch_config_entry, &config);
131131
string_list_clear(&config.vars, 1);
132132
free(normalized_url);
133+
urlmatch_config_release(&config);
133134
strbuf_release(&url);
134135

135136
c->configured = 1;

diff.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,7 @@ static void diff_words_flush(struct emit_callback *ecbdata)
21502150

21512151
for (i = 0; i < wol->nr; i++)
21522152
free((void *)wol->buf[i].line);
2153+
free(wol->buf);
21532154

21542155
wol->nr = 0;
21552156
}

path.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,6 @@ void report_linked_checkout_garbage(void);
169169
return r->cached_paths.var; \
170170
}
171171

172-
struct path_cache {
173-
const char *squash_msg;
174-
const char *merge_msg;
175-
const char *merge_rr;
176-
const char *merge_mode;
177-
const char *merge_head;
178-
const char *merge_autostash;
179-
const char *auto_merge;
180-
const char *fetch_head;
181-
const char *shallow;
182-
};
183-
184-
#define PATH_CACHE_INIT { 0 }
185-
186172
const char *git_path_squash_msg(struct repository *r);
187173
const char *git_path_merge_msg(struct repository *r);
188174
const char *git_path_merge_rr(struct repository *r);

range-diff.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static int read_patches(const char *range, struct string_list *list,
4040
char *line, *current_filename = NULL;
4141
ssize_t len;
4242
size_t size;
43+
int ret = -1;
4344

4445
strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
4546
"--reverse", "--date-order", "--decorate=no",
@@ -68,10 +69,10 @@ static int read_patches(const char *range, struct string_list *list,
6869
if (strbuf_read(&contents, cp.out, 0) < 0) {
6970
error_errno(_("could not read `log` output"));
7071
finish_command(&cp);
71-
return -1;
72+
goto cleanup;
7273
}
7374
if (finish_command(&cp))
74-
return -1;
75+
goto cleanup;
7576

7677
line = contents.buf;
7778
size = contents.len;
@@ -95,12 +96,9 @@ static int read_patches(const char *range, struct string_list *list,
9596
CALLOC_ARRAY(util, 1);
9697
if (get_oid(p, &util->oid)) {
9798
error(_("could not parse commit '%s'"), p);
98-
free(util);
99-
free(current_filename);
99+
FREE_AND_NULL(util);
100100
string_list_clear(list, 1);
101-
strbuf_release(&buf);
102-
strbuf_release(&contents);
103-
return -1;
101+
goto cleanup;
104102
}
105103
util->matching = -1;
106104
in_header = 1;
@@ -111,11 +109,8 @@ static int read_patches(const char *range, struct string_list *list,
111109
error(_("could not parse first line of `log` output: "
112110
"did not start with 'commit ': '%s'"),
113111
line);
114-
free(current_filename);
115112
string_list_clear(list, 1);
116-
strbuf_release(&buf);
117-
strbuf_release(&contents);
118-
return -1;
113+
goto cleanup;
119114
}
120115

121116
if (starts_with(line, "diff --git")) {
@@ -136,12 +131,9 @@ static int read_patches(const char *range, struct string_list *list,
136131
if (len < 0) {
137132
error(_("could not parse git header '%.*s'"),
138133
orig_len, line);
139-
free(util);
140-
free(current_filename);
134+
FREE_AND_NULL(util);
141135
string_list_clear(list, 1);
142-
strbuf_release(&buf);
143-
strbuf_release(&contents);
144-
return -1;
136+
goto cleanup;
145137
}
146138
strbuf_addstr(&buf, " ## ");
147139
if (patch.is_new > 0)
@@ -165,6 +157,7 @@ static int read_patches(const char *range, struct string_list *list,
165157
patch.old_mode, patch.new_mode);
166158

167159
strbuf_addstr(&buf, " ##");
160+
release_patch(&patch);
168161
} else if (in_header) {
169162
if (starts_with(line, "Author: ")) {
170163
strbuf_addstr(&buf, " ## Metadata ##\n");
@@ -218,14 +211,17 @@ static int read_patches(const char *range, struct string_list *list,
218211
strbuf_addch(&buf, '\n');
219212
util->diffsize++;
220213
}
214+
215+
ret = 0;
216+
cleanup:
221217
strbuf_release(&contents);
222218

223219
if (util)
224220
string_list_append(list, buf.buf)->util = util;
225221
strbuf_release(&buf);
226222
free(current_filename);
227223

228-
return 0;
224+
return ret;
229225
}
230226

231227
static int patch_util_cmp(const void *dummy, const struct patch_util *a,

0 commit comments

Comments
 (0)