From: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
To: git@vger.kernel.org
Cc: Taylor Blau <me@ttaylorr.com>, Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>, fox <fox.gbr@townlong-yak.com>,
Jeff King <peff@peff.net>
Subject: [PATCH v2] http: fix memory leak in fetch_and_setup_pack_index()
Date: Fri, 29 May 2026 01:49:44 +0200 [thread overview]
Message-ID: <ahjUmMCKxREamQE-@lorenzo-VM> (raw)
In-Reply-To: <agx5tblaCZNsYEBq@lorenzo-VM>
Inside the function `fetch_and_setup_pack_index()`, when the pack
obtained using `parse_pack_index()` fails to be verified by
`verify_pack_index()`, the function returns without closing and freeing
said pack.
Fix this by calling `close_pack_index()` to munmap the index file for
the leaking pack (which might have been mmapped by `fetch_pack_index()`
or `verify_pack_index()`), and then free it, when the verification
fails.
Also, do some more cleanup by removing the useless call to the function
`unlink()`. This is not necessary anymore since 63aca3f7f1 (dumb-http:
store downloaded pack idx as tempfile, 2024-10-25), when
`fetch_pack_index()` started registering its return value (in this case
`tmp_idx`) as a tempfile to be deleted at process exit.
Signed-off-by: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
---
http.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/http.c b/http.c
index 67c9c6fc60..99da4d7529 100644
--- a/http.c
+++ b/http.c
@@ -2538,18 +2538,18 @@ static int fetch_and_setup_pack_index(struct packfile_list *packs,
new_pack = parse_pack_index(the_repository, sha1, tmp_idx);
if (!new_pack) {
- unlink(tmp_idx);
free(tmp_idx);
-
return -1; /* parse_pack_index() already issued error message */
}
ret = verify_pack_index(new_pack);
- if (!ret)
- close_pack_index(new_pack);
+
+ close_pack_index(new_pack);
free(tmp_idx);
- if (ret)
+ if (ret) {
+ free(new_pack);
return -1;
+ }
packfile_list_prepend(packs, new_pack);
return 0;
--
2.54.0.129.g2dffd77b94.dirty
next prev parent reply other threads:[~2026-05-28 23:49 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 ` LorenzoPegorari [this message]
2026-05-29 5:36 ` [PATCH v2] " Jeff King
2026-05-29 5:40 ` Jeff King
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=ahjUmMCKxREamQE-@lorenzo-VM \
--to=lorenzo.pegorari2002@gmail.com \
--cc=fox.gbr@townlong-yak.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=me@ttaylorr.com \
--cc=peff@peff.net \
--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