Browse Source

Merge branch 'ab/plug-leak-in-revisions'

Plug the memory leaks from the trickiest API of all, the revision
walker.

* ab/plug-leak-in-revisions: (27 commits)
  revisions API: add a TODO for diff_free(&revs->diffopt)
  revisions API: have release_revisions() release "topo_walk_info"
  revisions API: have release_revisions() release "date_mode"
  revisions API: call diff_free(&revs->pruning) in revisions_release()
  revisions API: release "reflog_info" in release revisions()
  revisions API: clear "boundary_commits" in release_revisions()
  revisions API: have release_revisions() release "prune_data"
  revisions API: have release_revisions() release "grep_filter"
  revisions API: have release_revisions() release "filter"
  revisions API: have release_revisions() release "cmdline"
  revisions API: have release_revisions() release "mailmap"
  revisions API: have release_revisions() release "commits"
  revisions API users: use release_revisions() for "prune_data" users
  revisions API users: use release_revisions() with UNLEAK()
  revisions API users: use release_revisions() in builtin/log.c
  revisions API users: use release_revisions() in http-push.c
  revisions API users: add "goto cleanup" for release_revisions()
  stash: always have the owner of "stash_info" free it
  revisions API users: use release_revisions() needing REV_INFO_INIT
  revision.[ch]: document and move code declared around "init"
  ...
pull/1275/head
Junio C Hamano 2 months ago
parent
commit
2da81d1efb
  1. 3
      add-interactive.c
  2. 20
      bisect.c
  3. 3
      builtin/add.c
  4. 3
      builtin/am.c
  5. 2
      builtin/bisect--helper.c
  6. 4
      builtin/blame.c
  7. 3
      builtin/checkout.c
  8. 6
      builtin/commit.c
  9. 2
      builtin/describe.c
  10. 8
      builtin/diff-files.c
  11. 5
      builtin/diff-index.c
  12. 2
      builtin/diff.c
  13. 1
      builtin/fast-export.c
  14. 36
      builtin/log.c
  15. 2
      builtin/merge.c
  16. 2
      builtin/pack-objects.c
  17. 1
      builtin/prune.c
  18. 1
      builtin/reflog.c
  19. 25
      builtin/rev-list.c
  20. 8
      builtin/shortlog.c
  21. 115
      builtin/stash.c
  22. 14
      builtin/submodule--helper.c
  23. 12
      bundle.c
  24. 19
      commit.c
  25. 27
      contrib/coccinelle/free.cocci
  26. 8
      diff-lib.c
  27. 1
      fmt-merge-msg.c
  28. 3
      http-push.c
  29. 1
      merge-ort.c
  30. 5
      merge-recursive.c
  31. 1
      midx.c
  32. 1
      pack-bitmap-write.c
  33. 2
      range-diff.c
  34. 1
      ref-filter.c
  35. 24
      reflog-walk.c
  36. 1
      reflog-walk.h
  37. 1
      remote.c
  38. 70
      revision.c
  39. 73
      revision.h
  40. 26
      sequencer.c
  41. 1
      shallow.c
  42. 11
      submodule.c
  43. 23
      t/helper/test-fast-rebase.c
  44. 1
      t/helper/test-revision-walking.c
  45. 4
      t/lib-git-svn.sh
  46. 1
      t/t0056-git-C.sh
  47. 1
      t/t0062-revision-walking.sh
  48. 1
      t/t0100-previous.sh
  49. 2
      t/t0101-at-syntax.sh
  50. 1
      t/t1001-read-tree-m-2way.sh
  51. 1
      t/t1002-read-tree-m-u-2way.sh
  52. 2
      t/t1060-object-corruption.sh
  53. 2
      t/t1401-symbolic-ref.sh
  54. 1
      t/t1411-reflog-show.sh
  55. 2
      t/t1412-reflog-loop.sh
  56. 1
      t/t1415-worktree-refs.sh
  57. 1
      t/t2015-checkout-unborn.sh
  58. 1
      t/t2200-add-update.sh
  59. 1
      t/t3302-notes-index-expensive.sh
  60. 1
      t/t3303-notes-subtrees.sh
  61. 1
      t/t3305-notes-fanout.sh
  62. 1
      t/t3408-rebase-multi-line.sh
  63. 1
      t/t4021-format-patch-numbered.sh
  64. 1
      t/t4027-diff-submodule.sh
  65. 2
      t/t4028-format-patch-mime-headers.sh
  66. 1
      t/t4036-format-patch-signer-mime.sh
  67. 1
      t/t4039-diff-assume-unchanged.sh
  68. 1
      t/t4055-diff-context.sh
  69. 1
      t/t4066-diff-emit-delay.sh
  70. 1
      t/t4122-apply-symlink-inside.sh
  71. 1
      t/t4126-apply-empty.sh
  72. 1
      t/t4128-apply-root.sh
  73. 2
      t/t4206-log-follow-harder-copies.sh
  74. 1
      t/t4207-log-decoration-colors.sh
  75. 1
      t/t4212-log-corrupt.sh
  76. 2
      t/t5301-sliding-window.sh
  77. 2
      t/t5313-pack-bounds-checks.sh
  78. 2
      t/t5316-pack-delta-depth.sh
  79. 2
      t/t5320-delta-islands.sh
  80. 1
      t/t5322-pack-objects-sparse.sh
  81. 1
      t/t5506-remote-groups.sh
  82. 1
      t/t5513-fetch-track.sh
  83. 1
      t/t5515-fetch-merge-logic.sh
  84. 1
      t/t5518-fetch-exit-status.sh
  85. 2
      t/t5532-fetch-proxy.sh
  86. 1
      t/t5600-clone-fail-cleanup.sh
  87. 2
      t/t5900-repo-selection.sh
  88. 1
      t/t6002-rev-list-bisect.sh
  89. 1
      t/t6003-rev-list-topo-order.sh
  90. 1
      t/t6005-rev-list-count.sh
  91. 1
      t/t6018-rev-list-glob.sh
  92. 1
      t/t6100-rev-list-in-order.sh
  93. 1
      t/t6101-rev-parse-parents.sh
  94. 1
      t/t6110-rev-list-sparse.sh
  95. 2
      t/t6114-keep-packs.sh
  96. 2
      t/t6131-pathspec-icase.sh
  97. 1
      t/t7008-filter-branch-null-sha1.sh
  98. 2
      t/t7702-repack-cyclic-alternate.sh
  99. 1
      t/t9001-send-email.sh
  100. 1
      t/t9100-git-svn-basic.sh
  101. 2
      t/t9101-git-svn-props.sh
  102. 2
      t/t9104-git-svn-follow-parent.sh
  103. 2
      t/t9106-git-svn-commit-diff-clobber.sh
  104. 1
      t/t9115-git-svn-dcommit-funky-renames.sh
  105. 1
      t/t9116-git-svn-log.sh
  106. 2
      t/t9122-git-svn-author.sh
  107. 1
      t/t9127-git-svn-partial-rebuild.sh
  108. 1
      t/t9129-git-svn-i18n-commitencoding.sh
  109. 1
      t/t9132-git-svn-broken-symlink.sh
  110. 1
      t/t9139-git-svn-non-utf8-commitencoding.sh
  111. 2
      t/t9146-git-svn-empty-dirs.sh
  112. 1
      t/t9148-git-svn-propset.sh
  113. 1
      t/t9160-git-svn-preserve-empty-dirs.sh
  114. 2
      t/t9162-git-svn-dcommit-interactive.sh
  115. 2
      t/t9164-git-svn-dcommit-concurrent.sh
  116. 1
      t/t9501-gitweb-standalone-http-status.sh
  117. 15
      wt-status.c

