Skip to content

Commit 31f9acf

Browse files
committed
Merge branch 'ah/plugleaks'
Leak plugging. * ah/plugleaks: reset: clear_unpack_trees_porcelain to plug leak builtin/rebase: fix options.strategy memory lifecycle builtin/merge: free found_ref when done builtin/mv: free or UNLEAK multiple pointers at end of cmd_mv convert: release strbuf to avoid leak read-cache: call diff_setup_done to avoid leak ref-filter: also free head for ATOM_HEAD to avoid leak diffcore-rename: move old_dir/new_dir definition to plug leak builtin/for-each-repo: remove unnecessary argv copy to plug leak builtin/submodule--helper: release unused strbuf to avoid leak environment: move strbuf into block to plug leak fmt-merge-msg: free newly allocated temporary strings when done
2 parents 10f57e0 + 9a863b3 commit 31f9acf

File tree

12 files changed

+42
-27
lines changed

12 files changed

+42
-27
lines changed

builtin/for-each-repo.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ static const char * const for_each_repo_usage[] = {
1010
NULL
1111
};
1212

13-
static int run_command_on_repo(const char *path,
14-
void *cbdata)
13+
static int run_command_on_repo(const char *path, int argc, const char ** argv)
1514
{
1615
int i;
1716
struct child_process child = CHILD_PROCESS_INIT;
18-
struct strvec *args = (struct strvec *)cbdata;
1917

2018
child.git_cmd = 1;
2119
strvec_pushl(&child.args, "-C", path, NULL);
2220

23-
for (i = 0; i < args->nr; i++)
24-
strvec_push(&child.args, args->v[i]);
21+
for (i = 0; i < argc; i++)
22+
strvec_push(&child.args, argv[i]);
2523

2624
return run_command(&child);
2725
}
@@ -31,7 +29,6 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
3129
static const char *config_key = NULL;
3230
int i, result = 0;
3331
const struct string_list *values;
34-
struct strvec args = STRVEC_INIT;
3532

3633
const struct option options[] = {
3734
OPT_STRING(0, "config", &config_key, N_("config"),
@@ -45,9 +42,6 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
4542
if (!config_key)
4643
die(_("missing --config=<config>"));
4744

48-
for (i = 0; i < argc; i++)
49-
strvec_push(&args, argv[i]);
50-
5145
values = repo_config_get_value_multi(the_repository,
5246
config_key);
5347

@@ -59,7 +53,7 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
5953
return 0;
6054

6155
for (i = 0; !result && i < values->nr; i++)
62-
result = run_command_on_repo(values->items[i].string, &args);
56+
result = run_command_on_repo(values->items[i].string, argc, argv);
6357

6458
return result;
6559
}

builtin/merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
503503
struct strbuf bname = STRBUF_INIT;
504504
struct merge_remote_desc *desc;
505505
const char *ptr;
506-
char *found_ref;
506+
char *found_ref = NULL;
507507
int len, early;
508508

509509
strbuf_branchname(&bname, remote, 0);
@@ -586,6 +586,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
586586
strbuf_addf(msg, "%s\t\tcommit '%s'\n",
587587
oid_to_hex(&remote_head->object.oid), remote);
588588
cleanup:
589+
free(found_ref);
589590
strbuf_release(&buf);
590591
strbuf_release(&bname);
591592
}

builtin/mv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
303303
COMMIT_LOCK | SKIP_IF_UNCHANGED))
304304
die(_("Unable to write new index file"));
305305

306+
string_list_clear(&src_for_dst, 0);
307+
UNLEAK(source);
308+
UNLEAK(dest_path);
309+
free(submodule_gitfile);
310+
free(modes);
306311
return 0;
307312
}

