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()
  ...
This commit is contained in:
Junio C Hamano 2017-09-19 10:47:57 +09:00
Родитель 17cb5f85d0 b6ec307177
Коммит d811ba1897
22 изменённых файлов: 83 добавлений и 36 удалений

Просмотреть файл

@ -671,9 +671,7 @@ static int detect_patch_format(const char **paths)
goto done; goto done;
} }
strbuf_reset(&l2);
strbuf_getline(&l2, fp); strbuf_getline(&l2, fp);
strbuf_reset(&l3);
strbuf_getline(&l3, fp); strbuf_getline(&l3, fp);
/* /*
@ -696,6 +694,8 @@ static int detect_patch_format(const char **paths)
done: done:
fclose(fp); fclose(fp);
strbuf_release(&l1); strbuf_release(&l1);
strbuf_release(&l2);
strbuf_release(&l3);
return ret; return ret;
} }
@ -881,6 +881,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr) static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int rc = 0;
while (!strbuf_getline_lf(&sb, in)) { while (!strbuf_getline_lf(&sb, in)) {
const char *str; const char *str;
@ -894,19 +895,27 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
errno = 0; errno = 0;
timestamp = parse_timestamp(str, &end, 10); timestamp = parse_timestamp(str, &end, 10);
if (errno) if (errno) {
return error(_("invalid timestamp")); rc = error(_("invalid timestamp"));
goto exit;
}
if (!skip_prefix(end, " ", &str)) if (!skip_prefix(end, " ", &str)) {
return error(_("invalid Date line")); rc = error(_("invalid Date line"));
goto exit;
}
errno = 0; errno = 0;
tz = strtol(str, &end, 10); tz = strtol(str, &end, 10);
if (errno) if (errno) {
return error(_("invalid timezone offset")); rc = error(_("invalid timezone offset"));
goto exit;
}
if (*end) if (*end) {
return error(_("invalid Date line")); rc = error(_("invalid Date line"));
goto exit;
}
/* /*
* mercurial's timezone is in seconds west of UTC, * 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)
fwrite(sb.buf, 1, sb.len, out); fwrite(sb.buf, 1, sb.len, out);
strbuf_reset(&sb); strbuf_reset(&sb);
} }
exit:
strbuf_release(&sb); strbuf_release(&sb);
return 0; return rc;
} }
/** /**
@ -2096,6 +2105,7 @@ static int safe_to_abort(const struct am_state *state)
die(_("could not parse %s"), am_path(state, "abort-safety")); die(_("could not parse %s"), am_path(state, "abort-safety"));
} else } else
oidclr(&abort_safety); oidclr(&abort_safety);
strbuf_release(&sb);
if (get_oid("HEAD", &head)) if (get_oid("HEAD", &head))
oidclr(&head); oidclr(&head);

Просмотреть файл

@ -45,6 +45,7 @@ static int check_ref_format_branch(const char *arg)
if (strbuf_check_branch_ref(&sb, arg)) if (strbuf_check_branch_ref(&sb, arg))
die("'%s' is not a valid branch name", arg); die("'%s' is not a valid branch name", arg);
printf("%s\n", sb.buf + 11); printf("%s\n", sb.buf + 11);
strbuf_release(&sb);
return 0; return 0;
} }

Просмотреть файл

@ -167,7 +167,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
} }
*dir_gone = 0; *dir_gone = 0;
return 0; goto out;
} }
dir = opendir(path->buf); dir = opendir(path->buf);
@ -181,7 +181,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
warning_errno(_(msg_warn_remove_failed), quoted.buf); warning_errno(_(msg_warn_remove_failed), quoted.buf);
*dir_gone = 0; *dir_gone = 0;
} }
return res; ret = res;
goto out;
} }
strbuf_complete(path, '/'); strbuf_complete(path, '/');
@ -249,6 +250,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
for (i = 0; i < dels.nr; i++) for (i = 0; i < dels.nr; i++)
printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string); printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
} }
out:
strbuf_release(&quoted);
string_list_clear(&dels, 0); string_list_clear(&dels, 0);
return ret; return ret;
} }

Просмотреть файл

@ -507,8 +507,8 @@ static void remove_junk(void)
if (junk_work_tree) { if (junk_work_tree) {
strbuf_addstr(&sb, junk_work_tree); strbuf_addstr(&sb, junk_work_tree);
remove_dir_recursively(&sb, 0); remove_dir_recursively(&sb, 0);
strbuf_reset(&sb);
} }
strbuf_release(&sb);
} }
static void remove_junk_on_signal(int signo) static void remove_junk_on_signal(int signo)

Просмотреть файл

@ -131,6 +131,7 @@ static void exec_woman_emacs(const char *path, const char *page)
strbuf_addf(&man_page, "(woman \"%s\")", page); strbuf_addf(&man_page, "(woman \"%s\")", page);
execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL); execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
warning_errno(_("failed to exec '%s'"), path); warning_errno(_("failed to exec '%s'"), path);
strbuf_release(&man_page);
} }
} }
@ -152,6 +153,7 @@ static void exec_man_konqueror(const char *path, const char *page)
strbuf_addf(&man_page, "man:%s(1)", page); strbuf_addf(&man_page, "man:%s(1)", page);
execlp(path, filename, "newTab", man_page.buf, (char *)NULL); execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
warning_errno(_("failed to exec '%s'"), path); warning_errno(_("failed to exec '%s'"), path);
strbuf_release(&man_page);
} }
} }
@ -169,6 +171,7 @@ static void exec_man_cmd(const char *cmd, const char *page)
strbuf_addf(&shell_cmd, "%s %s", cmd, page); strbuf_addf(&shell_cmd, "%s %s", cmd, page);
execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL); execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
warning(_("failed to exec '%s'"), cmd); warning(_("failed to exec '%s'"), cmd);
strbuf_release(&shell_cmd);
} }
static void add_man_viewer(const char *name) static void add_man_viewer(const char *name)

Просмотреть файл

@ -255,6 +255,7 @@ static int save_state(struct object_id *stash)
struct child_process cp = CHILD_PROCESS_INIT; struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf buffer = STRBUF_INIT; struct strbuf buffer = STRBUF_INIT;
const char *argv[] = {"stash", "create", NULL}; const char *argv[] = {"stash", "create", NULL};
int rc = -1;
cp.argv = argv; cp.argv = argv;
cp.out = -1; cp.out = -1;
@ -268,11 +269,14 @@ static int save_state(struct object_id *stash)
if (finish_command(&cp) || len < 0) if (finish_command(&cp) || len < 0)
die(_("stash failed")); die(_("stash failed"));
else if (!len) /* no changes */ else if (!len) /* no changes */
return -1; goto out;
strbuf_setlen(&buffer, buffer.len-1); strbuf_setlen(&buffer, buffer.len-1);
if (get_oid(buffer.buf, stash)) if (get_oid(buffer.buf, stash))
die(_("not a valid object: %s"), buffer.buf); die(_("not a valid object: %s"), buffer.buf);
return 0; rc = 0;
out:
strbuf_release(&buffer);
return rc;
} }
static void read_empty(unsigned const char *sha1, int verbose) static void read_empty(unsigned const char *sha1, int verbose)
@ -942,6 +946,7 @@ static void write_merge_heads(struct commit_list *remoteheads)
if (fast_forward == FF_NO) if (fast_forward == FF_NO)
strbuf_addstr(&buf, "no-ff"); strbuf_addstr(&buf, "no-ff");
write_file_buf(git_path_merge_mode(), buf.buf, buf.len); write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
strbuf_release(&buf);
} }
static void write_merge_state(struct commit_list *remoteheads) static void write_merge_state(struct commit_list *remoteheads)