3
add-interactive.c

@ -568,8 +568,7 @@ static int get_modified_files(struct repository *r,
run_diff_files(&rev, 0);
}
if (ps)
clear_pathspec(&rev.prune_data);
release_revisions(&rev);
}
hashmap_clear_and_free(&s.file_map, struct pathname_entry, ent);
if (unmerged_count)

20
bisect.c

@ -884,6 +884,7 @@ static int check_ancestors(struct repository *r, int rev_nr,
/* Clean up objects used, as they will be reused. */
clear_commit_marks_many(rev_nr, rev, ALL_REV_FLAGS);
release_revisions(&revs);
return res;
}
@ -964,6 +965,7 @@ static void show_diff_tree(struct repository *r,
setup_revisions(ARRAY_SIZE(argv) - 1, argv, &opt, NULL);
log_tree_commit(&opt, commit);
release_revisions(&opt);
}
/*
@ -1008,7 +1010,7 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
*/
enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
{
struct rev_info revs;
struct rev_info revs = REV_INFO_INIT;
struct commit_list *tried;
int reaches = 0, all = 0, nr, steps;
enum bisect_error res = BISECT_OK;
@ -1033,7 +1035,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
if (res)
return res;
goto cleanup;
bisect_rev_setup(r, &revs, prefix, "%s", "^%s", 1);
@ -1058,14 +1060,16 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
term_good,
term_bad);
return BISECT_FAILED;
res = BISECT_FAILED;
goto cleanup;
}
if (!all) {
fprintf(stderr, _("No testable commit found.\n"
"Maybe you started with bad path arguments?\n"));
return BISECT_NO_TESTABLE_COMMIT;
res = BISECT_NO_TESTABLE_COMMIT;
goto cleanup;
}
bisect_rev = &revs.commits->item->object.oid;
@ -1085,7 +1089,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
* for negative return values for early returns up
* until the cmd_bisect__helper() caller.
*/
return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
res = BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
goto cleanup;
}
nr = all - reaches - 1;
@ -1104,7 +1109,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
/* Clean up objects used, as they will be reused. */
repo_clear_commit_marks(r, ALL_REV_FLAGS);
return bisect_checkout(bisect_rev, no_checkout);
res = bisect_checkout(bisect_rev, no_checkout);
cleanup:
release_revisions(&revs);
return res;
}
static inline int log2i(int n)

