All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
	Taylor Blau <me@ttaylorr.com>
Subject: Re: [PATCH 3/6] clone: fix a memory leak of the "git_dir" variable
Date: Thu, 21 Oct 2021 17:07:48 -0700	[thread overview]
Message-ID: <xmqqilxpgbvv.fsf@gitster.g> (raw)
In-Reply-To: <patch-3.6-86d928ae2f9-20211021T155529Z-avarab@gmail.com> ("Ævar	Arnfjörð Bjarmason"'s message of "Thu, 21 Oct 2021 17:57:34 +0200")

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> At this point in cmd_clone the "git_dir" is always either an
> xstrdup()'d string, or something we got from mkpathdup(). Let's free()
> it before we clobber it.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/clone.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/builtin/clone.c b/builtin/clone.c
> index 559acf9e036..fb377b27657 100644
> --- a/builtin/clone.c
> +++ b/builtin/clone.c
> @@ -1040,8 +1040,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>  	init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
>  		INIT_DB_QUIET);
>  
> -	if (real_git_dir)
> +	if (real_git_dir) {
> +		free((char *)git_dir);
>  		git_dir = real_git_dir;
> +	}
>  
>  	/*
>  	 * additional config can be injected with -c, make sure it's included

I had to wonder if the old git_dir can still be pointed at by
junk_git_dir.  Much earlier than this point there is this:

	if (real_git_dir) {
		if (real_dest_exists)
			junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
		junk_git_dir = real_git_dir;
	} else {
		if (dest_exists)
			junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
		junk_git_dir = git_dir;
	}
	if (safe_create_leading_directories_const(git_dir) < 0)
		die(_("could not create leading directories of '%s'"), git_dir);

Luckily, junk_git_dir gets git_dir only when !real_git_dir, so it is
safe.  real_git_dir can only be set via the --separate-git-dir
command line option, so we are safe here.

Thanks.

  reply	other threads:[~2021-10-22  0:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-21 15:57 [PATCH 0/6] leaks: miscellaneous small leak fixes Ævar Arnfjörð Bjarmason
2021-10-21 15:57 ` [PATCH 1/6] grep: prefer "struct grep_opt" over its "void *" Ævar Arnfjörð Bjarmason
2021-10-21 23:52   ` Junio C Hamano
2021-10-21 15:57 ` [PATCH 2/6] grep: use object_array_clear() in cmd_grep() Ævar Arnfjörð Bjarmason
2021-10-21 23:56   ` Junio C Hamano
2021-10-21 23:58   ` Junio C Hamano
2021-10-21 15:57 ` [PATCH 3/6] clone: fix a memory leak of the "git_dir" variable Ævar Arnfjörð Bjarmason
2021-10-22  0:07   ` Junio C Hamano [this message]
2021-10-21 15:57 ` [PATCH 4/6] submodule--helper: fix small memory leaks Ævar Arnfjörð Bjarmason
2021-10-21 15:57 ` [PATCH 5/6] reflog: free() ref given to us by dwim_log() Ævar Arnfjörð Bjarmason
2021-10-22  0:16   ` Junio C Hamano
2021-10-21 15:57 ` [PATCH 6/6] repack: stop leaking a "struct child_process" Ævar Arnfjörð Bjarmason
2021-10-22  0:22   ` Junio C Hamano
2021-10-22  8:55 ` [PATCH v2 0/6] leaks: miscellaneous small leak fixes Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 1/6] grep: prefer "struct grep_opt" over its "void *" equivalent Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 2/6] grep: use object_array_clear() in cmd_grep() Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 3/6] grep: fix a "path_list" memory leak Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 4/6] clone: fix a memory leak of the "git_dir" variable Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 5/6] submodule--helper: fix small memory leaks Ævar Arnfjörð Bjarmason
2021-10-22  8:55   ` [PATCH v2 6/6] reflog: free() ref given to us by dwim_log() Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqilxpgbvv.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=me@ttaylorr.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.