# [PATCH] Fix minor memory leak in init-db

5 messages from 2009-05-14 to 2009-05-17. Participants: Ammon Riley, Junio C Hamano.
Thread: https://gitlist.dev/t/19354

## Ammon Riley, 2009-05-14 22:22

Subject: [PATCH] Fix minor memory leak in init-db
Message-ID: <cd3664ac0905141522s3ad78411o9f015d2e7720c8b5@mail.gmail.com>
URL: https://gitlist.dev/e/cd3664ac0905141522s3ad78411o9f015d2e7720c8b5%40mail.gmail.com

```
There was an xmalloc() for path, but I didn't see a corresponding free().
Does it happen somewhere else that I'm not expecting?

Signed-off-by: Ammon Riley <ammon.riley@gmail.com>
---
 builtin-init-db.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/builtin-init-db.c b/builtin-init-db.c
index d1fa12a..6969987 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -308,6 +308,8 @@ int init_db(const char *template_dir, unsigned int flags)
 	strcpy(path+len, "/info");
 	safe_create_dir(path, 1);

+	free(path);
+
 	if (shared_repository) {
 		char buf[10];
 		/* We do not spell "group" and such, so that
-- 
1.6.3.1.9.g95405b.dirty

```

## Junio C Hamano, 2009-05-16 19:56

Subject: Re: [PATCH] Fix minor memory leak in init-db
Message-ID: <7vzldclt33.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzldclt33.fsf%40alter.siamese.dyndns.org
In-Reply-To: <cd3664ac0905141522s3ad78411o9f015d2e7720c8b5@mail.gmail.com>

```
Ammon Riley <ammon.riley@gmail.com> writes:

> There was an xmalloc() for path, but I didn't see a corresponding free().
> Does it happen somewhere else that I'm not expecting?

It implicitly happens in exit() in git.c:handle_internal_command()
after cmd_init_db() returns the control to it.

```

## Ammon Riley, 2009-05-17 04:07

Subject: Re: [PATCH] Fix minor memory leak in init-db
Message-ID: <cd3664ac0905162107t7ebca943p1722685a8484f08e@mail.gmail.com>
URL: https://gitlist.dev/e/cd3664ac0905162107t7ebca943p1722685a8484f08e%40mail.gmail.com
In-Reply-To: <7vzldclt33.fsf@alter.siamese.dyndns.org>

```
On Sat, May 16, 2009 at 12:56 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Ammon Riley <ammon.riley@gmail.com> writes:
>
>> There was an xmalloc() for path, but I didn't see a corresponding free().
>> Does it happen somewhere else that I'm not expecting?
>
> It implicitly happens in exit() in git.c:handle_internal_command()
> after cmd_init_db() returns the control to it.

Ah. Naturally. :)

So if I were to write a long-lived application (such as a custom UI) that
links to libgit, and bypasses those functions to call init_db() (and other
functions) directly, all those implicit free-on-exit() turn into memory
leaks.

Cheers,
Ammon

```

## Junio C Hamano, 2009-05-17 05:13

Subject: Re: [PATCH] Fix minor memory leak in init-db
Message-ID: <7veiuojoqy.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7veiuojoqy.fsf%40alter.siamese.dyndns.org
In-Reply-To: <cd3664ac0905162107t7ebca943p1722685a8484f08e@mail.gmail.com>

```
Ammon Riley <ammon.riley@gmail.com> writes:

> On Sat, May 16, 2009 at 12:56 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Ammon Riley <ammon.riley@gmail.com> writes:
>>
>>> There was an xmalloc() for path, but I didn't see a corresponding free().
>>> Does it happen somewhere else that I'm not expecting?
>>
>> It implicitly happens in exit() in git.c:handle_internal_command()
>> after cmd_init_db() returns the control to it.
>
> Ah. Naturally. :)
>
> So if I were to write a long-lived application (such as a custom UI) that
> links to libgit, and bypasses those functions to call init_db() (and other
> functions) directly, all those implicit free-on-exit() turn into memory
> leaks.

Correct, and there are other much larger issues to worry about.

That's why there is a separate libgit2 effort in progress.

```

## Ammon Riley, 2009-05-17 16:09

Subject: Re: [PATCH] Fix minor memory leak in init-db
Message-ID: <cd3664ac0905170909l3184c286s7f7d61e108fd5b05@mail.gmail.com>
URL: https://gitlist.dev/e/cd3664ac0905170909l3184c286s7f7d61e108fd5b05%40mail.gmail.com
In-Reply-To: <7veiuojoqy.fsf@alter.siamese.dyndns.org>

```
On Sat, May 16, 2009 at 10:13 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Ammon Riley <ammon.riley@gmail.com> writes:
>
>> On Sat, May 16, 2009 at 12:56 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Ammon Riley <ammon.riley@gmail.com> writes:
>>>
>>>> There was an xmalloc() for path, but I didn't see a corresponding free().
>>>> Does it happen somewhere else that I'm not expecting?
>>>
>>> It implicitly happens in exit() in git.c:handle_internal_command()
>>> after cmd_init_db() returns the control to it.
>>
>> Ah. Naturally. :)
>>
>> So if I were to write a long-lived application (such as a custom UI) that
>> links to libgit, and bypasses those functions to call init_db() (and other
>> functions) directly, all those implicit free-on-exit() turn into memory
>> leaks.
>
> Correct, and there are other much larger issues to worry about.
>
> That's why there is a separate libgit2 effort in progress.

Okay, cool! I wasn't aware of that -- I'll take a look at it. In the meantime,
are small patches for this type of issue welcome if I run across others,
or would you prefer I let them lie?

Cheers,
Ammon

```
