git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 14/23] pseudo-merge: fix various memory leaks
Date: Mon, 30 Sep 2024 12:21:27 -0400	[thread overview]
Message-ID: <ZvrQByUPoHhlDMiF@nand.local> (raw)
In-Reply-To: <6e7a272c29577536ba992cc73d736d8f66397607.1727687410.git.ps@pks.im>

On Mon, Sep 30, 2024 at 11:13:51AM +0200, Patrick Steinhardt wrote:
> diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c
> index 4dc0fe8e40..6413dd1731 100644
> --- a/pack-bitmap-write.c
> +++ b/pack-bitmap-write.c
> @@ -64,6 +64,12 @@ static void free_pseudo_merge_commit_idx(struct pseudo_merge_commit_idx *idx)
>  	free(idx);
>  }
>
> +static void pseudo_merge_group_release_cb(void *payload, const char *name UNUSED)
> +{
> +	pseudo_merge_group_release(payload);
> +	free(payload);
> +}
> +
>  void bitmap_writer_free(struct bitmap_writer *writer)
>  {
>  	uint32_t i;
> @@ -82,6 +88,8 @@ void bitmap_writer_free(struct bitmap_writer *writer)
>  	kh_foreach_value(writer->pseudo_merge_commits, idx,
>  			 free_pseudo_merge_commit_idx(idx));
>  	kh_destroy_oid_map(writer->pseudo_merge_commits);
> +	string_list_clear_func(&writer->pseudo_merge_groups,
> +			       pseudo_merge_group_release_cb);

Looking good, and this is the right spot to free the pseudo-merge
groups. Note for other readers: pseudo-merge "groups" are a utility
structure that is only used while writing pseudo-merge bitmaps, so we
don't expect to see any corresponding pseudo_merge_group_release() calls
sprinkled throughout, e.g., pack-bitmap.c.

> diff --git a/pack-bitmap.c b/pack-bitmap.c
> index 9d9b8c4bfb..32b222a7af 100644
> --- a/pack-bitmap.c
> +++ b/pack-bitmap.c
> @@ -1390,8 +1390,8 @@ static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
>  		}
>
>  		base = bitmap_new();
> -		if (!cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap))
> -			bitmap_free(roots_bitmap);
> +		cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap);
> +		bitmap_free(roots_bitmap);

I probably would have pulled this leakfix into its own separate patch,
since it's not immediately obvious how it's related to other changes in
the same patch.

But the change here is definitely correct. We initialize the
roots_bitmap field to just the tips of our traversal. I wrote some
details about why in 11d45a6e6a (pack-bitmap.c: use pseudo-merges during
traversal, 2024-05-23), but the gist is as follows: We want to avoid
accidentally thinking that roots which aren't part of some satisfied
pseudo-merge or existing bitmap are part of the reachability closure,
leaving those bits as dangling and leading to incorrect results.

In any event, we definitely do not need the roots_bitmap outside of that
block (regardless of whether or not we successfully cascaded any
pseudo-merges), so free-ing it here is the right thing to do.

> diff --git a/pseudo-merge.c b/pseudo-merge.c
> index 10ebd9a4e9..28782a31c6 100644
> --- a/pseudo-merge.c
> +++ b/pseudo-merge.c
> @@ -97,6 +97,25 @@ static void pseudo_merge_group_init(struct pseudo_merge_group *group)
>  	group->stable_size = DEFAULT_PSEUDO_MERGE_STABLE_SIZE;
>  }
>
> +void pseudo_merge_group_release(struct pseudo_merge_group *group)
> +{
> +	struct hashmap_iter iter;
> +	struct strmap_entry *e;
> +
> +	regfree(group->pattern);
> +	free(group->pattern);
> +
> +	strmap_for_each_entry(&group->matches, &iter, e) {
> +		struct pseudo_merge_matches *matches = e->value;
> +		free(matches->stable);
> +		free(matches->unstable);
> +		free(matches);
> +	}
> +	strmap_clear(&group->matches, 0);
> +
> +	free(group->merges);
> +}
> +

All looks good here, I didn't see any fields that were missed or
over-eagerly free'd here.

Thanks,
Taylor

  reply	other threads:[~2024-09-30 16:21 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30  9:13 [PATCH 00/23] Memory leak fixes (pt.8) Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 01/23] builtin/annotate: fix leaking args vector Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 02/23] read-cache: fix leaking hash context in `do_write_index()` Patrick Steinhardt
2024-09-30 14:05   ` Derrick Stolee
2024-09-30  9:13 ` [PATCH 03/23] scalar: fix leaking repositories Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 04/23] shell: fix leaking strings Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 05/23] wt-status: fix leaking buffer with sparse directories Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 06/23] submodule: fix leaking submodule entry list Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 07/23] builtin/stash: fix leaking `pathspec_from_file` Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 08/23] builtin/pack-redundant: fix various memory leaks Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 09/23] builtin/clone: fix leaking repo state when cloning with bundle URIs Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 10/23] t/helper: fix leaking repository in partial-clone helper Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 11/23] builtin/revert: fix leaking `gpg_sign` and `strategy` config Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 12/23] diff: improve lifecycle management of diff queues Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 13/23] line-log: fix several memory leaks Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 14/23] pseudo-merge: fix various " Patrick Steinhardt
2024-09-30 16:21   ` Taylor Blau [this message]
2024-09-30  9:13 ` [PATCH 15/23] pseudo-merge: fix leaking strmap keys Patrick Steinhardt
2024-09-30 21:22   ` Taylor Blau
2024-10-07  9:41     ` Patrick Steinhardt
2024-10-08  8:54       ` Jeff King
2024-10-09  7:06         ` Patrick Steinhardt
2024-09-30  9:13 ` [PATCH 16/23] pack-bitmap-write: fix leaking OID array Patrick Steinhardt
2024-09-30 21:22   ` Taylor Blau
2024-09-30  9:14 ` [PATCH 17/23] midx-write: fix leaking buffer Patrick Steinhardt
2024-09-30 21:27   ` Taylor Blau
2024-10-07  9:41     ` Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 18/23] revision: fix memory leaks when rewriting parents Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 19/23] revision: fix leaking saved parents Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 20/23] pack-write: fix return parameter of `write_rev_file_order()` Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 21/23] t/helper: fix leaks in proc-receive helper Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 22/23] remote: fix leaking push reports Patrick Steinhardt
2024-09-30  9:14 ` [PATCH 23/23] builtin/send-pack: fix leaking list of push options Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZvrQByUPoHhlDMiF@nand.local \
    --to=me@ttaylorr.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).