path: root/builtin/fsck.c
diff options
authorÆvar Arnfjörð Bjarmason <>2021-11-11 05:18:56 (GMT)
committerJunio C Hamano <>2021-11-11 21:40:43 (GMT)
commit16235e3b1460ddd708995b4cc5028e49d47e3761 (patch)
tree896bf4f4db27b7dd754f2389eaf7d3d1159d902f /builtin/fsck.c
parent168a937bbcf74370267fe04f5368035948745baa (diff)
object-file: free(*contents) only in read_loose_object() caller
In the preceding commit a free() of uninitialized memory regression in 96e41f58fe1 (fsck: report invalid object type-path combinations, 2021-10-01) was fixed, but we'd still have an issue with leaking memory from fsck_loose(). Let's fix that issue too. That issue was introduced in my 31deb28f5e0 (fsck: don't hard die on invalid object types, 2021-10-01). It can be reproduced under SANITIZE=leak with the test I added in 093fffdfbec (fsck tests: add test for fsck-ing an unknown type, 2021-10-01): ./ --run=84 -vixd In some sense it's not a problem, we lost the same amount of memory in terms of things malloc'd and not free'd. It just moved from the "still reachable" to "definitely lost" column in valgrind(1) nomenclature[1], since we'd have die()'d before. But now that we don't hard die() anymore in the library let's properly free() it. Doing so makes this code much easier to follow, since we'll now have one function owning the freeing of the "contents" variable, not two. For context on that memory management pattern the read_loose_object() function was added in f6371f92104 (sha1_file: add read_loose_object() function, 2017-01-13) and subsequently used in c68b489e564 (fsck: parse loose object paths directly, 2017-01-13). The pattern of it being the task of both sides to free() the memory has been there in this form since its inception. 1. Signed-off-by: Ævar Arnfjörð Bjarmason <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/fsck.c')
1 files changed, 2 insertions, 1 deletions
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 1a02391..97c7322 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -604,7 +604,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data)
struct object *obj;
enum object_type type = OBJ_NONE;
unsigned long size;
- void *contents;
+ void *contents = NULL;
int eaten;
struct object_info oi = OBJECT_INFO_INIT;
struct object_id real_oid = *null_oid();
@@ -629,6 +629,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data)
if (err < 0) {
errors_found |= ERROR_OBJECT;
+ free(contents);
return 0; /* keep checking other objects */