3
builtin/add.c

@ -151,7 +151,7 @@ int add_files_to_cache(const char *prefix,
run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
end_odb_transaction();
clear_pathspec(&rev.prune_data);
release_revisions(&rev);
return !!data.add_errors;
}
@ -344,6 +344,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
unlink(file);
free(file);
release_revisions(&rev);
return 0;
}

3
builtin/am.c

@ -1397,6 +1397,7 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
add_pending_object(&rev_info, &commit->object, "");
diff_setup_done(&rev_info.diffopt);
log_tree_commit(&rev_info, commit);
release_revisions(&rev_info);
}
/**
@ -1429,6 +1430,7 @@ static void write_index_patch(const struct am_state *state)
add_pending_object(&rev_info, &tree->object, "");
diff_setup_done(&rev_info.diffopt);
run_diff_index(&rev_info, 1);
release_revisions(&rev_info);
}
/**
@ -1582,6 +1584,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
add_pending_oid(&rev_info, "HEAD", &our_tree, 0);
diff_setup_done(&rev_info.diffopt);
run_diff_index(&rev_info, 1);
release_revisions(&rev_info);
}
if (run_apply(state, index_path))

2
builtin/bisect--helper.c

@ -596,6 +596,7 @@ static int bisect_skipped_commits(struct bisect_terms *terms)
reset_revision_walk();
strbuf_release(&commit_name);
release_revisions(&revs);
fclose(fp);
return 0;
}
@ -1084,6 +1085,7 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
oid_to_hex(&commit->object.oid));
reset_revision_walk();
release_revisions(&revs);
} else {
strvec_push(&argv_state, argv[i]);
}

4
builtin/blame.c

@ -1171,7 +1171,7 @@ parse_done:
if (!incremental)
setup_pager();
else
return 0;
goto cleanup;
blame_sort_final(&sb);
@ -1205,6 +1205,8 @@ parse_done:
printf("num commits: %d\n", sb.num_commits);
}
cleanup:
cleanup_scoreboard(&sb);
release_revisions(&revs);
return 0;
}

3
builtin/checkout.c

@ -629,7 +629,7 @@ static void show_local_changes(struct object *head,
diff_setup_done(&rev.diffopt);
add_pending_object(&rev, head, NULL);
run_diff_index(&rev, 0);
object_array_clear(&rev.pending);
release_revisions(&rev);
}
static void describe_detached_head(const char *msg, struct commit *commit)
@ -1082,6 +1082,7 @@ static void orphaned_commit_warning(struct commit *old_commit, struct commit *ne
/* Clean up objects used, as they will be reused. */
repo_clear_commit_marks(the_repository, ALL_REV_FLAGS);
release_revisions(&revs);
}
static int switch_branches(const struct checkout_opts *opts,

6
builtin/commit.c

@ -1100,7 +1100,6 @@ static const char *find_author_by_nickname(const char *name)
struct rev_info revs;
struct commit *commit;
struct strbuf buf = STRBUF_INIT;
struct string_list mailmap = STRING_LIST_INIT_NODUP;
const char *av[20];
int ac = 0;
@ -1111,7 +1110,8 @@ static const char *find_author_by_nickname(const char *name)
av[++ac] = buf.buf;
av[++ac] = NULL;
setup_revisions(ac, av, &revs, NULL);
revs.mailmap = &mailmap;
revs.mailmap = xmalloc(sizeof(struct string_list));
string_list_init_nodup(revs.mailmap);
read_mailmap(revs.mailmap);
if (prepare_revision_walk(&revs))
@ -1122,7 +1122,7 @@ static const char *find_author_by_nickname(const char *name)
ctx.date_mode.type = DATE_NORMAL;
strbuf_release(&buf);
format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
clear_mailmap(&mailmap);
release_revisions(&revs);
return strbuf_detach(&buf, NULL);
}
die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);

