diff mbox series

[v2,3/9] mv: free the *with_slash in check_dir_in_index()

Message ID 20220805030528.1535376-4-shaoxuan.yuan02@gmail.com (mailing list archive)
State Superseded
Headers show
Series mv: from in-cone to out-of-cone | expand

Commit Message

Shaoxuan Yuan Aug. 5, 2022, 3:05 a.m. UTC
*with_slash may be a malloc'd pointer, and when it is, free it.

Helped-by: Derrick Stolee <derrickstolee@github.com>
Helped-by: Victoria Dye <vdye@github.com>
Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
---
 builtin/mv.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Victoria Dye Aug. 8, 2022, 11:41 p.m. UTC | #1
Shaoxuan Yuan wrote:
> *with_slash may be a malloc'd pointer, and when it is, free it.

Super-nit: technically, `with_slash` (no `*`) is how you'd refer to the
pointer. `*` is the dereference operator [1], so `*with_slash` has type
`const char` and refers to the first character in the `with_slash` string.

[1] https://en.wikipedia.org/wiki/Dereference_operator

> 
> Helped-by: Derrick Stolee <derrickstolee@github.com>
> Helped-by: Victoria Dye <vdye@github.com>
> Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
> ---
>  builtin/mv.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/builtin/mv.c b/builtin/mv.c
> index 7c11b8f995..0a999640c9 100644
> --- a/builtin/mv.c
> +++ b/builtin/mv.c
> @@ -133,6 +133,7 @@ static int index_range_of_same_dir(const char *src, int length,
>   */
>  static int empty_dir_has_sparse_contents(const char *name)
>  {
> +	int ret = 0;
>  	const char *with_slash = add_slash(name);
>  	int length = strlen(with_slash);
>  
> @@ -142,14 +143,18 @@ static int empty_dir_has_sparse_contents(const char *name)
>  	if (pos < 0) {
>  		pos = -pos - 1;
>  		if (pos >= the_index.cache_nr)
> -			return 0;
> +			goto free_return;
>  		ce = active_cache[pos];
>  		if (strncmp(with_slash, ce->name, length))
> -			return 0;
> +			goto free_return;
>  		if (ce_skip_worktree(ce))
> -			return 1;
> +			ret = 1;
>  	}
> -	return 0;
> +
> +free_return:
> +	if (with_slash != name)
> +		free((char *)with_slash);
> +	return ret;
>  }
>  
>  int cmd_mv(int argc, const char **argv, const char *prefix)

The rest of this looks good, nice catch on the potential memory leak.
Shaoxuan Yuan Aug. 9, 2022, 2:33 a.m. UTC | #2
On 8/9/2022 7:41 AM, Victoria Dye wrote:
> Shaoxuan Yuan wrote:
>> *with_slash may be a malloc'd pointer, and when it is, free it.
> Super-nit: technically, `with_slash` (no `*`) is how you'd refer to the
> pointer. `*` is the dereference operator [1], so `*with_slash` has type
> `const char` and refers to the first character in the `with_slash` string.
>
> [1] https://en.wikipedia.org/wiki/Dereference_operator
Oh! Thanks, I was completely unaware. Will fix.

--
Thanks,
Shaoxuan
diff mbox series

Patch

diff --git a/builtin/mv.c b/builtin/mv.c
index 7c11b8f995..0a999640c9 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -133,6 +133,7 @@  static int index_range_of_same_dir(const char *src, int length,
  */
 static int empty_dir_has_sparse_contents(const char *name)
 {
+	int ret = 0;
 	const char *with_slash = add_slash(name);
 	int length = strlen(with_slash);
 
@@ -142,14 +143,18 @@  static int empty_dir_has_sparse_contents(const char *name)
 	if (pos < 0) {
 		pos = -pos - 1;
 		if (pos >= the_index.cache_nr)
-			return 0;
+			goto free_return;
 		ce = active_cache[pos];
 		if (strncmp(with_slash, ce->name, length))
-			return 0;
+			goto free_return;
 		if (ce_skip_worktree(ce))
-			return 1;
+			ret = 1;
 	}
-	return 0;
+
+free_return:
+	if (with_slash != name)
+		free((char *)with_slash);
+	return ret;
 }
 
 int cmd_mv(int argc, const char **argv, const char *prefix)