git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Victoria Dye <vdye@github.com>
Date
Aug 8, 2022, 23:41 UTC
Message-ID
<69039db2-e7b2-f006-63da-e699d302c8ce@github.com>
In-Reply-To
<20220805030528.1535376-4-shaoxuan.yuan02@gmail.com>
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
Show 43 quoted lines
> 
> 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.
Previous: Shaoxuan YuanNext: Shaoxuan Yuan
Message 37 of 61 in “mv: from in-cone to out-of-cone”
  1. 0/7 mv: from in-cone to out-of-coneShaoxuan Yuan, Jul 19, 2022
  2. 1/7 t7002: add tests for moving from in-cone to out-of-coneShaoxuan Yuan, Jul 19, 2022
  3. Ævar Arnfjörð BjarmasonJul 19, 2022
  4. Derrick StoleeJul 19, 2022
  5. Junio C HamanoJul 19, 2022
  6. 2/7 mv: add documentation for check_dir_in_index()Shaoxuan Yuan, Jul 19, 2022
  7. Derrick StoleeJul 19, 2022
  8. Shaoxuan YuanJul 21, 2022
  9. Victoria DyeJul 19, 2022
  10. Victoria DyeJul 19, 2022
  11. Shaoxuan YuanJul 21, 2022
  12. 3/7 mv: free the *with_slash in check_dir_in_index()Shaoxuan Yuan, Jul 19, 2022
  13. Derrick StoleeJul 19, 2022
  14. 5/7 mv: remove BOTH from enum update_modeShaoxuan Yuan, Jul 19, 2022
  15. Derrick StoleeJul 19, 2022
  16. 4/7 mv: check if <destination> is a SKIP_WORKTREE_DIRShaoxuan Yuan, Jul 19, 2022
  17. Derrick StoleeJul 19, 2022
  18. Shaoxuan YuanJul 21, 2022
  19. Derrick StoleeJul 22, 2022
  20. Junio C HamanoJul 22, 2022
  21. 6/7 mv: from in-cone to out-of-coneShaoxuan Yuan, Jul 19, 2022
  22. Derrick StoleeJul 19, 2022
  23. Shaoxuan YuanAug 3, 2022
  24. Derrick StoleeAug 3, 2022
  25. Shaoxuan YuanAug 4, 2022
  26. 7/7 mv: check overwrite for in-to-out moveShaoxuan Yuan, Jul 19, 2022
  27. Derrick StoleeJul 19, 2022
  28. Derrick StoleeJul 19, 2022
  29. 0/9 mv: from in-cone to out-of-coneShaoxuan Yuan, Aug 5, 2022
  30. 1/9 t7002: add tests for moving from in-cone to out-of-coneShaoxuan Yuan, Aug 5, 2022
  31. Victoria DyeAug 9, 2022
  32. Shaoxuan YuanAug 9, 2022
  33. Shaoxuan YuanAug 9, 2022
  34. Shaoxuan YuanAug 9, 2022
  35. 2/9 mv: rename check_dir_in_index() to empty_dir_has_sparse_contents()Shaoxuan Yuan, Aug 5, 2022
  36. 3/9 mv: free the *with_slash in check_dir_in_index()Shaoxuan Yuan, Aug 5, 2022
  37. Victoria DyeAug 8, 2022
  38. Shaoxuan YuanAug 9, 2022
  39. 4/9 mv: check if <destination> is a SKIP_WORKTREE_DIRShaoxuan Yuan, Aug 5, 2022
  40. Victoria DyeAug 8, 2022
  41. Victoria DyeAug 9, 2022
  42. Shaoxuan YuanAug 9, 2022
  43. 5/9 mv: remove BOTH from enum update_modeShaoxuan Yuan, Aug 5, 2022
  44. 6/9 mv: from in-cone to out-of-coneShaoxuan Yuan, Aug 5, 2022
  45. Victoria DyeAug 9, 2022
  46. Shaoxuan YuanAug 9, 2022
  47. 7/9 mv: cleanup empty WORKING_DIRECTORYShaoxuan Yuan, Aug 5, 2022
  48. 8/9 advice.h: add advise_on_moving_dirty_path()Shaoxuan Yuan, Aug 5, 2022
  49. 9/9 mv: check overwrite for in-to-out moveShaoxuan Yuan, Aug 5, 2022
  50. Victoria DyeAug 8, 2022
  51. 0/9 mv: from in-cone to out-of-coneShaoxuan Yuan, Aug 9, 2022
  52. 1/9 t7002: add tests for moving from in-cone to out-of-coneShaoxuan Yuan, Aug 9, 2022
  53. 2/9 mv: rename check_dir_in_index() to empty_dir_has_sparse_contents()Shaoxuan Yuan, Aug 9, 2022
  54. 3/9 mv: free the with_slash in check_dir_in_index()Shaoxuan Yuan, Aug 9, 2022
  55. 5/9 mv: remove BOTH from enum update_modeShaoxuan Yuan, Aug 9, 2022
  56. 6/9 mv: from in-cone to out-of-coneShaoxuan Yuan, Aug 9, 2022
  57. 7/9 mv: cleanup empty WORKING_DIRECTORYShaoxuan Yuan, Aug 9, 2022
  58. 8/9 advice.h: add advise_on_moving_dirty_path()Shaoxuan Yuan, Aug 9, 2022
  59. 9/9 mv: check overwrite for in-to-out moveShaoxuan Yuan, Aug 9, 2022
  60. 4/9 mv: check if <destination> is a SKIP_WORKTREE_DIRShaoxuan Yuan, Aug 9, 2022
  61. Victoria DyeAug 16, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.