2
builtin/describe.c

@ -517,6 +517,7 @@ static void describe_blob(struct object_id oid, struct strbuf *dst)
traverse_commit_list(&revs, process_commit, process_object, &pcd);
reset_revision_walk();
release_revisions(&revs);
}
static void describe(const char *arg, int last_one)
@ -667,6 +668,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
suffix = NULL;
else
suffix = dirty;
release_revisions(&revs);
}
describe("HEAD", 1);
} else if (dirty) {

8
builtin/diff-files.c

@ -77,8 +77,12 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
perror("read_cache_preload");
return -1;
result = -1;
goto cleanup;
}
cleanup:
result = run_diff_files(&rev, options);
return diff_result_code(&rev.diffopt, result);
result = diff_result_code(&rev.diffopt, result);
release_revisions(&rev);
return result;
}

5
builtin/diff-index.c

@ -70,6 +70,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
return -1;
}
result = run_diff_index(&rev, option);
UNLEAK(rev);
return diff_result_code(&rev.diffopt, result);
result = diff_result_code(&rev.diffopt, result);
release_revisions(&rev);
return result;
}

2
builtin/diff.c

@ -594,7 +594,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
result = diff_result_code(&rev.diffopt, result);
if (1 < rev.diffopt.skip_stat_unmatch)
refresh_index_quietly();
UNLEAK(rev);
release_revisions(&rev);
UNLEAK(ent);
UNLEAK(blob);
return result;

1
builtin/fast-export.c

@ -1276,6 +1276,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
printf("done\n");
refspec_clear(&refspecs);
release_revisions(&revs);
return 0;
}

36
builtin/log.c

@ -231,7 +231,8 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
}
if (mailmap) {
rev->mailmap = xcalloc(1, sizeof(struct string_list));
rev->mailmap = xmalloc(sizeof(struct string_list));
string_list_init_nodup(rev->mailmap);
read_mailmap(rev->mailmap);
}
@ -294,6 +295,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
cmd_log_init_finish(argc, argv, prefix, rev, opt);
}
static int cmd_log_deinit(int ret, struct rev_info *rev)
{
release_revisions(rev);
return ret;
}
/*
* This gives a rough estimate for how many commits we
* will print out in the list.
@ -565,7 +572,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
cmd_log_init(argc, argv, prefix, &rev, &opt);
if (!rev.diffopt.output_format)
rev.diffopt.output_format = DIFF_FORMAT_RAW;
return cmd_log_walk(&rev);
return cmd_log_deinit(cmd_log_walk(&rev), &rev);
}
static void show_tagger(const char *buf, struct rev_info *rev)
@ -689,7 +696,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
cmd_log_init(argc, argv, prefix, &rev, &opt);
if (!rev.no_walk)
return cmd_log_walk(&rev);
return cmd_log_deinit(cmd_log_walk(&rev), &rev);
count = rev.pending.nr;
objects = rev.pending.objects;
@ -749,8 +756,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
rev.diffopt.no_free = 0;
diff_free(&rev.diffopt);
free(objects);
return ret;
return cmd_log_deinit(ret, &rev);
}
/*
@ -778,7 +784,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
rev.always_show_header = 1;
cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
return cmd_log_walk(&rev);
return cmd_log_deinit(cmd_log_walk(&rev), &rev);
}
static void log_setup_revisions_tweak(struct rev_info *rev,
@ -809,7 +815,7 @@ int cmd_log(int argc, const char **argv, const char *prefix)
opt.revarg_opt = REVARG_COMMITTISH;
opt.tweak = log_setup_revisions_tweak;
cmd_log_init(argc, argv, prefix, &rev, &opt);
return cmd_log_walk(&rev);
return cmd_log_deinit(cmd_log_walk(&rev), &rev);
}
/* format-patch */
@ -1764,6 +1770,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
struct commit *commit;
struct commit **list = NULL;
struct rev_info rev;
char *to_free = NULL;
struct setup_revision_opt s_r_opt;
int nr = 0, total, i;
int use_stdout = 0;
@ -1965,7 +1972,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
strbuf_addch(&buf, '\n');
}
rev.extra_headers = strbuf_detach(&buf, NULL);
rev.extra_headers = to_free = strbuf_detach(&buf, NULL);
if (from) {
if (split_ident_line(&rev.from_ident, from, strlen(from)))
@ -2186,8 +2193,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
prepare_bases(&bases, base, list, nr);
}
if (in_reply_to || thread || cover_letter)
rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
if (in_reply_to || thread || cover_letter) {
rev.ref_message_ids = xmalloc(sizeof(*rev.ref_message_ids));
string_list_init_nodup(rev.ref_message_ids);
}
if (in_reply_to) {
const char *msgid = clean_message_id(in_reply_to);
string_list_append(rev.ref_message_ids, msgid);
@ -2294,8 +2303,11 @@ done:
strbuf_release(&rdiff1);
strbuf_release(&rdiff2);
strbuf_release(&rdiff_title);
UNLEAK(rev);
return 0;
free(to_free);
if (rev.ref_message_ids)
string_list_clear(rev.ref_message_ids, 0);
free(rev.ref_message_ids);
return cmd_log_deinit(0, &rev);
}
static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)

