From: Jeff King <peff@peff•net>
To: LorenzoPegorari <lorenzo.pegorari2002@gmail•com>
Cc: git@vger•kernel.org, Taylor Blau <me@ttaylorr•com>,
Junio C Hamano <gitster@pobox•com>,
Patrick Steinhardt <ps@pks•im>, fox <fox.gbr@townlong-yak•com>
Subject: Re: [PATCH v2] http: fix memory leak in fetch_and_setup_pack_index()
Date: Fri, 29 May 2026 01:40:24 -0400 [thread overview]
Message-ID: <20260529054024.GA1104383@coredump.intra.peff.net> (raw)
In-Reply-To: <20260529053659.GC1099450@coredump.intra.peff.net>
On Fri, May 29, 2026 at 01:36:59AM -0400, Jeff King wrote:
> But it _could_ be done as a preparatory patch. And the rationale for
> doing that on its own I think is roughly:
>
> 1. It is mostly doing nothing, because 63aca3f7f1 registered it as a
> tempfile, so it will be cleaned up at process end anyway (whether
> we succeed in fetching it or not).
>
> 2. It is maybe a little harmful, because we are going to unlink() it
> now, and then later the tempfile code will try to unlink() it again
> (so a simultaneous fetch could have created the same file).
BTW, for (2) I wondered about going in the opposite direction. If we
actually passed the tempfile back up, like in the patch below, then we
could use delete_tempfile() to do the unlink (and remove it from the
tempfile list).
And then your patch would want to similarly delete_tempfile() in its
error path.
But I don't think it really buys us much. _If_ we were going to keep
passing the tempfile struct up the call stack on success, then we could
store it and call delete_tempfile() as soon as we had ran index-pack on
it. But that's even more surgery, for again little gain (we delete our
tempfiles a little earlier, rather than at process end).
So I'm inclined to go in the direction that shortens the code. ;)
-Peff
---
diff --git a/http.c b/http.c
index ea9b16861b..e83a3857b3 100644
--- a/http.c
+++ b/http.c
@@ -2546,9 +2546,10 @@ int http_fetch_ref(const char *base, struct ref *ref)
}
/* Helpers for fetching packs */
-static char *fetch_pack_index(unsigned char *hash, const char *base_url)
+static struct tempfile *fetch_pack_index(unsigned char *hash, const char *base_url)
{
char *url, *tmp;
+ struct tempfile *ret;
struct strbuf buf = STRBUF_INIT;
if (http_is_verbose)
@@ -2575,23 +2576,24 @@ static char *fetch_pack_index(unsigned char *hash, const char *base_url)
tmp = xstrfmt("%s/tmp_pack_%s.idx",
repo_get_object_directory(the_repository),
hash_to_hex(hash));
- register_tempfile(tmp);
+ ret = register_tempfile(tmp);
+ free(tmp);
- if (http_get_file(url, tmp, NULL) != HTTP_OK) {
+ if (http_get_file(url, ret->filename.buf, NULL) != HTTP_OK) {
error("Unable to get pack index %s", url);
- FREE_AND_NULL(tmp);
+ delete_tempfile(&ret);
}
free(url);
- return tmp;
+ return ret;
}
static int fetch_and_setup_pack_index(struct packfile_list *packs,
unsigned char *sha1,
const char *base_url)
{
struct packed_git *new_pack, *p;
- char *tmp_idx = NULL;
+ struct tempfile *tmp_idx;
int ret;
/*
@@ -2607,11 +2609,9 @@ static int fetch_and_setup_pack_index(struct packfile_list *packs,
if (!tmp_idx)
return -1;
- new_pack = parse_pack_index(the_repository, sha1, tmp_idx);
+ new_pack = parse_pack_index(the_repository, sha1, tmp_idx->filename.buf);
if (!new_pack) {
- unlink(tmp_idx);
- free(tmp_idx);
-
+ delete_tempfile(&tmp_idx);
return -1; /* parse_pack_index() already issued error message */
}
next prev parent reply other threads:[~2026-05-29 5:40 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-19 14:54 [PATCH] http: fix memory leak in fetch_and_setup_pack_index() LorenzoPegorari
2026-05-19 19:17 ` Jeff King
2026-05-28 1:22 ` Lorenzo Pegorari
2026-05-29 5:32 ` Jeff King
2026-05-28 23:49 ` [PATCH v2] " LorenzoPegorari
2026-05-29 5:36 ` Jeff King
2026-05-29 5:40 ` Jeff King [this message]
2026-06-01 13:34 ` Lorenzo Pegorari
2026-06-01 13:27 ` Lorenzo Pegorari
2026-06-01 13:51 ` [PATCH v3 0/2] " LorenzoPegorari
2026-06-01 13:52 ` [PATCH v3 1/2] http: cleanup function fetch_and_setup_pack_index() LorenzoPegorari
2026-06-01 13:52 ` [PATCH v3 2/2] http: fix memory leak in fetch_and_setup_pack_index() LorenzoPegorari
2026-06-02 6:24 ` [PATCH v3 0/2] " Jeff King
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=20260529054024.GA1104383@coredump.intra.peff.net \
--to=peff@peff$(echo .)net \
--cc=fox.gbr@townlong-yak$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
--cc=gitster@pobox$(echo .)com \
--cc=lorenzo.pegorari2002@gmail$(echo .)com \
--cc=me@ttaylorr$(echo .)com \
--cc=ps@pks$(echo .)im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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