Skip to content

Commit 4c844c2

Browse files
peffgitster
authored andcommitted
dir.c: free removed sparse-pattern hashmap entries
In add_pattern_to_hashsets(), we remove entries from the recursive_hashmap when adding similar ones to the parent_hashmap. I won't pretend to understand all of what's going on here, but there's an obvious leak: whatever we removed from recursive_hashmap is not referenced anywhere else, and is never free()d. We can easily fix this by asking the hashmap to return a pointer to the old entry. This makes t7002 now completely leak-free. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent db83b64 commit 4c844c2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

dir.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
810810

811811
if (given->patternlen > 2 &&
812812
!strcmp(given->pattern + given->patternlen - 2, "/*")) {
813+
struct pattern_entry *old;
814+
813815
if (!(given->flags & PATTERN_FLAG_NEGATIVE)) {
814816
/* Not a cone pattern. */
815817
warning(_("unrecognized pattern: '%s'"), given->pattern);
@@ -835,7 +837,11 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
835837
}
836838

837839
hashmap_add(&pl->parent_hashmap, &translated->ent);
838-
hashmap_remove(&pl->recursive_hashmap, &translated->ent, &data);
840+
old = hashmap_remove_entry(&pl->recursive_hashmap, translated, ent, &data);
841+
if (old) {
842+
free(old->pattern);
843+
free(old);
844+
}
839845
free(data);
840846
return;
841847
}

t/t7002-mv-sparse-checkout.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git mv in sparse working trees'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
setup_sparse_checkout () {

0 commit comments

Comments
 (0)