diff mbox series

[v1,3/7] mv: free the *with_slash in check_dir_in_index()

Message ID 20220719132809.409247-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 July 19, 2022, 1:28 p.m. UTC
*with_slash may be a malloc'd pointer, and when it is, free it.

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

Comments

Derrick Stolee July 19, 2022, 5:46 p.m. UTC | #1
On 7/19/2022 9:28 AM, Shaoxuan Yuan wrote:> *with_slash may be a malloc'd pointer, and when it is, free it.
It might be worth mentioning that you are reorganizing the
method to use gotos.

>  static int check_dir_in_index(const char *name)
>  {
> +	int ret = 1;

Interesting to "assume error", but that works since there is only one
place where the success is guaranteed.

Should we use -1 here?

>  		if (ce_skip_worktree(ce))
> -			return 0;
> +			ret = 0;

This could be

	ret = ce_skip_worktree(ce);

unless you really rely on the "1" value.

>  	}
> -	return 1;
> +
> +free_return:
> +	if (with_slash != name)
> +		free((char *)with_slash);

Good check to not clear 'name'. It's unfortunate that we need to cast here,
but it makes sense.

Thanks,
-Stolee
diff mbox series

Patch

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