2
builtin/merge.c

@ -443,6 +443,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
}
write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
strbuf_release(&out);
release_revisions(&rev);
}
static void finish(struct commit *head_commit,
@ -998,6 +999,7 @@ static int evaluate_result(void)
*/
cnt += count_unmerged_entries();
release_revisions(&rev);
return cnt;
}

2
builtin/pack-objects.c

@ -4464,11 +4464,13 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
read_object_list_from_stdin();
} else if (pfd.have_revs) {
get_object_list(&pfd.revs, rp.nr, rp.v);
release_revisions(&pfd.revs);
} else {
struct rev_info revs;
repo_init_revisions(the_repository, &revs, NULL);
get_object_list(&revs, rp.nr, rp.v);
release_revisions(&revs);
}
cleanup_preferred_base();
if (include_tag && nr_result)

1
builtin/prune.c

@ -196,5 +196,6 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
}
release_revisions(&revs);
return 0;
}

1
builtin/reflog.c

@ -293,6 +293,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
if (verbose)
printf(_("Marking reachable objects..."));
mark_reachable_objects(&revs, 0, 0, NULL);
release_revisions(&revs);
if (verbose)
putchar('\n');
}

25
builtin/rev-list.c

@ -213,10 +213,8 @@ static void show_commit(struct commit *commit, void *data)
static void finish_commit(struct commit *commit)
{
if (commit->parents) {
free_commit_list(commit->parents);
commit->parents = NULL;
}
free_commit_list(commit->parents);
commit->parents = NULL;
free_commit_buffer(the_repository->parsed_objects,
commit);
}
@ -502,6 +500,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
int use_bitmap_index = 0;
int filter_provided_objects = 0;
const char *show_progress = NULL;
int ret = 0;
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(rev_list_usage);
@ -585,7 +584,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
}
if (!strcmp(arg, "--test-bitmap")) {
test_bitmap_walk(&revs);
return 0;
goto cleanup;
}
if (skip_prefix(arg, "--progress=", &arg)) {
show_progress = arg;
@ -674,11 +673,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
if (use_bitmap_index) {
if (!try_bitmap_count(&revs, filter_provided_objects))
return 0;
goto cleanup;
if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
return 0;
goto cleanup;
if (!try_bitmap_traversal(&revs, filter_provided_objects))
return 0;
goto cleanup;
}
if (prepare_revision_walk(&revs))
@ -698,8 +697,10 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
find_bisection(&revs.commits, &reaches, &all, bisect_flags);
if (bisect_show_vars)
return show_bisect_vars(&info, reaches, all);
if (bisect_show_vars) {
ret = show_bisect_vars(&info, reaches, all);
goto cleanup;
}
}
if (filter_provided_objects) {
@ -754,5 +755,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
if (show_disk_usage)
printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
return 0;
cleanup:
release_revisions(&revs);
return ret;
}

8
builtin/shortlog.c

@ -81,8 +81,10 @@ static void insert_one_record(struct shortlog *log,
format_subject(&subject, oneline, " ");
buffer = strbuf_detach(&subject, NULL);
if (!item->util)
item->util = xcalloc(1, sizeof(struct string_list));
if (!item->util) {
item->util = xmalloc(sizeof(struct string_list));
string_list_init_nodup(item->util);
}
string_list_append(item->util, buffer);
}
}
@ -420,6 +422,8 @@ parse_done:
else
get_from_rev(&rev, &log);
release_revisions(&rev);
shortlog_output(&log);
if (log.file != stdout)
fclose(log.file);

115
builtin/stash.c

