Skip to content

Commit

Permalink
Merge branch 'ps/leakfixes-part-10' into seen
Browse files Browse the repository at this point in the history
* ps/leakfixes-part-10: (26 commits)
  t: remove TEST_PASSES_SANITIZE_LEAK annotations
  test-lib: unconditionally enable leak checking
  t: remove unneeded !SANITIZE_LEAK prerequisites
  t: mark some tests as leak free
  t5601: work around leak sanitizer issue
  git-compat-util: drop `UNLEAK()` annotation
  t/helper: fix leaking commit graph in "read-graph" subcommand
  builtin/branch: fix leaking sorting options
  builtin/init-db: fix leaking directory paths
  builtin/help: fix leaks in `check_git_cmd()`
  help: fix leaking return value from `help_unknown_cmd()`
  help: fix leaking `struct cmdnames`
  help: refactor to not use globals for reading config
  builtin/sparse-checkout: fix leaking sanitized patterns
  split-index: fix memory leak in `move_cache_to_base_index()`
  git: refactor builtin handling to use a `struct strvec`
  git: refactor alias handling to use a `struct strvec`
  strvec: introduce new `strvec_splice()` function
  line-log: fix leak when rewriting commit parents
  bisect: fix various cases where we leak commit list items
  ...
  • Loading branch information
gitster committed Nov 8, 2024
2 parents 02d70bd + ea42eb7 commit a705ae7
Show file tree
Hide file tree
Showing 951 changed files with 379 additions and 1,256 deletions.
61 changes: 44 additions & 17 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static struct oid_array skipped_revs;

static struct object_id *current_bad_oid;

static const char *term_bad;
static const char *term_good;
static char *term_bad;
static char *term_good;

/* Remember to update object flag allocation in object.h */
#define COUNTED (1u<<16)
Expand Down Expand Up @@ -440,11 +440,19 @@ void find_bisection(struct commit_list **commit_list, int *reaches,
free_commit_list(list->next);
best = list;
best->next = NULL;
} else {
for (p = list; p != best; p = next) {
next = p->next;
free(p);
}
}
*reaches = weight(best);
} else {
free_commit_list(*commit_list);
}
free(weights);
*commit_list = best;

free(weights);
clear_commit_weight(&commit_weight);
}

Expand All @@ -456,6 +464,7 @@ static int register_ref(const char *refname, const char *referent UNUSED, const
strbuf_addstr(&good_prefix, "-");

if (!strcmp(refname, term_bad)) {
free(current_bad_oid);
current_bad_oid = xmalloc(sizeof(*current_bad_oid));
oidcpy(current_bad_oid, oid);
} else if (starts_with(refname, good_prefix.buf)) {
Expand Down Expand Up @@ -556,8 +565,11 @@ struct commit_list *filter_skipped(struct commit_list *list,
tried = &list->next;
} else {
if (!show_all) {
if (!skipped_first || !*skipped_first)
if (!skipped_first || !*skipped_first) {
free_commit_list(next);
free_commit_list(filtered);
return list;
}
} else if (skipped_first && !*skipped_first) {
/* This means we know it's not skipped */
*skipped_first = -1;
Expand Down Expand Up @@ -613,7 +625,7 @@ static int sqrti(int val)

static struct commit_list *skip_away(struct commit_list *list, int count)
{
struct commit_list *cur, *previous;
struct commit_list *cur, *previous, *result = list;
int prn, index, i;

prn = get_prn(count);
Expand All @@ -625,15 +637,23 @@ static struct commit_list *skip_away(struct commit_list *list, int count)
for (i = 0; cur; cur = cur->next, i++) {
if (i == index) {
if (!oideq(&cur->item->object.oid, current_bad_oid))
return cur;
if (previous)
return previous;
return list;
result = cur;
else if (previous)
result = previous;
else
result = list;
break;
}
previous = cur;
}

return list;
for (cur = list; cur != result; ) {
struct commit_list *next = cur->next;
free(cur);
cur = next;
}

return result;
}

static struct commit_list *managed_skipped(struct commit_list *list,
Expand Down Expand Up @@ -801,6 +821,8 @@ static enum bisect_error handle_bad_merge_base(void)
"between %s and [%s].\n"),
bad_hex, term_bad, term_good, bad_hex, good_hex);
}

free(good_hex);
return BISECT_MERGE_BASE_CHECK;
}

Expand Down Expand Up @@ -848,8 +870,8 @@ static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int
rev + 1, &result) < 0)
exit(128);

