Skip to content

Commit 8c1c63d

Browse files
committed
Merge branch 'ps/leakfixes-part-5'
Even more leak fixes. * ps/leakfixes-part-5: transport: fix leaking negotiation tips transport: fix leaking arguments when fetching from bundle builtin/fetch: fix leaking transaction with `--atomic` remote: fix leaking peer ref when expanding refmap remote: fix leaks when matching refspecs remote: fix leaking config strings builtin/fetch-pack: fix leaking refs sideband: fix leaks when configuring sideband colors builtin/send-pack: fix leaking refspecs transport: fix leaking OID arrays in git:// transport data t/helper: fix leaking multi-pack-indices in "read-midx" builtin/repack: fix leaks when computing packs to repack midx-write: fix leaking hashfile on error cases builtin/archive: fix leaking `OPT_FILENAME()` value builtin/upload-archive: fix leaking args passed to `write_archive()` builtin/merge-tree: fix leaking `-X` strategy options pretty: fix leaking key/value separator buffer pretty: fix memory leaks when parsing pretty formats convert: fix leaks when resetting attributes mailinfo: fix leaking header data
2 parents f123c19 + 13b23d2 commit 8c1c63d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+237
-73
lines changed

archive.c

+10
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
736736
struct pretty_print_describe_status describe_status = {0};
737737
struct pretty_print_context ctx = {0};
738738
struct archiver_args args;
739+
const char **argv_copy;
739740
int rc;
740741

741742
git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
@@ -749,6 +750,14 @@ int write_archive(int argc, const char **argv, const char *prefix,
749750
args.repo = repo;
750751
args.prefix = prefix;
751752
string_list_init_dup(&args.extra_files);
753+
754+
/*
755+
* `parse_archive_args()` modifies contents of `argv`, which is what we
756+
* want. Our callers may not want it though, so we create a copy here.
757+
*/
758+
DUP_ARRAY(argv_copy, argv, argc);
759+
argv = argv_copy;
760+
752761
argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
753762
if (!startup_info->have_repository) {
754763
/*
@@ -767,6 +776,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
767776
string_list_clear_func(&args.extra_files, extra_file_info_clear);
768777
free(args.refname);
769778
clear_pathspec(&args.pathspec);
779+
free(argv_copy);
770780

771781
return rc;
772782
}

builtin/archive.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
100100
if (output)
101101
create_output_file(output);
102102

103-
if (remote)
104-
return run_remote_archiver(argc, argv, remote, exec, output);
103+
if (remote) {
104+
ret = run_remote_archiver(argc, argv, remote, exec, output);
105+
goto out;
106+
}
105107

106108
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
107109

108110
ret = write_archive(argc, argv, prefix, the_repository, output, 0);
109111

112+
out:
110113
free(output);
111114
return ret;
112115
}

builtin/bundle.c

+2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
221221
&extra_index_pack_args, 0) ||
222222
list_bundle_refs(&header, argc, argv);
223223
bundle_header_release(&header);
224+
224225
cleanup:
226+
strvec_clear(&extra_index_pack_args);
225227
free(bundle_file);
226228
return ret;
227229
}

builtin/fetch-pack.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
4646
int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
4747
{
4848
int i, ret;
49-
struct ref *ref = NULL;
49+
struct ref *fetched_refs = NULL, *remote_refs = NULL;
5050
const char *dest = NULL;
5151
struct ref **sought = NULL;
5252
int nr_sought = 0, alloc_sought = 0;
@@ -228,19 +228,20 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
228228
version = discover_version(&reader);
229229
switch (version) {
230230
case protocol_v2:
231-
get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL,
231+
get_remote_refs(fd[1], &reader, &remote_refs, 0, NULL, NULL,
232232
args.stateless_rpc);
233233
break;
234234
case protocol_v1:
235235
case protocol_v0:
236-
get_remote_heads(&reader, &ref, 0, NULL, &shallow);
236+
get_remote_heads(&reader, &remote_refs, 0, NULL, &shallow);
237237
break;
238238
case protocol_unknown_version:
239239
BUG("unknown protocol version");
240240
}
241241

242-
ref = fetch_pack(&args, fd, ref, sought, nr_sought,
242+
fetched_refs = fetch_pack(&args, fd, remote_refs, sought, nr_sought,
243243
&shallow, pack_lockfiles_ptr, version);
244+
244245
if (pack_lockfiles.nr) {
245246
int i;
246247

@@ -260,7 +261,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
260261
if (finish_connect(conn))
261262
return 1;
262263

263-
ret = !ref;
264+
ret = !fetched_refs;
264265

265266
/*
266267
* If the heads to pull were given, we should have consumed
@@ -270,11 +271,14 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
270271
*/
271272
ret |= report_unmatched_refs(sought, nr_sought);
272273

273-
while (ref) {
274+
for (struct ref *ref = fetched_refs; ref; ref = ref->next)
274275
printf("%s %s\n",
275276
oid_to_hex(&ref->old_oid), ref->name);
276-
ref = ref->next;
277-
}
278277

278+
for (size_t i = 0; i < nr_sought; i++)
279+
free_one_ref(sought[i]);
280+
free(sought);
281+
free_refs(fetched_refs);
282+
free_refs(remote_refs);
279283
return ret;
280284
}

builtin/fetch.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1731,11 +1731,8 @@ static int do_fetch(struct transport *transport,
17311731
goto cleanup;
17321732

17331733
retcode = ref_transaction_commit(transaction, &err);
1734-
if (retcode) {
1735-
ref_transaction_free(transaction);
1736-
transaction = NULL;
1734+
if (retcode)
17371735
goto cleanup;
1738-
}
17391736
}
17401737

17411738
commit_fetch_head(&fetch_head);
@@ -1803,8 +1800,11 @@ static int do_fetch(struct transport *transport,
18031800
if (transaction && ref_transaction_abort(transaction, &err) &&
18041801
err.len)
18051802
error("%s", err.buf);
1803+
transaction = NULL;
18061804
}
18071805

1806+
if (transaction)
1807+
ref_transaction_free(transaction);
18081808
display_state_release(&display_state);
18091809
close_fetch_head(&fetch_head);
18101810
strbuf_release(&err);

builtin/merge-tree.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
533533
int expected_remaining_argc;
534534
int original_argc;
535535
const char *merge_base = NULL;
536+
int ret;
536537

537538
const char * const merge_tree_usage[] = {
538539
N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -625,7 +626,9 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
625626
strbuf_list_free(split);
626627
}
627628
strbuf_release(&buf);
628-
return 0;
629+
630+
ret = 0;
631+
goto out;
629632
}
630633

