Skip to content

Commit d811ba1

Browse files
committed
Merge branch 'rs/strbuf-leakfix'
Many leaks of strbuf have been fixed. * rs/strbuf-leakfix: (34 commits) wt-status: release strbuf after use in wt_longstatus_print_tracking() wt-status: release strbuf after use in read_rebase_todolist() vcs-svn: release strbuf after use in end_revision() utf8: release strbuf on error return in strbuf_utf8_replace() userdiff: release strbuf after use in userdiff_get_textconv() transport-helper: release strbuf after use in process_connect_service() sequencer: release strbuf after use in save_head() shortlog: release strbuf after use in insert_one_record() sha1_file: release strbuf on error return in index_path() send-pack: release strbuf on error return in send_pack() remote: release strbuf after use in set_url() remote: release strbuf after use in migrate_file() remote: release strbuf after use in read_remote_branches() refs: release strbuf on error return in write_pseudoref() notes: release strbuf after use in notes_copy_from_stdin() merge: release strbuf after use in write_merge_heads() merge: release strbuf after use in save_state() mailinfo: release strbuf on error return in handle_boundary() mailinfo: release strbuf after use in handle_from() help: release strbuf on error return in exec_woman_emacs() ...
2 parents 17cb5f8 + b6ec307 commit d811ba1

22 files changed

+83
-36
lines changed

builtin/am.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,7 @@ static int detect_patch_format(const char **paths)
671671
goto done;
672672
}
673673

674-
strbuf_reset(&l2);
675674
strbuf_getline(&l2, fp);
676-
strbuf_reset(&l3);
677675
strbuf_getline(&l3, fp);
678676

679677
/*
@@ -696,6 +694,8 @@ static int detect_patch_format(const char **paths)
696694
done:
697695
fclose(fp);
698696
strbuf_release(&l1);
697+
strbuf_release(&l2);
698+
strbuf_release(&l3);
699699
return ret;
700700
}
701701

@@ -881,6 +881,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
881881
static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
882882
{
883883
struct strbuf sb = STRBUF_INIT;
884+
int rc = 0;
884885

885886
while (!strbuf_getline_lf(&sb, in)) {
886887
const char *str;
@@ -894,19 +895,27 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
894895

895896
errno = 0;
896897
timestamp = parse_timestamp(str, &end, 10);
897-
if (errno)
898-
return error(_("invalid timestamp"));
898+
if (errno) {
899+
rc = error(_("invalid timestamp"));
900+
goto exit;
901+
}
899902

900-
if (!skip_prefix(end, " ", &str))
901-
return error(_("invalid Date line"));
903+
if (!skip_prefix(end, " ", &str)) {
904+
rc = error(_("invalid Date line"));
905+
goto exit;
906+
}
902907

903908
errno = 0;
904909
tz = strtol(str, &end, 10);
905-
if (errno)
906-
return error(_("invalid timezone offset"));
910+
if (errno) {
911+
rc = error(_("invalid timezone offset"));
912+
goto exit;
913+
}
907914

908-
if (*end)
909-
return error(_("invalid Date line"));
915+
if (*end) {
916+
rc = error(_("invalid Date line"));
917+
goto exit;
918+
}
910919

911920
/*
912921
* mercurial's timezone is in seconds west of UTC,
@@ -931,9 +940,9 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
931940
fwrite(sb.buf, 1, sb.len, out);
932941
strbuf_reset(&sb);
933942
}
934-
943+
exit:
935944
strbuf_release(&sb);
936-
return 0;
945+
return rc;
937946
}
938947

939948
/**
@@ -2096,6 +2105,7 @@ static int safe_to_abort(const struct am_state *state)
20962105
die(_("could not parse %s"), am_path(state, "abort-safety"));
20972106
} else
20982107
oidclr(&abort_safety);
2108+
strbuf_release(&sb);
20992109

21002110
if (get_oid("HEAD", &head))
21012111
oidclr(&head);

builtin/check-ref-format.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static int check_ref_format_branch(const char *arg)
4545
if (strbuf_check_branch_ref(&sb, arg))
4646
die("'%s' is not a valid branch name", arg);
4747
printf("%s\n", sb.buf + 11);
48+
strbuf_release(&sb);
4849
return 0;
4950
}
5051

builtin/clean.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
167167
}
168168

169169
*dir_gone = 0;
170-
return 0;
170+
goto out;
171171
}
172172

173173
dir = opendir(path->buf);
@@ -181,7 +181,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
181181
warning_errno(_(msg_warn_remove_failed), quoted.buf);
182182
*dir_gone = 0;
183183
}
184-
return res;
184+
ret = res;
185+
goto out;
185186
}
186187

187188
strbuf_complete(path, '/');
@@ -249,6 +250,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
249250
for (i = 0; i < dels.nr; i++)
250251
printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
251252
}
253+
out:
254+
strbuf_release(&quoted);
252255
string_list_clear(&dels, 0);
253256
return ret;
254257
}

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ static void remove_junk(void)
507507
if (junk_work_tree) {
508508
strbuf_addstr(&sb, junk_work_tree);
509509
remove_dir_recursively(&sb, 0);
510-
strbuf_reset(&sb);
511510
}
511+
strbuf_release(&sb);
512512
}
513513

514514
static void remove_junk_on_signal(int signo)

builtin/help.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static void exec_woman_emacs(const char *path, const char *page)
131131
strbuf_addf(&man_page, "(woman \"%s\")", page);
132132
execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
133133
warning_errno(_("failed to exec '%s'"), path);
134+
strbuf_release(&man_page);
134135
}
135136
}
136137

@@ -152,6 +153,7 @@ static void exec_man_konqueror(const char *path, const char *page)
152153
strbuf_addf(&man_page, "man:%s(1)", page);
153154
execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
154155
warning_errno(_("failed to exec '%s'"), path);
156+
strbuf_release(&man_page);
155157
}
156158
}
157159

@@ -169,6 +171,7 @@ static void exec_man_cmd(const char *cmd, const char *page)
169171
strbuf_addf(&shell_cmd, "%s %s", cmd, page);
170172
execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
171173
warning(_("failed to exec '%s'"), cmd);
174+
strbuf_release(&shell_cmd);
172175
}
173176

174177
static void add_man_viewer(const char *name)

builtin/merge.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ static int save_state(struct object_id *stash)
255255
struct child_process cp = CHILD_PROCESS_INIT;
256256
struct strbuf buffer = STRBUF_INIT;
257257
const char *argv[] = {"stash", "create", NULL};
258+
int rc = -1;
258259

259260
cp.argv = argv;
260261
cp.out = -1;
@@ -268,11 +269,14 @@ static int save_state(struct object_id *stash)
268269
if (finish_command(&cp) || len < 0)
269270
die(_("stash failed"));
270271
else if (!len) /* no changes */
271-
return -1;
272+
goto out;
272273
strbuf_setlen(&buffer, buffer.len-1);
273274
if (get_oid(buffer.buf, stash))
274275
die(_("not a valid object: %s"), buffer.buf);
275-
return 0;
276+
rc = 0;
277+
out:
278+
strbuf_release(&buffer);
279+
return rc;
276280
}
277281

