Subject: Re: [PATCH v4 12/27] object-file: mark cached object buffers as const

From: Junio C Hamano <[email-masked]>
Date: Wed, 05 Jun 2024 23:10:20 -0700

Junio C Hamano <[email-masked]> writes:

>> +	co_buf = xmalloc(len);
>> +	memcpy(co_buf, buf, len);
>>  
>>  	hash_object_file(the_hash_algo, buf, len, type, oid);
>>  	if (repo_has_object_file_with_flags(the_repository, oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
>
> There is an early return around here.  Perhaps we are leaking co_buf
> when we hit it?

Indeed, that seems to be the case.  With the attached at the tip of
the branch and rebuilding 'seen' seems to pass these 6130, 7010, 8002
tests with SANTIZE=leak.

>From f307bbf7bd317d90db29bd1589b49e84b9e37e88 Mon Sep 17 00:00:00 2001
From: Junio C Hamano <[email-masked]>
Date: Wed, 5 Jun 2024 23:03:34 -0700
Subject: [PATCH] fixup! object-file: mark cached object buffers as const

---
 object-file.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/object-file.c b/object-file.c
index b5b5a59dc6..2d5bd3a211 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1785,8 +1785,10 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
 
 	hash_object_file(the_hash_algo, buf, len, type, oid);
 	if (repo_has_object_file_with_flags(the_repository, oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
-	    find_cached_object(oid))
+	    find_cached_object(oid)) {
+		free(co_buf);
 		return 0;
+	}
 	ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
 	co = &cached_objects[cached_object_nr++];
 	co->size = len;
-- 
2.45.2-409-g7b0defb391