diff mbox series

[3/6] clone: fix a memory leak of the "git_dir" variable

Message ID patch-3.6-86d928ae2f9-20211021T155529Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series leaks: miscellaneous small leak fixes | expand

Commit Message

Ævar Arnfjörð Bjarmason Oct. 21, 2021, 3:57 p.m. UTC
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(-)

Comments

Junio C Hamano Oct. 22, 2021, 12:07 a.m. UTC | #1
Æ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.
diff mbox series

Patch

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