# [PATCH 2/2] cmd_clone: make sure repo is allocated on the stack and free it

6 messages from 2011-03-31 to 2011-03-31. Participants: Carlos Martín Nieto, John Szakmeister.
Thread: https://gitlist.dev/t/26945

## Carlos Martín Nieto, 2011-03-31 14:18

Subject: [PATCH 1/2] cmd_clone: free dir and path buffers
Message-ID: <1301581122-19947-1-git-send-email-cmn@elego.de>
URL: https://gitlist.dev/e/1301581122-19947-1-git-send-email-cmn%40elego.de

```
The variables dir and path are always allocated on the stack so it's
always safe to free them.

This memory would be freed by _exit, but it's good form to free memory
you've allocated.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

 This is a split-up version of an earlier patch. It's not a big deal,
but I had it in a local branch and I though I'd try to have it merged upstream.

 builtin/clone.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 2ee1fa9..fe0408c 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -665,6 +665,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
 	}
 
+	free(dir);
+	free(path);
 	strbuf_release(&reflog_msg);
 	strbuf_release(&branch_top);
 	strbuf_release(&key);
-- 
1.7.4.1

```

## Carlos Martín Nieto, 2011-03-31 14:18

Subject: [PATCH 2/2] cmd_clone: make sure repo is allocated on the stack and free it
Message-ID: <1301581122-19947-2-git-send-email-cmn@elego.de>
URL: https://gitlist.dev/e/1301581122-19947-2-git-send-email-cmn%40elego.de
In-Reply-To: <1301581122-19947-1-git-send-email-cmn@elego.de>

```
Make sure that the buffer repo points to is in the stack and free it
afterwards.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---
 builtin/clone.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index fe0408c..697b1bf 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -415,7 +415,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	else if (!strchr(repo_name, ':'))
 		die("repository '%s' does not exist", repo_name);
 	else
-		repo = repo_name;
+		repo = xstrdup(repo_name);
 	is_local = path && !is_bundle;
 	if (is_local && option_depth)
 		warning("--depth is ignored in local clones; use file:// instead.");
@@ -667,6 +667,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	free(dir);
 	free(path);
+	free(repo);
 	strbuf_release(&reflog_msg);
 	strbuf_release(&branch_top);
 	strbuf_release(&key);
-- 
1.7.4.1

```

## John Szakmeister, 2011-03-31 14:45

Subject: Re: [PATCH 1/2] cmd_clone: free dir and path buffers
Message-ID: <AANLkTi=XYv1WFbvHwqvrJDkssKC-zU91Vj4EPbZG6OK2@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTi%3DXYv1WFbvHwqvrJDkssKC-zU91Vj4EPbZG6OK2%40mail.gmail.com
In-Reply-To: <1301581122-19947-1-git-send-email-cmn@elego.de>

```
On Thu, Mar 31, 2011 at 10:18 AM, Carlos Martín Nieto <cmn@elego.de> wrote:
> The variables dir and path are always allocated on the stack so it's
> always safe to free them.

I think you mean "on the heap". :-)

-John

```

## Carlos Martín Nieto, 2011-03-31 15:06

Subject: Re: [PATCH 1/2] cmd_clone: free dir and path buffers
Message-ID: <1301583996.11592.3.camel@bee.lab.cmartin.tk>
URL: https://gitlist.dev/e/1301583996.11592.3.camel%40bee.lab.cmartin.tk
In-Reply-To: <AANLkTi=XYv1WFbvHwqvrJDkssKC-zU91Vj4EPbZG6OK2@mail.gmail.com>

```
On jue, 2011-03-31 at 10:45 -0400, John Szakmeister wrote:
> On Thu, Mar 31, 2011 at 10:18 AM, Carlos Martín Nieto <cmn@elego.de> wrote:
> > The variables dir and path are always allocated on the stack so it's
> > always safe to free them.
> 
> I think you mean "on the heap". :-)

 Indeed I do 

   cmn

```

## Carlos Martín Nieto, 2011-03-31 15:10

Subject: [PATCH 1/2] cmd_clone: free dir and path buffers
Message-ID: <1301584242-3193-1-git-send-email-cmn@elego.de>
URL: https://gitlist.dev/e/1301584242-3193-1-git-send-email-cmn%40elego.de
In-Reply-To: <1301583996.11592.3.camel@bee.lab.cmartin.tk>

```
The variables dir and path are always allocated on the heap so it's
always safe to free them.

This memory would be freed by _exit, but it's good form to free memory
you've allocated.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

Hopefully this time there are no silly mistakes.

 builtin/clone.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 2ee1fa9..fe0408c 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -665,6 +665,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
 	}
 
+	free(dir);
+	free(path);
 	strbuf_release(&reflog_msg);
 	strbuf_release(&branch_top);
 	strbuf_release(&key);
-- 
1.7.4.1

```

## Carlos Martín Nieto, 2011-03-31 15:10

Subject: [PATCH 2/2] cmd_clone: make sure repo is allocated on the heap and free it
Message-ID: <1301584242-3193-2-git-send-email-cmn@elego.de>
URL: https://gitlist.dev/e/1301584242-3193-2-git-send-email-cmn%40elego.de
In-Reply-To: <1301584242-3193-1-git-send-email-cmn@elego.de>

```
Make sure that the buffer repo points to is in the heap and free it
afterwards.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---
 builtin/clone.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index fe0408c..697b1bf 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -415,7 +415,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	else if (!strchr(repo_name, ':'))
 		die("repository '%s' does not exist", repo_name);
 	else
-		repo = repo_name;
+		repo = xstrdup(repo_name);
 	is_local = path && !is_bundle;
 	if (is_local && option_depth)
 		warning("--depth is ignored in local clones; use file:// instead.");
@@ -667,6 +667,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	free(dir);
 	free(path);
+	free(repo);
 	strbuf_release(&reflog_msg);
 	strbuf_release(&branch_top);
 	strbuf_release(&key);
-- 
1.7.4.1

```
