]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pseudo-merge: fix leaking strmap keys
authorPatrick Steinhardt <ps@pks.im>
Mon, 30 Sep 2024 09:13:53 +0000 (11:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Sep 2024 18:23:06 +0000 (11:23 -0700)
When creating a new pseudo-merge group we collect a set of matchnig
commits and put them into a string map. This strmap is initialized such
that it does not allocate its keys, and instead we try to pass ownership
of the keys to it via `strmap_put()`. This isn't how it works though:
the strmap will never try to release these keys, and consequently they
end up leaking.

Fix this leak by initializing the strmap as duplicating its keys and not
trying to hand over ownership.

The leak is exposed by t5333, but plugging it does not yet make the full
test suite pass.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pseudo-merge.c

index 28782a31c6e08692c53d7557dfa1c80b409b6d08..bb59965ed26d5bbddb17149f8460bfe175722898 100644 (file)
@@ -87,7 +87,7 @@ static void pseudo_merge_group_init(struct pseudo_merge_group *group)
 {
        memset(group, 0, sizeof(struct pseudo_merge_group));
 
-       strmap_init_with_options(&group->matches, NULL, 0);
+       strmap_init_with_options(&group->matches, NULL, 1);
 
        group->decay = DEFAULT_PSEUDO_MERGE_DECAY;
        group->max_merges = DEFAULT_PSEUDO_MERGE_MAX_MERGES;
@@ -275,7 +275,7 @@ static int find_pseudo_merge_group_for_ref(const char *refname,
                matches = strmap_get(&group->matches, group_name.buf);
                if (!matches) {
                        matches = xcalloc(1, sizeof(*matches));
-                       strmap_put(&group->matches, strbuf_detach(&group_name, NULL),
+                       strmap_put(&group->matches, group_name.buf,
                                   matches);
                }