Просмотреть файл

@ -328,6 +328,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
} else { } else {
finish_copy_notes_for_rewrite(c, msg); finish_copy_notes_for_rewrite(c, msg);
} }
strbuf_release(&buf);
return ret; return ret;
} }

Просмотреть файл

@ -571,6 +571,7 @@ static int read_remote_branches(const char *refname,
else else
item->util = NULL; item->util = NULL;
} }
strbuf_release(&buf);
return 0; return 0;
} }
@ -595,6 +596,7 @@ static int migrate_file(struct remote *remote)
unlink_or_warn(git_path("remotes/%s", remote->name)); unlink_or_warn(git_path("remotes/%s", remote->name));
else if (remote->origin == REMOTE_BRANCHES) else if (remote->origin == REMOTE_BRANCHES)
unlink_or_warn(git_path("branches/%s", remote->name)); unlink_or_warn(git_path("branches/%s", remote->name));
strbuf_release(&buf);
return 0; return 0;
} }
@ -1563,9 +1565,7 @@ static int set_url(int argc, const char **argv)
"^$", 0); "^$", 0);
else else
git_config_set(name_buf.buf, newurl); git_config_set(name_buf.buf, newurl);
strbuf_release(&name_buf); goto out;
return 0;
} }
/* Old URL specified. Demand that one matches. */ /* Old URL specified. Demand that one matches. */
@ -1588,6 +1588,8 @@ static int set_url(int argc, const char **argv)
git_config_set_multivar(name_buf.buf, newurl, oldurl, 0); git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
else else
git_config_set_multivar(name_buf.buf, NULL, oldurl, 1); git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
out:
strbuf_release(&name_buf);
return 0; return 0;
} }

