Skip to content

Commit 37766b6

Browse files
avargitster
authored andcommitted
tag: use a "goto cleanup" pattern, leak less memory
Change cmd_tag() to free its "struct strbuf"'s instead of using an UNLEAK() pattern. This changes code added in 886e108 (builtin/: add UNLEAKs, 2017-10-01). As shown in the context of the declaration of the "struct msg_arg" (which I'm changing to use a designated initializer while at it, and to show the context in this change), that struct is just a thin wrapper around an int and "struct strbuf". Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d530dc commit 37766b6

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

builtin/tag.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
432432
int annotate = 0, force = 0;
433433
int cmdmode = 0, create_tag_object = 0;
434434
const char *msgfile = NULL, *keyid = NULL;
435-
struct msg_arg msg = { 0, STRBUF_INIT };
435+
struct msg_arg msg = { .buf = STRBUF_INIT };
436436
struct ref_transaction *transaction;
437437
struct strbuf err = STRBUF_INIT;
438438
struct ref_filter filter;
@@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
482482
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
483483
OPT_END()
484484
};
485+
int ret = 0;
485486

486487
setup_ref_filter_porcelain_msg();
487488

@@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
529530
ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
530531
filter.ignore_case = icase;
531532
if (cmdmode == 'l') {
532-
int ret;
533533
if (column_active(colopts)) {
534534
struct column_options copts;
535535
memset(&copts, 0, sizeof(copts));
@@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
540540
ret = list_tags(&filter, sorting, &format);
541541
if (column_active(colopts))
542542
stop_column_filter();
543-
return ret;
543+
goto cleanup;
544544
}
545545
if (filter.lines != -1)
546546
die(_("-n option is only allowed in list mode"));
@@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
552552
die(_("--points-at option is only allowed in list mode"));
553553
if (filter.reachable_from || filter.unreachable_from)
554554
die(_("--merged and --no-merged options are only allowed in list mode"));
555-
if (cmdmode == 'd')
556-
return delete_tags(argv);
555+
if (cmdmode == 'd') {
556+
ret = delete_tags(argv);
557+
goto cleanup;
558+
}
557559
if (cmdmode == 'v') {
558560
if (format.format && verify_ref_format(&format))
559561
usage_with_options(git_tag_usage, options);
560-
return for_each_tag_name(argv, verify_tag, &format);
562+
ret = for_each_tag_name(argv, verify_tag, &format);
563+
goto cleanup;
561564
}
562565

563566
if (msg.given || msgfile) {
@@ -626,10 +629,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
626629
printf(_("Updated tag '%s' (was %s)\n"), tag,
627630
find_unique_abbrev(&prev, DEFAULT_ABBREV));
628631

629-
UNLEAK(buf);
630-
UNLEAK(ref);
631-
UNLEAK(reflog_msg);
632-
UNLEAK(msg);
633-
UNLEAK(err);
634-
return 0;
632+
cleanup:
633+
strbuf_release(&buf);
634+
strbuf_release(&ref);
635+
strbuf_release(&reflog_msg);
636+
strbuf_release(&msg.buf);
637+
strbuf_release(&err);
638+
return ret;
635639
}

0 commit comments

Comments
 (0)