Leakfix with code clean-up.

* en/dir-clear:
  dir: fix problematic API to avoid memory leaks
  dir: make clear_directory() free all relevant memory
This commit is contained in:
Junio C Hamano 2020-08-24 14:54:33 -07:00
Родитель 51226147d1 eceba53214
Коммит ad00f44f54
10 изменённых файлов: 46 добавлений и 40 удалений

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

@ -534,11 +534,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
die_in_unpopulated_submodule(&the_index, prefix);
die_path_inside_submodule(&the_index, &pathspec);
dir_init(&dir);
if (add_new_files) {
int baselen;
/* Set up the default git porcelain excludes */
memset(&dir, 0, sizeof(dir));
if (!ignored_too) {
dir.flags |= DIR_COLLECT_IGNORED;
setup_standard_excludes(&dir);
@ -611,7 +611,7 @@ finish:
COMMIT_LOCK | SKIP_IF_UNCHANGED))
die(_("Unable to write new index file"));
dir_clear(&dir);
UNLEAK(pathspec);
UNLEAK(dir);
return exit_status;
}

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

@ -180,7 +180,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
if (!no_index && read_cache() < 0)
die(_("index file corrupt"));
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
setup_standard_excludes(&dir);
if (stdin_paths) {
@ -190,7 +190,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
maybe_flush_or_die(stdout, "ignore to stdout");
}
clear_directory(&dir);
dir_clear(&dir);
return !num_ignored;
}

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

@ -667,7 +667,7 @@ static int filter_by_patterns_cmd(void)
if (!confirm.len)
break;
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
ignore_list = strbuf_split_max(&confirm, ' ', 0);
@ -698,7 +698,7 @@ static int filter_by_patterns_cmd(void)
}
strbuf_list_free(ignore_list);
clear_directory(&dir);
dir_clear(&dir);
}
strbuf_release(&confirm);
@ -923,7 +923,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
0);
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
if (!interactive && !dry_run && !force) {
if (config_set)
die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
@ -1021,11 +1021,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
string_list_append(&del_list, rel);
}
for (i = 0; i < dir.nr; i++)
free(dir.entries[i]);
for (i = 0; i < dir.ignored_nr; i++)
free(dir.ignored[i]);
dir_clear(&dir);
if (interactive && del_list.nr > 0)
interactive_main_loop();

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

@ -693,7 +693,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
struct dir_struct dir;
int i, hit = 0;
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
if (!use_index)
dir.flags |= DIR_NO_GITLINKS;
if (exc_std)
@ -705,6 +705,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
if (hit && opt->status_only)
break;
}
dir_clear(&dir);
return hit;
}

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

@ -584,7 +584,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(ls_files_usage, builtin_ls_files_options);
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
prefix = cmd_prefix;
if (prefix)
prefix_len = strlen(prefix);
@ -688,6 +688,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
return bad ? 1 : 0;
}
UNLEAK(dir);
dir_clear(&dir);
return 0;
}

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

@ -864,7 +864,7 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
int found = 0;
struct dir_struct dir;
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
if (include_untracked != INCLUDE_ALL_FILES)
setup_standard_excludes(&dir);
@ -875,12 +875,9 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
strbuf_addstr(untracked_files, ent->name);
/* NUL-terminate: will be fed to update-index -z */
strbuf_addch(untracked_files, '\0');
free(ent);
}
free(dir.entries);
free(dir.ignored);
clear_directory(&dir);
dir_clear(&dir);
return found;
}

20
dir.c
Просмотреть файл

