path: root/builtin/check-ignore.c
diff options
authorElijah Newren <>2020-08-18 22:58:26 (GMT)
committerJunio C Hamano <>2020-08-19 00:17:31 (GMT)
commiteceba5321410bbcc9e0b11e6aa479832f574eca8 (patch)
treecd31d4f411acc27ef42499dc5331c683fe022a90 /builtin/check-ignore.c
parentdad4f23ce59339bb32ad9e1cc1682c696f7a724f (diff)
dir: fix problematic API to avoid memory leaks
The dir structure seemed to have a number of leaks and problems around it. First I noticed that parent_hashmap and recursive_hashmap were being leaked (though Peff noticed and submitted fixes before me). Then I noticed in the previous commit that clear_directory() was only taking responsibility for a subset of fields within dir_struct, despite the fact that entries[] and ignored[] we allocated internally to dir.c. That, of course, resulted in many callers either leaking or haphazardly trying to free these arrays and their contents. Digging further, I found that despite the pretty clear documentation near the top of dir.h that folks were supposed to call clear_directory() when the user no longer needed the dir_struct, there were four callers that didn't bother doing that at all. However, two of them clearly thought about leaks since they had an UNLEAK(dir) directive, which to me suggests that the method to free the data was too unclear. I suspect the non-obviousness of the API and its holes led folks to avoid it, which then snowballed into further problems with the entries[], ignored[], parent_hashmap, and recursive_hashmap problems. Rename clear_directory() to dir_clear() to be more in line with other data structures in git, and introduce a dir_init() to handle the suggested memsetting of dir_struct to all zeroes. I hope that a name like "dir_clear()" is more clear, and that the presence of dir_init() will provide a hint to those looking at the code that they need to look for either a dir_clear() or a dir_free() and lead them to find dir_clear(). Signed-off-by: Elijah Newren <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/check-ignore.c')
1 files changed, 2 insertions, 2 deletions
diff --git a/builtin/check-ignore.c b/builtin/check-ignore.c
index ea5d0ae..3c65274 100644
--- a/builtin/check-ignore.c
+++ b/builtin/check-ignore.c
@@ -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);
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;