{"thread":{"id":"65035","subject":"[PATCH] pack-redundant: fix memory leak when open_pack_index() fails","startedAt":"2026-02-21T10:39:10Z","lastAt":"2026-02-24T10:14:47Z","messageCount":2,"participants":["Sahitya Chandra","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"536582","messageId":"20260221103900.41740-1-sahityajb@gmail.com","threadId":"65035","inReplyTo":null,"subject":"[PATCH] pack-redundant: fix memory leak when open_pack_index() fails","fromName":"Sahitya Chandra","fromEmail":"sahityajb@gmail.com","sentAt":"2026-02-21T10:38:59Z","receivedAt":"2026-02-21T10:39:10Z","isPatch":true,"sender":{"key":"sahityajb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/167204896?v=4"},"body":"In add_pack(), we allocate l.remaining_objects with llist_init() before\ncalling open_pack_index(). If open_pack_index() fails we return NULL\nwithout freeing the allocated list, leaking the memory.\n\nFix by calling llist_free(l.remaining_objects) on the error path before\nreturning.\n\nSigned-off-by: Sahitya Chandra <sahityajb@gmail.com>\n---\n builtin/pack-redundant.c | 4 +++-\n 1 file changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex e4ecf774ca..86749bb7e7 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -546,8 +546,10 @@ static struct pack_list * add_pack(struct packed_git *p)\n \tl.pack = p;\n \tllist_init(&l.remaining_objects);\n \n-\tif (open_pack_index(p))\n+\tif (open_pack_index(p)) {\n+\t\tllist_free(l.remaining_objects);\n \t\treturn NULL;\n+\t}\n \n \tbase = p->index_data;\n \tbase += 256 * 4 + ((p->index_version < 2) ? 4 : 8);\n-- \n2.43.0\n\n"},{"id":"536960","messageId":"aZ16EWgLFbTPwM-v@pks.im","threadId":"65035","inReplyTo":"20260221103900.41740-1-sahityajb@gmail.com","subject":"Re: [PATCH] pack-redundant: fix memory leak when open_pack_index() fails","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-02-24T10:14:41Z","receivedAt":"2026-02-24T10:14:47Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, Feb 21, 2026 at 04:08:59PM +0530, Sahitya Chandra wrote:\n> diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\n> index e4ecf774ca..86749bb7e7 100644\n> --- a/builtin/pack-redundant.c\n> +++ b/builtin/pack-redundant.c\n\nIt's arguably not really worth it to work on git-pack-redundant(1)\nas it's deprecated and dies unless you pass \"--i-still-use-this\". But\nthe fix is small enough, so it doesn't hurt much, either.\n\n> @@ -546,8 +546,10 @@ static struct pack_list * add_pack(struct packed_git *p)\n>  \tl.pack = p;\n>  \tllist_init(&l.remaining_objects);\n>  \n> -\tif (open_pack_index(p))\n> +\tif (open_pack_index(p)) {\n> +\t\tllist_free(l.remaining_objects);\n>  \t\treturn NULL;\n> +\t}\n\nRight. The confusing part here is that `llist_init()` doesn't only\ninitialize the data structure as its name might suggest, but it also\nends up allocating memory. It would be great do adjust this interface to\nclarify, but that is certainly out of scope for this patch series.\n\nBy the way, can't we avoid the memory allocation altogether by\nreordering the code so that we try to open the pack before we allocate\nmemory?\n\nThanks!\n\nPatrick\n"}]}