for (; result; result = result->next) {
const struct object_id *mb = &result->item->object.oid;
for (struct commit_list *l = result; l; l = l->next) {
const struct object_id *mb = &l->item->object.oid;
if (oideq(mb, current_bad_oid)) {
res = handle_bad_merge_base();
break;
Expand Down Expand Up @@ -985,24 +1007,28 @@ static void show_commit(struct commit *commit)
* We read them and store them to adapt the messages accordingly.
* Default is bad/good.
*/
void read_bisect_terms(const char **read_bad, const char **read_good)
void read_bisect_terms(char **read_bad, char **read_good)
{
struct strbuf str = STRBUF_INIT;
const char *filename = git_path_bisect_terms();
FILE *fp = fopen(filename, "r");

if (!fp) {
if (errno == ENOENT) {
*read_bad = "bad";
*read_good = "good";
free(*read_bad);
*read_bad = xstrdup("bad");
free(*read_good);
*read_good = xstrdup("good");
return;
} else {
die_errno(_("could not read file '%s'"), filename);
}
} else {
strbuf_getline_lf(&str, fp);
free(*read_bad);
*read_bad = strbuf_detach(&str, NULL);
strbuf_getline_lf(&str, fp);
free(*read_good);
*read_good = strbuf_detach(&str, NULL);
}
strbuf_release(&str);
Expand All @@ -1024,7 +1050,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
{
struct strvec rev_argv = STRVEC_INIT;
struct rev_info revs = REV_INFO_INIT;
struct commit_list *tried;
struct commit_list *tried = NULL;
int reaches = 0, all = 0, nr, steps;
enum bisect_error res = BISECT_OK;
struct object_id *bisect_rev;
Expand Down Expand Up @@ -1091,7 +1117,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
if (oideq(bisect_rev, current_bad_oid)) {
res = error_if_skipped_commits(tried, current_bad_oid);
if (res)
return res;
goto cleanup;
printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
term_bad);

Expand Down Expand Up @@ -1125,6 +1151,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)

res = bisect_checkout(bisect_rev, no_checkout);
cleanup:
free_commit_list(tried);
release_revisions(&revs);
strvec_clear(&rev_argv);
return res;
Expand Down
2 changes: 1 addition & 1 deletion bisect.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix);

int estimate_bisect_steps(int all);

void read_bisect_terms(const char **bad, const char **good);
void read_bisect_terms(char **bad, char **good);

int bisect_clean_state(void);

Expand Down
1 change: 1 addition & 0 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2931,6 +2931,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)
void cleanup_scoreboard(struct blame_scoreboard *sb)
{
free(sb->lineno);
free(sb->final_buf);
clear_prio_queue(&sb->commits);
oidset_clear(&sb->ignore_list);

Expand Down
2 changes: 1 addition & 1 deletion blame.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct blame_scoreboard {
* Used by many functions to obtain contents of the nth line,
* indexed with scoreboard.lineno[blame_entry.lno].
*/
const char *final_buf;
char *final_buf;
unsigned long final_buf_size;

/* linked list of blames */
Expand Down
12 changes: 6 additions & 6 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,12 +1216,6 @@ int cmd_blame(int argc,
output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);

output(&sb, output_option);
free((void *)sb.final_buf);
for (ent = sb.ent; ent; ) {
struct blame_entry *e = ent->next;
free(ent);
ent = e;
}

if (show_stats) {
printf("num read blob: %d\n", sb.num_read_blob);
Expand All @@ -1230,6 +1224,12 @@ int cmd_blame(int argc,
}

cleanup:
for (ent = sb.ent; ent; ) {
struct blame_entry *e = ent->next;
free(ent);
ent = e;
}

free(path);
cleanup_scoreboard(&sb);
release_revisions(&revs);
Expand Down
33 changes: 22 additions & 11 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ int cmd_branch(int argc,
static struct ref_sorting *sorting;
struct string_list sorting_options = STRING_LIST_INIT_DUP;
struct ref_format format = REF_FORMAT_INIT;
int ret;

struct option options[] = {
OPT_GROUP(N_("Generic options")),
Expand Down Expand Up @@ -851,15 +852,15 @@ int cmd_branch(int argc,
if (list)
setup_auto_pager("branch", 1);

UNLEAK(sorting_options);

if (delete) {
if (!argc)
die(_("branch name required"));
return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
ret = delete_branches(argc, argv, delete > 1, filter.kind, quiet);
goto out;
} else if (show_current) {
print_current_branch_name();
return 0;
ret = 0;
goto out;
} else if (list) {
/* git branch --list also shows HEAD when it is detached */
if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
Expand All @@ -882,12 +883,13 @@ int cmd_branch(int argc,
ref_sorting_release(sorting);
ref_filter_clear(&filter);
ref_format_clear(&format);
return 0;

ret = 0;
goto out;
} else if (edit_description) {
const char *branch_name;
struct strbuf branch_ref = STRBUF_INIT;
struct strbuf buf = STRBUF_INIT;
int ret = 1; /* assume failure */

if (!argc) {
if (filter.detached)
Expand All @@ -901,18 +903,22 @@ int cmd_branch(int argc,
}

strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf))
if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf)) {
error((!argc || branch_checked_out(branch_ref.buf))
? _("no commit on branch '%s' yet")
: _("no branch named '%s'"),
branch_name);
else if (!edit_branch_description(branch_name))
ret = 1;
} else if (!edit_branch_description(branch_name)) {
ret = 0; /* happy */
} else {
ret = 1;
}

strbuf_release(&branch_ref);
strbuf_release(&buf);

return ret;
goto out;
} else if (copy || rename) {
if (!argc)
die(_("branch name required"));
Expand Down Expand Up @@ -1000,12 +1006,17 @@ int cmd_branch(int argc,
create_branches_recursively(the_repository, branch_name,
start_name, NULL, force,
reflog, quiet, track, 0);
return 0;
ret = 0;
goto out;
}
create_branch(the_repository, branch_name, start_name, force, 0,
reflog, quiet, track, 0);
} else
usage_with_options(builtin_branch_usage, options);

return 0;
ret = 0;

out:
string_list_clear(&sorting_options, 0);
return ret;
}
1 change: 0 additions & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,6 @@ int cmd_clone(int argc,
free(dir);
free(path);
free(repo_to_free);
UNLEAK(repo);
junk_mode = JUNK_LEAVE_ALL;

transport_ls_refs_options_release(&transport_ls_refs_options);
Expand Down
1 change: 0 additions & 1 deletion builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,5 @@ int cmd_diff(int argc,
release_revisions(&rev);
object_array_clear(&ent);
symdiff_release(&sdiff);
UNLEAK(blob);
return result;
}
13 changes: 7 additions & 6 deletions builtin/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,12 @@ static void show_html_page(const char *page)
open_html(page_path.buf);
}

static const char *check_git_cmd(const char* cmd)
static char *check_git_cmd(const char *cmd)
{
char *alias;

if (is_git_command(cmd))
return cmd;
return xstrdup(cmd);

alias = alias_lookup(cmd);
if (alias) {
Expand Down Expand Up @@ -589,14 +589,13 @@ static const char *check_git_cmd(const char* cmd)
die(_("bad alias.%s string: %s"), cmd,
split_cmdline_strerror(count));
free(argv);
UNLEAK(alias);
return alias;
}

if (exclude_guides)
return help_unknown_cmd(cmd);

return cmd;
return xstrdup(cmd);
}

static void no_help_format(const char *opt_mode, enum help_format fmt)
Expand Down Expand Up @@ -642,6 +641,7 @@ int cmd_help(int argc,
{
int nongit;
enum help_format parsed_help_format;
char *command = NULL;
const char *page;

argc = parse_options(argc, argv, prefix, builtin_help_options,
Expand Down Expand Up @@ -713,9 +713,9 @@ int cmd_help(int argc,
if (help_format == HELP_FORMAT_NONE)
help_format = parse_help_format(DEFAULT_HELP_FORMAT);

argv[0] = check_git_cmd(argv[0]);
command = check_git_cmd(argv[0]);

page = cmd_to_page(argv[0]);
page = cmd_to_page(command);
switch (help_format) {
case HELP_FORMAT_NONE:
case HELP_FORMAT_MAN:
Expand All @@ -729,5 +729,6 @@ int cmd_help(int argc,
break;
}

free(command);
return 0;
}
Loading

0 comments on commit a705ae7

Please sign in to comment.