@ -117,6 +117,10 @@ struct stash_info {
int has_u;
};
#define STASH_INFO_INIT { \
.revision = STRBUF_INIT, \
}
static void free_stash_info(struct stash_info *info)
{
strbuf_release(&info->revision);
@ -158,10 +162,8 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
if (argc == 1)
commit = argv[0];
strbuf_init(&info->revision, 0);
if (!commit) {
if (!ref_exists(ref_stash)) {
free_stash_info(info);
fprintf_ln(stderr, _("No stash entries found."));
return -1;
}
@ -175,11 +177,8 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
revision = info->revision.buf;
if (get_oid(revision, &info->w_commit)) {
error(_("%s is not a valid reference"), revision);
free_stash_info(info);
return -1;
}
if (get_oid(revision, &info->w_commit))
return error(_("%s is not a valid reference"), revision);
assert_stash_like(info, revision);
@ -198,7 +197,7 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
break;
default: /* Invalid or ambiguous */
free_stash_info(info);
break;
}
free(expanded_ref);
@ -616,10 +615,10 @@ restore_untracked:
static int apply_stash(int argc, const char **argv, const char *prefix)
{
int ret;
int ret = -1;
int quiet = 0;
int index = 0;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_BOOL(0, "index", &index,
@ -631,9 +630,10 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
git_stash_apply_usage, 0);
if (get_stash_info(&info, argc, argv))
return -1;
goto cleanup;
ret = do_apply_stash(prefix, &info, index, quiet);
cleanup:
free_stash_info(&info);
return ret;
}
@ -669,20 +669,25 @@ static int do_drop_stash(struct stash_info *info, int quiet)
return 0;
}
static void assert_stash_ref(struct stash_info *info)
static int get_stash_info_assert(struct stash_info *info, int argc,
const char **argv)
{
if (!info->is_stash_ref) {
error(_("'%s' is not a stash reference"), info->revision.buf);
free_stash_info(info);
exit(1);
}
int ret = get_stash_info(info, argc, argv);
if (ret < 0)
return ret;
if (!info->is_stash_ref)
return error(_("'%s' is not a stash reference"), info->revision.buf);
return 0;
}
static int drop_stash(int argc, const char **argv, const char *prefix)
{
int ret;
int ret = -1;
int quiet = 0;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_END()
@ -691,22 +696,21 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options,
git_stash_drop_usage, 0);
if (get_stash_info(&info, argc, argv))
return -1;
assert_stash_ref(&info);
if (get_stash_info_assert(&info, argc, argv))
goto cleanup;
ret = do_drop_stash(&info, quiet);
cleanup:
free_stash_info(&info);
return ret;
}
static int pop_stash(int argc, const char **argv, const char *prefix)
{
int ret;
int ret = -1;
int index = 0;
int quiet = 0;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_BOOL(0, "index", &index,
@ -717,25 +721,25 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options,
git_stash_pop_usage, 0);
if (get_stash_info(&info, argc, argv))
return -1;
if (get_stash_info_assert(&info, argc, argv))
goto cleanup;
assert_stash_ref(&info);
if ((ret = do_apply_stash(prefix, &info, index, quiet)))
printf_ln(_("The stash entry is kept in case "
"you need it again."));
else
ret = do_drop_stash(&info, quiet);
cleanup:
free_stash_info(&info);
return ret;
}
static int branch_stash(int argc, const char **argv, const char *prefix)
{
int ret;
int ret = -1;
const char *branch = NULL;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct child_process cp = CHILD_PROCESS_INIT;
struct option options[] = {
OPT_END()
@ -752,7 +756,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
branch = argv[0];
if (get_stash_info(&info, argc - 1, argv + 1))
return -1;
goto cleanup;
cp.git_cmd = 1;
strvec_pushl(&cp.args, "checkout", "-b", NULL);
@ -764,8 +768,8 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
if (!ret && info.is_stash_ref)
ret = do_drop_stash(&info, 0);
cleanup:
free_stash_info(&info);
return ret;
}
@ -843,8 +847,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
static int show_stash(int argc, const char **argv, const char *prefix)
{
int i;
int ret = 0;
struct stash_info info;
int ret = -1;
struct stash_info info = STASH_INFO_INIT;
struct rev_info rev;
struct strvec stash_args = STRVEC_INIT;
struct strvec revision_args = STRVEC_INIT;
@ -862,6 +866,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
UNTRACKED_ONLY, PARSE_OPT_NONEG),
OPT_END()
};
int do_usage = 0;
init_diff_ui_defaults();
git_config(git_diff_ui_config, NULL);
@ -879,10 +884,8 @@ static int show_stash(int argc, const char **argv, const char *prefix)
strvec_push(&revision_args, argv[i]);
}
ret = get_stash_info(&info, stash_args.nr, stash_args.v);
strvec_clear(&stash_args);
if (ret)
return -1;
if (get_stash_info(&info, stash_args.nr, stash_args.v))
goto cleanup;
/*
* The config settings are applied only if there are not passed
@ -896,16 +899,14 @@ static int show_stash(int argc, const char **argv, const char *prefix)
rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
if (!show_stat && !show_patch) {
free_stash_info(&info);
return 0;
ret = 0;
goto cleanup;
}
}
argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
if (argc > 1) {
free_stash_info(&info);
usage_with_options(git_stash_show_usage, options);
}
if (argc > 1)
goto usage;
if (!rev.diffopt.output_format) {
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
diff_setup_done(&rev.diffopt);
@ -930,8 +931,17 @@ static int show_stash(int argc, const char **argv, const char *prefix)
}
log_tree_diff_flush(&rev);
ret = diff_result_code(&rev.diffopt, 0);
cleanup:
strvec_clear(&stash_args);
free_stash_info(&info);
return diff_result_code(&rev.diffopt, 0);
release_revisions(&rev);
if (do_usage)
usage_with_options(git_stash_show_usage, options);
return ret;
usage:
do_usage = 1;
goto cleanup;
}
static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
@ -1065,7 +1075,6 @@ static int check_changes_tracked_files(const struct pathspec *ps)
goto done;
}
object_array_clear(&rev.pending);
result = run_diff_files(&rev, 0);
if (diff_result_code(&rev.diffopt, result)) {
ret = 1;
@ -1073,7 +1082,7 @@ static int check_changes_tracked_files(const struct pathspec *ps)
}
done:
clear_pathspec(&rev.prune_data);
release_revisions(&rev);
return ret;
}
@ -1284,9 +1293,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
done:
discard_index(&istate);
UNLEAK(rev);
object_array_clear(&rev.pending);
clear_pathspec(&rev.prune_data);
release_revisions(&rev);
strbuf_release(&diff_output);
remove_path(stash_index_path.buf);
return ret;
@ -1428,9 +1435,9 @@ done:
static int create_stash(int argc, const char **argv, const char *prefix)
{
int ret = 0;
int ret;
struct strbuf stash_msg_buf = STRBUF_INIT;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct pathspec ps;
/* Starting with argv[1], since argv[0] is "create" */
@ -1445,6 +1452,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
if (!ret)
printf_ln("%s", oid_to_hex(&info.w_commit));
free_stash_info(&info);
strbuf_release(&stash_msg_buf);
return ret;
}
@ -1453,7 +1461,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
int keep_index, int patch_mode, int include_untracked, int only_staged)
{
int ret = 0;
struct stash_info info;
struct stash_info info = STASH_INFO_INIT;
struct strbuf patch = STRBUF_INIT;
struct strbuf stash_msg_buf = STRBUF_INIT;
struct strbuf untracked_files = STRBUF_INIT;
@ -1652,6 +1660,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
}
done:
free_stash_info(&info);
strbuf_release(&stash_msg_buf);
return ret;
}