@ -54,6 +54,11 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
static int resolve_dtype(int dtype, struct index_state *istate,
const char *path, int len);
void dir_init(struct dir_struct *dir)
{
memset(dir, 0, sizeof(*dir));
}
int count_slashes(const char *s)
{
int cnt = 0;
@ -3012,10 +3017,10 @@ int remove_path(const char *name)
}
/*
* Frees memory within dir which was allocated for exclude lists and
* the exclude_stack. Does not free dir itself.
* Frees memory within dir which was allocated, and resets fields for further
* use. Does not free dir itself.
*/
void clear_directory(struct dir_struct *dir)
void dir_clear(struct dir_struct *dir)
{
int i, j;
struct exclude_list_group *group;
@ -3033,6 +3038,13 @@ void clear_directory(struct dir_struct *dir)
free(group->pl);
}
for (i = 0; i < dir->ignored_nr; i++)
free(dir->ignored[i]);
for (i = 0; i < dir->nr; i++)
free(dir->entries[i]);
free(dir->ignored);
free(dir->entries);
stk = dir->exclude_stack;
while (stk) {
struct exclude_stack *prev = stk->prev;
@ -3040,6 +3052,8 @@ void clear_directory(struct dir_struct *dir)
stk = prev;
}
strbuf_release(&dir->basebuf);
dir_init(dir);
}
struct ondisk_untracked_cache {

21
dir.h
Просмотреть файл

@ -19,24 +19,23 @@
* CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have
* loaded the index first.
*
* - Prepare `struct dir_struct dir` and clear it with `memset(&dir, 0,
* sizeof(dir))`.
* - Prepare `struct dir_struct dir` using `dir_init()` function.
*
* - To add single exclude pattern, call `add_pattern_list()` and then
* `add_pattern()`.
*
* - To add patterns from a file (e.g. `.git/info/exclude`), call
* `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`. A
* short-hand function `setup_standard_excludes()` can be used to set
* up the standard set of exclude settings.
* `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
*
* - Set options described in the Data Structure section above.
* - A short-hand function `setup_standard_excludes()` can be used to set
* up the standard set of exclude settings, instead of manually calling
* the add_pattern*() family of functions.
*
* - Call `read_directory()`.
* - Call `fill_directory()`.
*
* - Use `dir.entries[]`.
* - Use `dir.entries[]` and `dir.ignored[]`.
*
* - Call `clear_directory()` when none of the contained elements are no longer in use.
* - Call `dir_clear()` when the contained elements are no longer in use.
*
*/
@ -362,6 +361,8 @@ int match_pathspec(const struct index_state *istate,
int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
int within_depth(const char *name, int namelen, int depth, int max_depth);
void dir_init(struct dir_struct *dir);
int fill_directory(struct dir_struct *dir,
struct index_state *istate,
const struct pathspec *pathspec);
@ -428,7 +429,7 @@ void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, i
void add_pattern(const char *string, const char *base,
int baselen, struct pattern_list *pl, int srcpos);
void clear_pattern_list(struct pattern_list *pl);
void clear_directory(struct dir_struct *dir);
void dir_clear(struct dir_struct *dir);
int repo_file_exists(struct repository *repo, const char *path);
int file_exists(const char *);

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

@ -80,8 +80,8 @@ int checkout_fast_forward(struct repository *r,
}
memset(&opts, 0, sizeof(opts));
dir_init(&dir);
if (overwrite_ignore) {
memset(&dir, 0, sizeof(dir));
dir.flags |= DIR_SHOW_IGNORED;
setup_standard_excludes(&dir);
opts.dir = &dir;
@ -102,6 +102,7 @@ int checkout_fast_forward(struct repository *r,
clear_unpack_trees_porcelain(&opts);
return -1;
}
dir_clear(&dir);
clear_unpack_trees_porcelain(&opts);
if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))

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

@ -703,7 +703,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
if (!s->show_untracked_files)
return;
memset(&dir, 0, sizeof(dir));
dir_init(&dir);
if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
dir.flags |=
DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
@ -724,19 +724,15 @@ static void wt_status_collect_untracked(struct wt_status *s)
struct dir_entry *ent = dir.entries[i];
if (index_name_is_other(istate, ent->name, ent->len))
string_list_insert(&s->untracked, ent->name);
free(ent);
}
for (i = 0; i < dir.ignored_nr; i++) {
struct dir_entry *ent = dir.ignored[i];
if (index_name_is_other(istate, ent->name, ent->len))
string_list_insert(&s->ignored, ent->name);
free(ent);
}
free(dir.entries);
free(dir.ignored);
clear_directory(&dir);
dir_clear(&dir);
if (advice_status_u_option)
s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;