]> git.ipfire.org Git - thirdparty/git.git/commitdiff
check_connected(): fix leak of pack-index mmap
authorJeff King <peff@peff.net>
Thu, 5 Mar 2026 23:09:56 +0000 (18:09 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Mar 2026 21:51:03 +0000 (13:51 -0800)
Since c6807a40dc (clone: open a shortcut for connectivity check,
2013-05-26), we may open a one-off packed_git struct to check what's in
the pack we just received. At the end of the function we throw away the
struct (rather than linking it into the repository struct as usual).

We used to leak the struct until dd4143e7bf (connected.c: free the
"struct packed_git", 2022-11-08), which calls free(). But that's not
sufficient; inside the struct we'll have mmap'd the pack idx data from
disk, which needs an munmap() call.

Building with SANITIZE=leak doesn't detect this, because we are leaking
our own mmap(), and it only finds heap allocations from malloc(). But if
we use our compat mmap implementation like this:

  make NO_MMAP=MapsBecomeMallocs SANITIZE=leak

then LSan will notice the leak, because now it's a regular heap buffer
allocated by malloc().

We can fix it by calling close_pack(), which will free any associated
memory. Note that we need to check for NULL ourselves; unlike free(), it
is not safe to pass a NULL pointer to close_pack().

Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
connected.c

index 530357de54f890f769c1f31f43ecd532453c1e18..6718503649da8ab7d8ca517956f8f6261350f017 100644 (file)
@@ -159,6 +159,9 @@ no_promisor_pack_found:
                err = error_errno(_("failed to close rev-list's stdin"));
 
        sigchain_pop(SIGPIPE);
-       free(new_pack);
+       if (new_pack) {
+               close_pack(new_pack);
+               free(new_pack);
+       }
        return finish_command(&rev_list) || err;
 }