14
builtin/submodule--helper.c

@ -649,7 +649,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
{
char *displaypath;
struct strvec diff_files_args = STRVEC_INIT;
struct rev_info rev;
struct rev_info rev = REV_INFO_INIT;
int diff_files_result;
struct strbuf buf = STRBUF_INIT;
const char *git_dir;
@ -736,6 +736,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
cleanup:
strvec_clear(&diff_files_args);
free(displaypath);
release_revisions(&rev);
}
static void status_submodule_cb(const struct cache_entry *list_item,
@ -1114,6 +1115,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
struct strvec diff_args = STRVEC_INIT;
struct rev_info rev;
struct module_cb_list list = MODULE_CB_LIST_INIT;
int ret = 0;
strvec_push(&diff_args, get_diff_cmd(diff_cmd));
if (info->cached)
@ -1139,11 +1141,13 @@ static int compute_summary_module_list(struct object_id *head_oid,
setup_work_tree();
if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
perror("read_cache_preload");
return -1;
ret = -1;
goto cleanup;
}
} else if (read_cache() < 0) {
perror("read_cache");
return -1;
ret = -1;
goto cleanup;
}
if (diff_cmd == DIFF_INDEX)
@ -1151,8 +1155,10 @@ static int compute_summary_module_list(struct object_id *head_oid,
else
run_diff_files(&rev, 0);
prepare_submodule_summary(info, &list);
cleanup:
strvec_clear(&diff_args);
return 0;
release_revisions(&rev);
return ret;
}
static int module_summary(int argc, const char **argv, const char *prefix)

12
bundle.c

