git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Patrick Steinhardt <ps@pks.im>, git@vger.kernel.org
Subject: Re: [PATCH 07/23] builtin/submodule--helper: fix various trivial memory leaks
Date: Wed, 31 Jul 2024 23:52:26 +0200	[thread overview]
Message-ID: <82f4bef1-57d9-4427-8316-5e63b573931d@gmail.com> (raw)
In-Reply-To: <5220c91bda7bc766368c2925499e5d244a03697b.1721995576.git.ps@pks.im>

On Fri, Jul 26, 2024 at 02:15:09PM +0200, Patrick Steinhardt wrote:

> There are multiple trivial memory leaks in the submodule helper. Fix
> those.
> 
> Signed-off-by: Patrick Steinhardt <ps@pks.im>

Hi Patrick,

While working on another series, I fixed some leaks.  But, I haven't
sent any patches.

The series this message belongs to fixes all the leaks that my unsent
series addresses.  So, I won't bother sending my patches.

However, I think we can do better for the second leak solved in this
patch.  I leave my patch at the end of this message in case you want
to take a look.

In any case, thanks for working on this.  I have the habit of having
"SANITIZE=leak" in my config.mak, so this series will make me have
fewer distractions :)

> ---
>  builtin/submodule--helper.c | 13 ++++++++++---
>  t/t7400-submodule-basic.sh  |  1 +
>  2 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index f1218a1995..5ae06c3e0b 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -2269,6 +2269,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
>  	struct child_process cp = CHILD_PROCESS_INIT;
>  	struct strbuf rev = STRBUF_INIT;
>  	char *hex = oid_to_hex(oid);
> +	int reachable;
>  
>  	cp.git_cmd = 1;
>  	cp.dir = path;
> @@ -2278,9 +2279,12 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
>  	prepare_submodule_repo_env(&cp.env);
>  
>  	if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
> -		return 0;
> +		reachable = 0;
> +	else
> +		reachable = 1;
>  
> -	return 1;
> +	strbuf_release(&rev);
> +	return reachable;
>  }
>  
>  static int fetch_in_submodule(const char *module_path, int depth, int quiet,
> @@ -3135,6 +3139,7 @@ static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
>  static int add_submodule(const struct add_data *add_data)
>  {
>  	char *submod_gitdir_path;
> +	char *depth_to_free = NULL;
>  	struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
>  	struct string_list reference = STRING_LIST_INIT_NODUP;
>  	int ret = -1;
> @@ -3200,7 +3205,7 @@ static int add_submodule(const struct add_data *add_data)
>  		}
>  		clone_data.dissociate = add_data->dissociate;
>  		if (add_data->depth >= 0)
> -			clone_data.depth = xstrfmt("%d", add_data->depth);
> +			clone_data.depth = depth_to_free = xstrfmt("%d", add_data->depth);
>  
>  		if (clone_submodule(&clone_data, &reference))
>  			goto cleanup;
> @@ -3223,7 +3228,9 @@ static int add_submodule(const struct add_data *add_data)
>  			die(_("unable to checkout submodule '%s'"), add_data->sm_path);
>  	}
>  	ret = 0;
> +
>  cleanup:
> +	free(depth_to_free);
>  	string_list_clear(&reference, 1);
>  	return ret;
>  }
> diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
> index 981488885f..098d8833b6 100755
> --- a/t/t7400-submodule-basic.sh
> +++ b/t/t7400-submodule-basic.sh
> @@ -12,6 +12,7 @@ subcommands of git submodule.
>  GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
>  export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
>  
> +TEST_PASSES_SANITIZE_LEAK=true
>  . ./test-lib.sh
>  
>  test_expect_success 'setup - enable local submodules' '
> -- 
> 2.46.0.rc1.dirty
> 

----- >8 --------- >8 --------- >8 --------- >8 -----
Subject: [PATCH] submodule--helper: depth clone parameter

The `clone` subcommand has an OPT_STRING parameter: `--depth`, that
eventually is used when invoking `git-clone`.

As `git-clone` only support positive integer values for `--depth`
since 5594bcad21 (clone,fetch: catch non positive `--depth` option
value, 2013-12-05), seems safe to change the `--depth` parameter to
OPT_INTEGER in `module_clone`.

Doing so we avoid a leak in `add_submodule`.

