# [PATCH] object-file: fix leak on conversion failure

3 messages from 2024-06-22 to 2024-06-24. Participants: Eric Wong, Derrick Stolee, Junio C Hamano.
Thread: https://gitlist.dev/t/61670

## Eric Wong, 2024-06-22 04:36

Subject: [PATCH] object-file: fix leak on conversion failure
Message-ID: <20240622043648.M78681@dcvr>
URL: https://gitlist.dev/e/20240622043648.M78681%40dcvr

```
I'm not sure exactly how to trigger the leak, but it seems fairly
obvious that the `content' buffer should be freed even if
convert_object_file() fails.  Noticed while working in this area
on unrelated things.

Signed-off-by: Eric Wong <e@80x24.org>
---
 object-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/object-file.c b/object-file.c
index d3cf4b8b2e..00c8f1039b 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1711,9 +1711,9 @@ static int oid_object_info_convert(struct repository *r,
 			ret = convert_object_file(&outbuf,
 						  the_hash_algo, input_algo,
 						  content, size, type, !do_die);
+			free(content);
 			if (ret == -1)
 				return -1;
-			free(content);
 			size = outbuf.len;
 			content = strbuf_detach(&outbuf, NULL);
 		}

```

## Derrick Stolee, 2024-06-23 16:34

Subject: Re: [PATCH] object-file: fix leak on conversion failure
Message-ID: <1f536f91-1dce-4156-98f2-4059cd5235ff@gmail.com>
URL: https://gitlist.dev/e/1f536f91-1dce-4156-98f2-4059cd5235ff%40gmail.com
In-Reply-To: <20240622043648.M78681@dcvr>

```
On 6/22/24 12:36 AM, Eric Wong wrote:
> I'm not sure exactly how to trigger the leak, but it seems fairly
> obvious that the `content' buffer should be freed even if
> convert_object_file() fails.  Noticed while working in this area
> on unrelated things.

Definitely a good thing to include, even if it is unlikely to
be hit frequently in common scenarios.

>   			ret = convert_object_file(&outbuf,
>   						  the_hash_algo, input_algo,
>   						  content, size, type, !do_die);
> +			free(content);
>   			if (ret == -1)
>   				return -1;
> -			free(content);

I looked at the context of this function to see that 'content'
was local to the method, so was not "owned" by something outside
of the method that might expect to reuse the buffer on failure.

Thanks,
-Stolee

```

## Junio C Hamano, 2024-06-24 16:08

Subject: Re: [PATCH] object-file: fix leak on conversion failure
Message-ID: <xmqq8qyue330.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq8qyue330.fsf%40gitster.g
In-Reply-To: <1f536f91-1dce-4156-98f2-4059cd5235ff@gmail.com>

```
Derrick Stolee <stolee@gmail.com> writes:

> On 6/22/24 12:36 AM, Eric Wong wrote:
>> I'm not sure exactly how to trigger the leak, but it seems fairly
>> obvious that the `content' buffer should be freed even if
>> convert_object_file() fails.  Noticed while working in this area
>> on unrelated things.
>
> Definitely a good thing to include, even if it is unlikely to
> be hit frequently in common scenarios.
>
>>   			ret = convert_object_file(&outbuf,
>>   						  the_hash_algo, input_algo,
>>   						  content, size, type, !do_die);
>> +			free(content);
>>   			if (ret == -1)
>>   				return -1;
>> -			free(content);
>
> I looked at the context of this function to see that 'content'
> was local to the method, so was not "owned" by something outside
> of the method that might expect to reuse the buffer on failure.

Thanks, both.

Will queue.


```
