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 886e1084d78 (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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 20, 2021 at 20:27 UTC 37766b61cdb3e12709febcd2f9dbee2b90e62969
1 file changed +16 -12
builtin/tag.c
+16 -12
@@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
432 int annotate = 0, force = 0;
433 int cmdmode = 0, create_tag_object = 0;
434 const char *msgfile = NULL, *keyid = NULL;
435 - struct msg_arg msg = { 0, STRBUF_INIT };
435 + struct msg_arg msg = { .buf = STRBUF_INIT };
436 struct ref_transaction *transaction;
437 struct strbuf err = STRBUF_INIT;
438 struct ref_filter filter;
@@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
482 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
483 OPT_END()
484 };
485 + int ret = 0;
486
487 setup_ref_filter_porcelain_msg();
488
@@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
530 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
531 filter.ignore_case = icase;
532 if (cmdmode == 'l') {
532 - int ret;
533 if (column_active(colopts)) {
534 struct column_options copts;
535 memset(&copts, 0, sizeof(copts));
@@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
540 ret = list_tags(&filter, sorting, &format);
541 if (column_active(colopts))
542 stop_column_filter();
543 - return ret;
543 + goto cleanup;
544 }
545 if (filter.lines != -1)
546 die(_("-n option is only allowed in list mode"));
@@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
552 die(_("--points-at option is only allowed in list mode"));
553 if (filter.reachable_from || filter.unreachable_from)
554 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 + }
559 if (cmdmode == 'v') {
560 if (format.format && verify_ref_format(&format))
561 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;
564 }
565
566 if (msg.given || msgfile) {
@@ -626,10 +629,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
629 printf(_("Updated tag '%s' (was %s)\n"), tag,
630 find_unique_abbrev(&prev, DEFAULT_ABBREV));
631
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;
639 }