]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir.c: free removed sparse-pattern hashmap entries
authorJeff King <peff@peff.net>
Tue, 4 Jun 2024 10:13:20 +0000 (06:13 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 4 Jun 2024 17:38:23 +0000 (10:38 -0700)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
t/t7002-mv-sparse-checkout.sh

diff --git a/dir.c b/dir.c
index 9cc4a68fbce3c1be6a24e71da829650e27ba23cd..d812d521b0b2e024d7c64c425e94a573fe83f845 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -810,6 +810,8 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
 
        if (given->patternlen > 2 &&
            !strcmp(given->pattern + given->patternlen - 2, "/*")) {
+               struct pattern_entry *old;
+
                if (!(given->flags & PATTERN_FLAG_NEGATIVE)) {
                        /* Not a cone pattern. */
                        warning(_("unrecognized pattern: '%s'"), given->pattern);
@@ -835,7 +837,11 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
                }
 
                hashmap_add(&pl->parent_hashmap, &translated->ent);
-               hashmap_remove(&pl->recursive_hashmap, &translated->ent, &data);
+               old = hashmap_remove_entry(&pl->recursive_hashmap, translated, ent, &data);
+               if (old) {
+                       free(old->pattern);
+                       free(old);
+               }
                free(data);
                return;
        }
index 26582ae4e5fb5ebb6aebb6239ecd3ab711b119ed..57969ce805a54839fe18bfb3bea63faacb1dd85a 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git mv in sparse working trees'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 setup_sparse_checkout () {