@ -196,14 +196,16 @@ int verify_bundle(struct repository *r,
* to be verbose about the errors
*/
struct string_list *p = &header->prerequisites;
struct rev_info revs;
struct rev_info revs = REV_INFO_INIT;
const char *argv[] = {NULL, "--all", NULL};
struct commit *commit;
int i, ret = 0, req_nr;
const char *message = _("Repository lacks these prerequisite commits:");
if (!r || !r->objects || !r->objects->odb)
return error(_("need a repository to verify a bundle"));
if (!r || !r->objects || !r->objects->odb) {
ret = error(_("need a repository to verify a bundle"));
goto cleanup;
}
repo_init_revisions(r, &revs, NULL);
for (i = 0; i < p->nr; i++) {
@ -221,7 +223,7 @@ int verify_bundle(struct repository *r,
error("%s %s", oid_to_hex(oid), name);
}
if (revs.pending.nr != p->nr)
return ret;
goto cleanup;
req_nr = revs.pending.nr;
setup_revisions(2, argv, &revs, NULL);
@ -284,6 +286,8 @@ int verify_bundle(struct repository *r,
printf_ln("The bundle uses this filter: %s",
list_objects_filter_spec(&header->filter));
}
cleanup:
release_revisions(&revs);
return ret;
}

19
commit.c

@ -407,17 +407,14 @@ int parse_commit_buffer(struct repository *r, struct commit *item, const void *b
if (item->object.parsed)
return 0;
if (item->parents) {
/*
* Presumably this is leftover from an earlier failed parse;
* clear it out in preparation for us re-parsing (we'll hit the
* same error, but that's good, since it lets our caller know
* the result cannot be trusted.
*/
free_commit_list(item->parents);
item->parents = NULL;
}
/*
* Presumably this is leftover from an earlier failed parse;
* clear it out in preparation for us re-parsing (we'll hit the
* same error, but that's good, since it lets our caller know
* the result cannot be trusted.
*/
free_commit_list(item->parents);
item->parents = NULL;
tail += size;
if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||

27
contrib/coccinelle/free.cocci

@ -2,13 +2,21 @@
expression E;
@@
- if (E)
(
free(E);
|
free_commit_list(E);
)
@@
expression E;
@@
- if (!E)
(
free(E);
|
free_commit_list(E);
)
@@
expression E;
@ -16,3 +24,22 @@ expression E;
- free(E);
+ FREE_AND_NULL(E);
- E = NULL;
@@
expression E;
@@
- if (E)
- {
free_commit_list(E);
E = NULL;
- }
@@
expression E;
statement S;
@@
- if (E) {
+ if (E)
S
free_commit_list(E);
- }

8
diff-lib.c

@ -641,7 +641,7 @@ int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
if (diff_cache(&revs, tree_oid, NULL, 1))
exit(128);
clear_pathspec(&revs.prune_data);
release_revisions(&revs);
return 0;
}
@ -651,6 +651,7 @@ int index_differs_from(struct repository *r,
{
struct rev_info rev;
struct setup_revision_opt opt;
unsigned has_changes;
repo_init_revisions(r, &rev, NULL);
memset(&opt, 0, sizeof(opt));
@ -662,8 +663,9 @@ int index_differs_from(struct repository *r,
diff_flags_or(&rev.diffopt.flags, flags);
rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
run_diff_index(&rev, 1);
object_array_clear(&rev.pending);
return (rev.diffopt.flags.has_changes != 0);
has_changes = rev.diffopt.flags.has_changes;
release_revisions(&rev);
return (has_changes != 0);
}
static struct strbuf *idiff_prefix_cb(struct diff_options *opt, void *data)

1
fmt-merge-msg.c

@ -699,6 +699,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
shortlog(origins.items[i].string,
origins.items[i].util,
head, &rev, opts, out);
release_revisions(&rev);
}
strbuf_complete_line(out);

3
http-push.c

@ -1689,7 +1689,6 @@ int cmd_main(int argc, const char **argv)
struct refspec rs = REFSPEC_INIT_PUSH;
struct remote_lock *ref_lock = NULL;
struct remote_lock *info_ref_lock = NULL;
struct rev_info revs;
int delete_branch = 0;
int force_delete = 0;
int objects_to_send;
@ -1825,6 +1824,7 @@ int cmd_main(int argc, const char **argv)
new_refs = 0;
for (ref = remote_refs; ref; ref = ref->next) {
struct rev_info revs;
struct strvec commit_argv = STRVEC_INIT;
if (!ref->peer_ref)
@ -1941,6 +1941,7 @@ int cmd_main(int argc, const char **argv)
unlock_remote(ref_lock);
check_locks();
strvec_clear(&commit_argv);
release_revisions(&revs);
}
/* Update remote server info if appropriate */

1
merge-ort.c

@ -1594,6 +1594,7 @@ static int find_first_merges(struct repository *repo,
}
object_array_clear(&merges);
release_revisions(&revs);
return result->nr;
}