278282
static void read_empty(unsigned const char *sha1, int verbose)
@@ -942,6 +946,7 @@ static void write_merge_heads(struct commit_list *remoteheads)
942946
if (fast_forward == FF_NO)
943947
strbuf_addstr(&buf, "no-ff");
944948
write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
949+
strbuf_release(&buf);
945950
}
946951

947952
static void write_merge_state(struct commit_list *remoteheads)

builtin/notes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
328328
} else {
329329
finish_copy_notes_for_rewrite(c, msg);
330330
}
331+
strbuf_release(&buf);
331332
return ret;
332333
}
333334

builtin/remote.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ static int read_remote_branches(const char *refname,
571571
else
572572
item->util = NULL;
573573
}
574+
strbuf_release(&buf);
574575

575576
return 0;
576577
}
@@ -595,6 +596,7 @@ static int migrate_file(struct remote *remote)
595596
unlink_or_warn(git_path("remotes/%s", remote->name));
596597
else if (remote->origin == REMOTE_BRANCHES)
597598
unlink_or_warn(git_path("branches/%s", remote->name));
599+
strbuf_release(&buf);
598600

599601
return 0;
600602
}
@@ -1563,9 +1565,7 @@ static int set_url(int argc, const char **argv)
15631565
"^$", 0);
15641566
else
15651567
git_config_set(name_buf.buf, newurl);
1566-
strbuf_release(&name_buf);
1567-
1568-
return 0;
1568+
goto out;
15691569
}
15701570

15711571
/* Old URL specified. Demand that one matches. */
@@ -1588,6 +1588,8 @@ static int set_url(int argc, const char **argv)
15881588
git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
15891589
else
15901590
git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1591+
out:
1592+
strbuf_release(&name_buf);
15911593
return 0;
15921594
}
15931595

commit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,10 +1570,13 @@ int commit_tree_extended(const char *msg, size_t msg_len,
15701570
if (encoding_is_utf8 && !verify_utf8(&buffer))
15711571
fprintf(stderr, _(commit_utf8_warn));
15721572

1573-
if (sign_commit && do_sign_commit(&buffer, sign_commit))
1574-
return -1;
1573+
if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1574+
result = -1;
1575+
goto out;
1576+
}
15751577

15761578
result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1579+
out:
15771580
strbuf_release(&buffer);
15781581
return result;
15791582
}

connect.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ struct child_process *git_connect(int fd[2], const char *url,
778778
char *hostandport, *path;
779779
struct child_process *conn = &no_fork;
780780
enum protocol protocol;
781-
struct strbuf cmd = STRBUF_INIT;
782781

783782
/* Without this we cannot rely on waitpid() to tell
784783
* what happened to our children.
@@ -826,6 +825,8 @@ struct child_process *git_connect(int fd[2], const char *url,
826825
target_host, 0);
827826
free(target_host);
828827
} else {
828+
struct strbuf cmd = STRBUF_INIT;
829+
829830
conn = xmalloc(sizeof(*conn));
830831
child_process_init(conn);
831832

@@ -862,6 +863,7 @@ struct child_process *git_connect(int fd[2], const char *url,
862863
free(hostandport);
863864
free(path);
864865
free(conn);
866+
strbuf_release(&cmd);
865867
return NULL;
866868
}
867869

0 commit comments

Comments
 (0)