object-file: fix memory leak when reading corrupted headers

When reading corrupt object headers in `read_loose_object()`, we bail out immediately. This causes a memory leak though because we would have already initialized the zstream in `unpack_loose_header()`, and it is the callers responsibility to finish the zstream even on error. While this feels weird, other callsites do it correctly already. Fix this leak by ending the zstream even on errors. We may want to revisit this interface in the future such that the callee handles this for us already when there was an error. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 14, 2024 at 08:51 UTC aa9ef614dc281d123e8f18d19c9abecf82295c0e
2 files changed +2
object-file.c
+1
@@ -2954,6 +2954,7 @@ int read_loose_object(const char *path,
2954 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
2955 NULL) != ULHR_OK) {
2956 error(_("unable to unpack header of %s"), path);
2957 + git_inflate_end(&stream);
2958 goto out;
2959 }
2960
t/t1450-fsck.sh
+1
@@ -6,6 +6,7 @@ test_description='git fsck random collection of tests
6 * (main) A
7 '
8
9 +TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
11
12 test_expect_success setup '