Просмотреть файл

@ -1570,10 +1570,13 @@ int commit_tree_extended(const char *msg, size_t msg_len,
if (encoding_is_utf8 && !verify_utf8(&buffer)) if (encoding_is_utf8 && !verify_utf8(&buffer))
fprintf(stderr, _(commit_utf8_warn)); fprintf(stderr, _(commit_utf8_warn));
if (sign_commit && do_sign_commit(&buffer, sign_commit)) if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
return -1; result = -1;
goto out;
}
result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret); result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
out:
strbuf_release(&buffer); strbuf_release(&buffer);
return result; return result;
} }

Просмотреть файл

@ -778,7 +778,6 @@ struct child_process *git_connect(int fd[2], const char *url,
char *hostandport, *path; char *hostandport, *path;
struct child_process *conn = &no_fork; struct child_process *conn = &no_fork;
enum protocol protocol; enum protocol protocol;
struct strbuf cmd = STRBUF_INIT;
/* Without this we cannot rely on waitpid() to tell /* Without this we cannot rely on waitpid() to tell
* what happened to our children. * what happened to our children.
@ -826,6 +825,8 @@ struct child_process *git_connect(int fd[2], const char *url,
target_host, 0); target_host, 0);
free(target_host); free(target_host);
} else { } else {
struct strbuf cmd = STRBUF_INIT;
conn = xmalloc(sizeof(*conn)); conn = xmalloc(sizeof(*conn));
child_process_init(conn); child_process_init(conn);
@ -862,6 +863,7 @@ struct child_process *git_connect(int fd[2], const char *url,
free(hostandport); free(hostandport);
free(path); free(path);
free(conn); free(conn);
strbuf_release(&cmd);
return NULL; return NULL;
} }

Просмотреть файл

@ -423,8 +423,10 @@ static int filter_buffer_or_fd(int in, int out, void *data)
child_process.in = -1; child_process.in = -1;
child_process.out = out; child_process.out = out;
if (start_command(&child_process)) if (start_command(&child_process)) {
strbuf_release(&cmd);
return error("cannot fork to run external filter '%s'", params->cmd); return error("cannot fork to run external filter '%s'", params->cmd);
}
sigchain_push(SIGPIPE, SIG_IGN); sigchain_push(SIGPIPE, SIG_IGN);

3
diff.c
Просмотреть файл

@ -2583,6 +2583,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
} }
print_stat_summary_inserts_deletes(options, total_files, adds, dels); print_stat_summary_inserts_deletes(options, total_files, adds, dels);
strbuf_release(&out);
} }
static void show_shortstats(struct diffstat_t *data, struct diff_options *options) static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
@ -5288,6 +5289,7 @@ static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
sb.buf, sb.len, 0); sb.buf, sb.len, 0);
show_mode_change(opt, p, 0); show_mode_change(opt, p, 0);
strbuf_release(&sb);
} }
static void diff_summary(struct diff_options *opt, struct diff_filepair *p) static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
@ -5313,6 +5315,7 @@ static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
sb.buf, sb.len, 0); sb.buf, sb.len, 0);
strbuf_release(&sb);
} }
show_mode_change(opt, p, !p->score); show_mode_change(opt, p, !p->score);
break; break;

Просмотреть файл

@ -149,16 +149,14 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
at = strchr(f.buf, '@'); at = strchr(f.buf, '@');
if (!at) { if (!at) {
parse_bogus_from(mi, from); parse_bogus_from(mi, from);
return; goto out;
} }
/* /*
* If we already have one email, don't take any confusing lines * If we already have one email, don't take any confusing lines
*/ */
if (mi->email.len && strchr(at + 1, '@')) { if (mi->email.len && strchr(at + 1, '@'))
strbuf_release(&f); goto out;
return;
}
/* Pick up the string around '@', possibly delimited with <> /* Pick up the string around '@', possibly delimited with <>
* pair; that is the email part. * pair; that is the email part.
@ -198,6 +196,7 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
} }
get_sane_name(&mi->name, &f, &mi->email); get_sane_name(&mi->name, &f, &mi->email);
out:
strbuf_release(&f); strbuf_release(&f);
} }
@ -929,6 +928,7 @@ again:
error("Detected mismatched boundaries, can't recover"); error("Detected mismatched boundaries, can't recover");
mi->input_error = -1; mi->input_error = -1;
mi->content_top = mi->content; mi->content_top = mi->content;
strbuf_release(&newline);
return 0; return 0;
} }
handle_filter(mi, &newline); handle_filter(mi, &newline);

2
refs.c
Просмотреть файл

@ -594,7 +594,7 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
if (fd < 0) { if (fd < 0) {
strbuf_addf(err, "could not open '%s' for writing: %s", strbuf_addf(err, "could not open '%s' for writing: %s",
filename, strerror(errno)); filename, strerror(errno));
return -1; goto done;
} }
if (old_sha1) { if (old_sha1) {

Просмотреть файл

@ -492,8 +492,11 @@ int send_pack(struct send_pack_args *args,
* we were to send it and we're trying to send the refs * we were to send it and we're trying to send the refs
* atomically, abort the whole operation. * atomically, abort the whole operation.
*/ */
if (use_atomic) if (use_atomic) {
strbuf_release(&req_buf);
strbuf_release(&cap_buf);
return atomic_push_failure(args, remote_refs, ref); return atomic_push_failure(args, remote_refs, ref);
}
/* Fallthrough for non atomic case. */ /* Fallthrough for non atomic case. */
default: default:
continue; continue;

Просмотреть файл

@ -1565,6 +1565,7 @@ static int save_head(const char *head)
static struct lock_file head_lock; static struct lock_file head_lock;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int fd; int fd;
ssize_t written;
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
if (fd < 0) { if (fd < 0) {
@ -1572,7 +1573,9 @@ static int save_head(const char *head)
return error_errno(_("could not lock HEAD")); return error_errno(_("could not lock HEAD"));
} }
strbuf_addf(&buf, "%s\n", head); strbuf_addf(&buf, "%s\n", head);
if (write_in_full(fd, buf.buf, buf.len) < 0) { written = write_in_full(fd, buf.buf, buf.len);
strbuf_release(&buf);
if (written < 0) {
rollback_lock_file(&head_lock); rollback_lock_file(&head_lock);
return error_errno(_("could not write to '%s'"), return error_errno(_("could not write to '%s'"),
git_path_head_file()); git_path_head_file());

Просмотреть файл

@ -1820,6 +1820,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
{ {
int fd; int fd;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int rc = 0;
switch (st->st_mode & S_IFMT) { switch (st->st_mode & S_IFMT) {
case S_IFREG: case S_IFREG:
@ -1836,8 +1837,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
if (!(flags & HASH_WRITE_OBJECT)) if (!(flags & HASH_WRITE_OBJECT))
hash_sha1_file(sb.buf, sb.len, blob_type, oid->hash); hash_sha1_file(sb.buf, sb.len, blob_type, oid->hash);
else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash)) else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash))
return error("%s: failed to insert into database", rc = error("%s: failed to insert into database", path);
path);
strbuf_release(&sb); strbuf_release(&sb);
break; break;
case S_IFDIR: case S_IFDIR:
@ -1845,7 +1845,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
default: default:
return error("%s: unsupported file type", path); return error("%s: unsupported file type", path);
} }
return 0; return rc;
} }
int read_pack_header(int fd, struct pack_header *header) int read_pack_header(int fd, struct pack_header *header)

