remote: plug memory leaks at early returns

Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 23, 2024 at 22:21 UTC 4e7aa344f254f7cd283445f953ad1b45dfd2f660
1 file changed +15 -8
builtin/remote.c
+15 -8
@@ -164,6 +164,7 @@ static int add(int argc, const char **argv, const char *prefix)
164 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
165 const char *name, *url;
166 int i;
167 + int result = 0;
168
169 struct option options[] = {
170 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
@@ -230,8 +231,10 @@ static int add(int argc, const char **argv, const char *prefix)
231 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
232 }
233
233 - if (fetch && fetch_remote(name))
234 - return 1;
234 + if (fetch && fetch_remote(name)) {
235 + result = 1;
236 + goto out;
237 + }
238
239 if (master) {
240 strbuf_reset(&buf);
@@ -241,14 +244,15 @@ static int add(int argc, const char **argv, const char *prefix)
244 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
245
246 if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote add"))
244 - return error(_("Could not setup master '%s'"), master);
247 + result = error(_("Could not setup master '%s'"), master);
248 }
249
250 +out:
251 strbuf_release(&buf);
252 strbuf_release(&buf2);
253 string_list_clear(&track, 0);
254
251 - return 0;
255 + return result;
256 }
257
258 struct branch_info {
@@ -715,6 +719,7 @@ static int mv(int argc, const char **argv, const char *prefix)
719 struct rename_info rename;
720 int i, refs_renamed_nr = 0, refspec_updated = 0;
721 struct progress *progress = NULL;
722 + int result = 0;
723
724 argc = parse_options(argc, argv, prefix, options,
725 builtin_remote_rename_usage, 0);
@@ -747,9 +752,11 @@ static int mv(int argc, const char **argv, const char *prefix)
752
753 strbuf_addf(&buf, "remote.%s", rename.old_name);
754 strbuf_addf(&buf2, "remote.%s", rename.new_name);
750 - if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1)
751 - return error(_("Could not rename config section '%s' to '%s'"),
752 - buf.buf, buf2.buf);
755 + if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1) {
756 + result = error(_("Could not rename config section '%s' to '%s'"),
757 + buf.buf, buf2.buf);
758 + goto out;
759 + }
760
761 if (oldremote->fetch.raw_nr) {
762 strbuf_reset(&buf);
@@ -870,7 +877,7 @@ out:
877 strbuf_release(&buf);
878 strbuf_release(&buf2);
879 strbuf_release(&buf3);
873 - return 0;
880 + return result;
881 }
882
883 static int rm(int argc, const char **argv, const char *prefix)