git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 08/23] builtin/ls-remote: fix leaking `pattern` strings
Date: Wed, 31 Jul 2024 12:35:29 -0400	[thread overview]
Message-ID: <Zqpn0bLD+aLgY9PN@nand.local> (raw)
In-Reply-To: <d42152654bf91e90b8b417316f255746a3a75155.1721995576.git.ps@pks.im>

On Fri, Jul 26, 2024 at 02:15:24PM +0200, Patrick Steinhardt wrote:
> Users can pass patterns to git-ls-remote(1), which allows them to filter
> the list of printed references. We assemble those patterns into an array
> and prefix them with "*/", but never free either the array nor the
> allocated strings.
>
> Fix those leaks.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  builtin/ls-remote.c          | 11 +++++++----
>  t/t5535-fetch-push-symref.sh |  1 +
>  2 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
> index debf2d4f88..500f76fe4c 100644
> --- a/builtin/ls-remote.c
> +++ b/builtin/ls-remote.c
> @@ -47,7 +47,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  	int status = 0;
>  	int show_symref_target = 0;
>  	const char *uploadpack = NULL;
> -	const char **pattern = NULL;
> +	char **pattern = NULL;
>  	struct transport_ls_refs_options transport_options =
>  		TRANSPORT_LS_REFS_OPTIONS_INIT;
>  	int i;
> @@ -96,9 +96,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  	if (argc > 1) {
>  		int i;
>  		CALLOC_ARRAY(pattern, argc);
> -		for (i = 1; i < argc; i++) {
> +		for (i = 1; i < argc; i++)
>  			pattern[i - 1] = xstrfmt("*/%s", argv[i]);
> -		}

Instead of xstrfmt()-ing these strings directly, it may be more
ergonomic to use a strvec here and call strvec_pushf(). That would save
you explicitly CALLOC'ing the array, and would also save you the
explicit free() on each element of pattern below.

>  	}
>
>  	if (flags & REF_TAGS)
> @@ -136,7 +135,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  		struct ref_array_item *item;
>  		if (!check_ref_type(ref, flags))
>  			continue;
> -		if (!tail_match(pattern, ref->name))
> +		if (!tail_match((const char **) pattern, ref->name))

You could also drop the const cast here as well. The resulting patch on
top of this one simplifies things a bit:

--- 8< ---
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 500f76fe4c..b59ac98d81 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -47,7 +47,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	int status = 0;
 	int show_symref_target = 0;
 	const char *uploadpack = NULL;
-	char **pattern = NULL;
+	struct strvec pattern = STRVEC_INIT;
 	struct transport_ls_refs_options transport_options =
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
 	int i;
@@ -93,12 +93,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)

 	packet_trace_identity("ls-remote");

-	if (argc > 1) {
-		int i;
-		CALLOC_ARRAY(pattern, argc);
-		for (i = 1; i < argc; i++)
-			pattern[i - 1] = xstrfmt("*/%s", argv[i]);
-	}
+	for (int i = 1; i < argc; i++)
+		strvec_pushf(&pattern, "*/%s", argv[i]);

 	if (flags & REF_TAGS)
 		strvec_push(&transport_options.ref_prefixes, "refs/tags/");
@@ -135,7 +131,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		struct ref_array_item *item;
 		if (!check_ref_type(ref, flags))
 			continue;
-		if (!tail_match((const char **) pattern, ref->name))
+		if (!tail_match(pattern.v, ref->name))
 			continue;
 		item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
 		item->symref = xstrdup_or_null(ref->symref);
@@ -158,8 +154,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		status = 1;
 	transport_ls_refs_options_release(&transport_options);

-	for (i = 1; i < argc; i++)
-		free(pattern[i - 1]);
-	free(pattern);
+	strvec_clear(&pattern);
 	return status;
 }
--- >8 ---

Thanks,
Taylor

  reply	other threads:[~2024-07-31 16:35 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
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 [this message]
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=Zqpn0bLD+aLgY9PN@nand.local \
    --to=me@ttaylorr.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).