631634
/* Figure out which mode to use */
@@ -664,7 +667,11 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
664667

665668
/* Do the relevant type of merge */
666669
if (o.mode == MODE_REAL)
667-
return real_merge(&o, merge_base, argv[0], argv[1], prefix);
670+
ret = real_merge(&o, merge_base, argv[0], argv[1], prefix);
668671
else
669-
return trivial_merge(argv[0], argv[1], argv[2]);
672+
ret = trivial_merge(argv[0], argv[1], argv[2]);
673+
674+
out:
675+
strvec_clear(&xopts);
676+
return ret;
670677
}

builtin/repack.c

+27-9
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,23 @@ static void midx_included_packs(struct string_list *include,
732732
struct pack_geometry *geometry)
733733
{
734734
struct string_list_item *item;
735+
struct strbuf buf = STRBUF_INIT;
736+
737+
for_each_string_list_item(item, &existing->kept_packs) {
738+
strbuf_reset(&buf);
739+
strbuf_addf(&buf, "%s.idx", item->string);
740+
string_list_insert(include, buf.buf);
741+
}
742+
743+
for_each_string_list_item(item, names) {
744+
strbuf_reset(&buf);
745+
strbuf_addf(&buf, "pack-%s.idx", item->string);
746+
string_list_insert(include, buf.buf);
747+
}
735748

736-
for_each_string_list_item(item, &existing->kept_packs)
737-
string_list_insert(include, xstrfmt("%s.idx", item->string));
738-
for_each_string_list_item(item, names)
739-
string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
740749
if (geometry->split_factor) {
741-
struct strbuf buf = STRBUF_INIT;
742750
uint32_t i;
751+
743752
for (i = geometry->split; i < geometry->pack_nr; i++) {
744753
struct packed_git *p = geometry->pack[i];
745754

@@ -754,17 +763,21 @@ static void midx_included_packs(struct string_list *include,
754763
if (!p->pack_local)
755764
continue;
756765

766+
strbuf_reset(&buf);
757767
strbuf_addstr(&buf, pack_basename(p));
758768
strbuf_strip_suffix(&buf, ".pack");
759769
strbuf_addstr(&buf, ".idx");
760770

761-
string_list_insert(include, strbuf_detach(&buf, NULL));
771+
string_list_insert(include, buf.buf);
762772
}
763773
} else {
764774
for_each_string_list_item(item, &existing->non_kept_packs) {
765775
if (pack_is_marked_for_deletion(item))
766776
continue;
767-
string_list_insert(include, xstrfmt("%s.idx", item->string));
777+
778+
strbuf_reset(&buf);
779+
strbuf_addf(&buf, "%s.idx", item->string);
780+
string_list_insert(include, buf.buf);
768781
}
769782
}
770783

@@ -784,8 +797,13 @@ static void midx_included_packs(struct string_list *include,
784797
*/
785798
if (pack_is_marked_for_deletion(item))
786799
continue;
787-
string_list_insert(include, xstrfmt("%s.idx", item->string));
800+
801+
strbuf_reset(&buf);
802+
strbuf_addf(&buf, "%s.idx", item->string);
803+
string_list_insert(include, buf.buf);
788804
}
805+
806+
strbuf_release(&buf);
789807
}
790808

791809
static int write_midx_included_packs(struct string_list *include,
@@ -1476,7 +1494,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
14761494
mark_packs_for_deletion(&existing, &names);
14771495

14781496
if (write_midx) {
1479-
struct string_list include = STRING_LIST_INIT_NODUP;
1497+
struct string_list include = STRING_LIST_INIT_DUP;
14801498
midx_included_packs(&include, &existing, &names, &geometry);
14811499

14821500
ret = write_midx_included_packs(&include, &geometry, &names,

builtin/send-pack.c

+1
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,6 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
338338

339339
free_refs(remote_refs);
340340
free_refs(local_refs);
341+
refspec_clear(&rs);
341342
return ret;
342343
}

builtin/upload-archive.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
2222
{
2323
struct strvec sent_argv = STRVEC_INIT;
2424
const char *arg_cmd = "argument ";
25+
int ret;
2526

2627
if (argc != 2 || !strcmp(argv[1], "-h"))
2728
usage(upload_archive_usage);
@@ -46,8 +47,11 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
4647
}
4748

4849
/* parse all options sent by the client */
49-
return write_archive(sent_argv.nr, sent_argv.v, prefix,
50-
the_repository, NULL, 1);
50+
ret = write_archive(sent_argv.nr, sent_argv.v, prefix,
51+
the_repository, NULL, 1);
52+
53+
strvec_clear(&sent_argv);
54+
return ret;
5155
}
5256

5357
__attribute__((format (printf, 1, 2)))

bundle.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,8 @@ int unbundle(struct repository *r, struct bundle_header *header,
644644
if (flags & VERIFY_BUNDLE_FSCK)
645645
strvec_push(&ip.args, "--fsck-objects");
646646

647-
if (extra_index_pack_args) {
647+
if (extra_index_pack_args)
648648
strvec_pushv(&ip.args, extra_index_pack_args->v);
649-
strvec_clear(extra_index_pack_args);
650-
}
651649

652650
ip.in = bundle_fd;
653651
ip.no_stdout = 1;

convert.c

+3
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,9 @@ void reset_parsed_attributes(void)
13711371
for (drv = user_convert; drv; drv = next) {
13721372
next = drv->next;
13731373
free((void *)drv->name);
1374+
free((void *)drv->smudge);
1375+
free((void *)drv->clean);
1376+
free((void *)drv->process);
13741377
free(drv);
13751378
}
13761379
user_convert = NULL;

mailinfo.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,21 @@ void clear_mailinfo(struct mailinfo *mi)
12921292
strbuf_release(&mi->inbody_header_accum);
12931293
free(mi->message_id);
12941294

1295-
strbuf_list_free(mi->p_hdr_data);
1296-
strbuf_list_free(mi->s_hdr_data);
1295+
for (size_t i = 0; header[i]; i++) {
1296+
if (!mi->p_hdr_data[i])
1297+
continue;
1298+
strbuf_release(mi->p_hdr_data[i]);
1299+
free(mi->p_hdr_data[i]);
1300+
}
1301+
free(mi->p_hdr_data);
1302+
1303+
for (size_t i = 0; header[i]; i++) {
1304+
if (!mi->s_hdr_data[i])
1305+
continue;
1306+
strbuf_release(mi->s_hdr_data[i]);
1307+
free(mi->s_hdr_data[i]);
1308+
}
1309+
free(mi->s_hdr_data);
12971310

12981311
while (mi->content < mi->content_top) {
12991312
free(*(mi->content_top));

midx-write.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,18 @@ static int write_midx_internal(const char *object_dir,
13061306
pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
13071307
(pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
13081308

1309+
if (ctx.nr - dropped_packs == 0) {
1310+
error(_("no pack files to index."));
1311+
result = 1;
1312+
goto cleanup;
1313+
}
1314+
1315+
if (!ctx.entries_nr) {
1316+
if (flags & MIDX_WRITE_BITMAP)
1317+
warning(_("refusing to write multi-pack .bitmap without any objects"));
1318+
flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1319+
}
1320+
13091321
if (ctx.incremental) {
13101322
struct strbuf lock_name = STRBUF_INIT;
13111323

@@ -1331,18 +1343,6 @@ static int write_midx_internal(const char *object_dir,
13311343
f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
13321344
}
13331345

1334-
if (ctx.nr - dropped_packs == 0) {
1335-
error(_("no pack files to index."));
1336-
result = 1;
1337-
goto cleanup;
1338-
}
1339-
1340-
if (!ctx.entries_nr) {
1341-
if (flags & MIDX_WRITE_BITMAP)
1342-
warning(_("refusing to write multi-pack .bitmap without any objects"));
1343-
flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1344-
}
1345-
13461346
cf = init_chunkfile(f);
13471347

13481348
add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,

0 commit comments

Comments
 (0)