builtin/rebase.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
139139
replay.ignore_date = opts->ignore_date;
140140
replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
141141
if (opts->strategy)
142-
replay.strategy = opts->strategy;
142+
replay.strategy = xstrdup_or_null(opts->strategy);
143143
else if (!replay.strategy && replay.default_strategy) {
144144
replay.strategy = replay.default_strategy;
145145
replay.default_strategy = NULL;
@@ -2109,6 +2109,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
21092109
free(options.head_name);
21102110
free(options.gpg_sign_opt);
21112111
free(options.cmd);
2112+
free(options.strategy);
21122113
strbuf_release(&options.git_format_patch_opt);
21132114
free(squash_onto_name);
21142115
return ret;

builtin/submodule--helper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,13 @@ static char *relative_url(const char *remote_url,
187187
out = xstrdup(sb.buf + 2);
188188
else
189189
out = xstrdup(sb.buf);
190-
strbuf_reset(&sb);
191190

192-
if (!up_path || !is_relative)
191+
if (!up_path || !is_relative) {
192+
strbuf_release(&sb);
193193
return out;
194+
}
194195

196+
strbuf_reset(&sb);
195197
strbuf_addf(&sb, "%s%s", up_path, out);
196198
free(out);
197199
return strbuf_detach(&sb, NULL);

convert.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
916916
else
917917
strbuf_swap(dst, &nbuf);
918918
strbuf_release(&nbuf);
919+
strbuf_release(&filter_status);
919920
return !err;
920921
}
921922

@@ -966,6 +967,7 @@ int async_query_available_blobs(const char *cmd, struct string_list *available_p
966967

967968
if (err)
968969
handle_filter_error(&filter_status, entry, 0);
970+
strbuf_release(&filter_status);
969971
return !err;
970972
}
971973

diffcore-rename.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ static void update_dir_rename_counts(struct dir_rename_info *info,
448448
const char *oldname,
449449
const char *newname)
450450
{
451-
char *old_dir = xstrdup(oldname);
452-
char *new_dir = xstrdup(newname);
453-
char new_dir_first_char = new_dir[0];
451+
char *old_dir;
452+
char *new_dir;
453+
const char new_dir_first_char = newname[0];
454454
int first_time_in_loop = 1;
455455

456456
if (!info->setup)
@@ -475,6 +475,10 @@ static void update_dir_rename_counts(struct dir_rename_info *info,
475475
*/
476476
return;
477477

478+
479+
old_dir = xstrdup(oldname);
480+
new_dir = xstrdup(newname);
481+
478482
while (1) {
479483
int drd_flag = NOT_RELEVANT;
480484

environment.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,20 @@ static int git_work_tree_initialized;
253253
*/
254254
void set_git_work_tree(const char *new_work_tree)
255255
{
256-
struct strbuf realpath = STRBUF_INIT;
257-
258256
if (git_work_tree_initialized) {
257+
struct strbuf realpath = STRBUF_INIT;
258+
259259
strbuf_realpath(&realpath, new_work_tree, 1);
260260
new_work_tree = realpath.buf;
261261
if (strcmp(new_work_tree, the_repository->worktree))
262262
die("internal error: work tree has already been set\n"
263263
"Current worktree: %s\nNew worktree: %s",
264264
the_repository->worktree, new_work_tree);
265+
strbuf_release(&realpath);
265266
return;
266267
}
267268
git_work_tree_initialized = 1;
268269
repo_set_worktree(the_repository, new_work_tree);
269-
270-
strbuf_release(&realpath);
271270
}
272271

273272
const char *get_git_work_tree(void)

fmt-merge-msg.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
108108
struct origin_data *origin_data;
109109
char *src;
110110
const char *origin, *tag_name;
111+
char *to_free = NULL;
111112
struct src_data *src_data;
112113
struct string_list_item *item;
113114
int pulling_head = 0;
@@ -183,12 +184,13 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
183184
if (!strcmp(".", src) || !strcmp(src, origin)) {
184185
int len = strlen(origin);
185186
if (origin[0] == '\'' && origin[len - 1] == '\'')
186-
origin = xmemdupz(origin + 1, len - 2);
187+
origin = to_free = xmemdupz(origin + 1, len - 2);
187188
} else
188-
origin = xstrfmt("%s of %s", origin, src);
189+
origin = to_free = xstrfmt("%s of %s", origin, src);
189190
if (strcmp(".", src))
190191
origin_data->is_local_branch = 0;
191192
string_list_append(&origins, origin)->util = origin_data;
193+
free(to_free);
192194
return 0;
193195
}
194196

read-cache.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,7 @@ int repo_index_has_changes(struct repository *repo,
25062506
opt.flags.exit_with_status = 1;
25072507
if (!sb)
25082508
opt.flags.quick = 1;
2509+
diff_setup_done(&opt);
25092510
do_diff_cache(&cmp, &opt);
25102511
diffcore_std(&opt);
25112512
for (i = 0; sb && i < diff_queued_diff.nr; i++) {

ref-filter.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,8 +2226,12 @@ void ref_array_clear(struct ref_array *array)
22262226
FREE_AND_NULL(array->items);
22272227
array->nr = array->alloc = 0;
22282228

2229-
for (i = 0; i < used_atom_cnt; i++)
2230-
free((char *)used_atom[i].name);
2229+
for (i = 0; i < used_atom_cnt; i++) {
2230+
struct used_atom *atom = &used_atom[i];
2231+
if (atom->atom_type == ATOM_HEAD)
2232+
free(atom->u.head);
2233+
free((char *)atom->name);
2234+
}
22312235
FREE_AND_NULL(used_atom);
22322236
used_atom_cnt = 0;
22332237

reset.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
2121
struct object_id head_oid;
2222
struct tree_desc desc[2] = { { NULL }, { NULL } };
2323
struct lock_file lock = LOCK_INIT;
24-
struct unpack_trees_options unpack_tree_opts;
24+
struct unpack_trees_options unpack_tree_opts = { 0 };
2525
struct tree *tree;
2626
const char *reflog_action;
2727
struct strbuf msg = STRBUF_INIT;
@@ -49,7 +49,6 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
4949
if (refs_only)
5050
goto reset_head_refs;
5151

52-
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
5352
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
5453
unpack_tree_opts.head_idx = 1;
5554
unpack_tree_opts.src_index = r->index;
@@ -134,6 +133,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
134133
leave_reset_head:
135134
strbuf_release(&msg);
136135
rollback_lock_file(&lock);
136+
clear_unpack_trees_porcelain(&unpack_tree_opts);
137137
while (nr)
138138
free((void *)desc[--nr].buffer);
139139
return ret;

0 commit comments

Comments
 (0)