Просмотреть файл

@ -604,6 +604,7 @@ static int process_connect_service(struct transport *transport,
cmdbuf.buf); cmdbuf.buf);
exit: exit:
strbuf_release(&cmdbuf);
fclose(input); fclose(input);
return ret; return ret;
} }

Просмотреть файл

@ -293,6 +293,7 @@ struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
strbuf_addf(&name, "textconv/%s", driver->name); strbuf_addf(&name, "textconv/%s", driver->name);
notes_cache_init(c, name.buf, driver->textconv); notes_cache_init(c, name.buf, driver->textconv);
driver->textconv_cache = c; driver->textconv_cache = c;
strbuf_release(&name);
} }
return driver; return driver;

3
utf8.c
Просмотреть файл

@ -381,7 +381,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
old = src; old = src;
n = utf8_width((const char**)&src, NULL); n = utf8_width((const char**)&src, NULL);
if (!src) /* broken utf-8, do nothing */ if (!src) /* broken utf-8, do nothing */
return; goto out;
if (n && w >= pos && w < pos + width) { if (n && w >= pos && w < pos + width) {
if (subst) { if (subst) {
memcpy(dst, subst, subst_len); memcpy(dst, subst, subst_len);
@ -397,6 +397,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
} }
strbuf_setlen(&sb_dst, dst - sb_dst.buf); strbuf_setlen(&sb_dst, dst - sb_dst.buf);
strbuf_swap(sb_src, &sb_dst); strbuf_swap(sb_src, &sb_dst);
out:
strbuf_release(&sb_dst); strbuf_release(&sb_dst);
} }

Просмотреть файл

@ -318,6 +318,7 @@ static void end_revision(const char *note_ref)
strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision); strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
fast_export_note(mark.buf, "inline"); fast_export_note(mark.buf, "inline");
fast_export_buf_to_data(&rev_ctx.note); fast_export_buf_to_data(&rev_ctx.note);
strbuf_release(&mark);
} }
} }

Просмотреть файл

@ -1026,6 +1026,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
comment_line_char); comment_line_char);
else else
fputs("\n", s->fp); fputs("\n", s->fp);
strbuf_release(&sb);
} }
static int has_unmerged(struct wt_status *s) static int has_unmerged(struct wt_status *s)
@ -1193,6 +1194,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
string_list_append(lines, line.buf); string_list_append(lines, line.buf);
} }
fclose(f); fclose(f);
strbuf_release(&line);
return 0; return 0;
} }