Do it.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 builtin/submodule--helper.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f1218a1995..e7d52f38d9 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1530,7 +1530,7 @@ struct module_clone_data {
 	const char *path;
 	const char *name;
 	const char *url;
-	const char *depth;
+	int depth;
 	struct list_objects_filter_options *filter_options;
 	unsigned int quiet: 1;
 	unsigned int progress: 1;
@@ -1729,8 +1729,8 @@ static int clone_submodule(const struct module_clone_data *clone_data,
 			strvec_push(&cp.args, "--quiet");
 		if (clone_data->progress)
 			strvec_push(&cp.args, "--progress");
-		if (clone_data->depth && *(clone_data->depth))
-			strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
+		if (clone_data->depth > 0)
+			strvec_pushf(&cp.args, "--depth=%d", clone_data->depth);
 		if (reference->nr) {
 			struct string_list_item *item;
 
@@ -1851,8 +1851,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
 			   N_("reference repository")),
 		OPT_BOOL(0, "dissociate", &dissociate,
 			   N_("use --reference only while cloning")),
-		OPT_STRING(0, "depth", &clone_data.depth,
-			   N_("string"),
+		OPT_INTEGER(0, "depth", &clone_data.depth,
 			   N_("depth for shallow clones")),
 		OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
 		OPT_BOOL(0, "progress", &progress,
@@ -3199,8 +3198,7 @@ static int add_submodule(const struct add_data *add_data)
 			string_list_append(&reference, p)->util = p;
 		}
 		clone_data.dissociate = add_data->dissociate;
-		if (add_data->depth >= 0)
-			clone_data.depth = xstrfmt("%d", add_data->depth);
+		clone_data.depth = add_data->depth;
 
 		if (clone_submodule(&clone_data, &reference))
 			goto cleanup;
-- 
2.45.1

  reply	other threads:[~2024-07-31 21:52 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-26 12:13 [PATCH 00/23] Memory leak fixes (pt.3) Patrick Steinhardt
2024-07-26 12:13 ` [PATCH 01/23] builtin/replay: plug leaking `advance_name` variable Patrick Steinhardt
2024-07-31 16:22   ` Taylor Blau
2024-07-26 12:14 ` [PATCH 02/23] builtin/log: fix leaking branch name when creating cover letters Patrick Steinhardt
2024-07-30  9:14   ` Karthik Nayak
2024-07-31 16:23     ` Taylor Blau
2024-07-26 12:14 ` [PATCH 03/23] builtin/describe: fix memory leak with `--contains=` Patrick Steinhardt
2024-07-30  9:23   ` Karthik Nayak
2024-07-30 15:27   ` Junio C Hamano
2024-07-31 10:42     ` Patrick Steinhardt
2024-07-31 16:04       ` Junio C Hamano
2024-07-31 16:28   ` Taylor Blau
2024-07-26 12:14 ` [PATCH 04/23] builtin/describe: fix leaking array when running diff-index Patrick Steinhardt
2024-07-30  9:34   ` Karthik Nayak
2024-07-26 12:14 ` [PATCH 05/23] builtin/describe: fix trivial memory leak when describing blob Patrick Steinhardt
2024-07-26 12:14 ` [PATCH 06/23] builtin/name-rev: fix various trivial memory leaks Patrick Steinhardt
2024-07-30 15:36   ` Junio C Hamano
2024-07-26 12:15 ` [PATCH 07/23] builtin/submodule--helper: " Patrick Steinhardt
2024-07-31 21:52   ` Rubén Justo [this message]
2024-08-01  8:20     ` Patrick Steinhardt
2024-07-26 12:15 ` [PATCH 08/23] builtin/ls-remote: fix leaking `pattern` strings Patrick Steinhardt
2024-07-31 16:35   ` Taylor Blau
2024-08-01  8:19     ` Patrick Steinhardt
2024-07-26 12:15 ` [PATCH 09/23] builtin/remote: fix leaking strings in `branch_list` Patrick Steinhardt
2024-07-31 16:37   ` Taylor Blau
2024-07-26 12:15 ` [PATCH 10/23] builtin/remote: fix various trivial memory leaks Patrick Steinhardt
2024-07-26 12:16 ` [PATCH 11/23] builtin/stash: " Patrick Steinhardt
2024-07-31 16:40   ` Taylor Blau
2024-07-26 12:16 ` [PATCH 12/23] builtin/rev-parse: fix memory leak with `--parseopt` Patrick Steinhardt
2024-07-30 11:00   ` Karthik Nayak
2024-07-26 12:16 ` [PATCH 13/23] builtin/show-branch: fix several memory leaks Patrick Steinhardt
2024-07-26 12:17 ` [PATCH 14/23] builtin/credential-store: fix leaking credential Patrick Steinhardt
2024-07-26 12:17 ` [PATCH 15/23] builtin/rerere: fix various trivial memory leaks Patrick Steinhardt
2024-07-26 12:17 ` [PATCH 16/23] builtin/shortlog: " Patrick Steinhardt
2024-07-26 12:17 ` [PATCH 17/23] builtin/worktree: fix leaking derived branch names Patrick Steinhardt
2024-07-26 12:17 ` [PATCH 18/23] builtin/credential-cache: fix trivial leaks Patrick Steinhardt
2024-07-26 12:18 ` [PATCH 19/23] t/test-repository: fix leaking repository Patrick Steinhardt
2024-07-26 12:18 ` [PATCH 20/23] object-name: fix leaking commit list items Patrick Steinhardt
2024-07-26 12:18 ` [PATCH 21/23] entry: fix leaking pathnames during delayed checkout Patrick Steinhardt
2024-07-26 12:19 ` [PATCH 22/23] convert: fix leaking config strings Patrick Steinhardt
2024-07-26 12:19 ` [PATCH 23/23] commit-reach: fix trivial memory leak when computing reachability Patrick Steinhardt
2024-07-30 11:09 ` [PATCH 00/23] Memory leak fixes (pt.3) Karthik Nayak
2024-07-31 10:44   ` Patrick Steinhardt
2024-07-31 17:01 ` Taylor Blau
2024-08-01  8:19   ` Patrick Steinhardt
2024-08-01 17:16     ` Taylor Blau
2024-08-01 10:38 ` [PATCH v2 00/24] " Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 01/24] builtin/replay: plug leaking `advance_name` variable Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 02/24] builtin/log: fix leaking branch name when creating cover letters Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 03/24] builtin/describe: fix memory leak with `--contains=` Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 04/24] builtin/describe: fix leaking array when running diff-index Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 05/24] builtin/describe: fix trivial memory leak when describing blob Patrick Steinhardt
2024-08-01 10:38   ` [PATCH v2 06/24] builtin/name-rev: fix various trivial memory leaks Patrick Steinhardt
2024-08-01 10:39   ` [PATCH v2 08/24] builtin/submodule--helper: fix leaking buffer in `is_tip_reachable` Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 09/24] builtin/ls-remote: fix leaking `pattern` strings Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 10/24] builtin/remote: fix leaking strings in `branch_list` Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 11/24] builtin/remote: fix various trivial memory leaks Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 12/24] builtin/stash: " Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 13/24] builtin/rev-parse: fix memory leak with `--parseopt` Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 14/24] builtin/show-branch: fix several memory leaks Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 15/24] builtin/credential-store: fix leaking credential Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 16/24] builtin/rerere: fix various trivial memory leaks Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 17/24] builtin/shortlog: " Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 18/24] builtin/worktree: fix leaking derived branch names Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 19/24] builtin/credential-cache: fix trivial leaks Patrick Steinhardt
2024-08-01 10:40   ` [PATCH v2 20/24] t/test-repository: fix leaking repository Patrick Steinhardt
2024-08-01 10:41   ` [PATCH v2 21/24] object-name: fix leaking commit list items Patrick Steinhardt
2024-08-01 10:41   ` [PATCH v2 22/24] entry: fix leaking pathnames during delayed checkout Patrick Steinhardt
2024-08-01 10:41   ` [PATCH v2 23/24] convert: fix leaking config strings Patrick Steinhardt
2024-08-01 10:41   ` [PATCH v2 24/24] commit-reach: fix trivial memory leak when computing reachability Patrick Steinhardt
2024-08-01 10:42   ` [PATCH v2 07/24] builtin/submodule--helper: fix leaking clone depth parameter Patrick Steinhardt
2024-08-01 17:17   ` [PATCH v2 00/24] Memory leak fixes (pt.3) Taylor Blau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=82f4bef1-57d9-4427-8316-5e63b573931d@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).