git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] add -p: fix memory leak
@ 2020-09-07 15:04 Phillip Wood via GitGitGadget
  2020-09-08 12:09 ` Johannes Schindelin
  0 siblings, 1 reply; 2+ messages in thread
From: Phillip Wood via GitGitGadget @ 2020-09-07 15:04 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin, Phillip Wood, Phillip Wood

From: Phillip Wood <phillip.wood@dunelm.org.uk>

asan reports that the C version of `add -p` is not freeing all the
memory it allocates. Fix this by introducing a function to clear
`struct add_p_state` and use it instead of freeing individual members.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
    add -p: fix memory leak
    
    It seems to be the season for add -p fixes. This patch fixes a memory
    leak in the C version found with asan.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-729%2Fphillipwood%2Fwip%2Fadd-p-fix-memory-leak-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-729/phillipwood/wip/add-p-fix-memory-leak-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/729

 add-patch.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 457b8c550e..2fcab983a6 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -266,6 +266,20 @@ struct add_p_state {
 	const char *revision;
 };
 
+static void add_p_state_clear(struct add_p_state *s)
+{
+	size_t i;
+
+	strbuf_release(&s->answer);
+	strbuf_release(&s->buf);
+	strbuf_release(&s->plain);
+	strbuf_release(&s->colored);
+	for (i = 0; i < s->file_diff_nr; i++)
+		free(s->file_diff[i].hunk);
+	free(s->file_diff);
+	clear_add_i_state(&s->s);
+}
+
 static void err(struct add_p_state *s, const char *fmt, ...)
 {
 	va_list args;
@@ -1690,9 +1704,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
 	     repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
 					  NULL, NULL, NULL) < 0) ||
 	    parse_diff(&s, ps) < 0) {
-		strbuf_release(&s.plain);
-		strbuf_release(&s.colored);
-		clear_add_i_state(&s.s);
+		add_p_state_clear(&s);
 		return -1;
 	}
 
@@ -1707,10 +1719,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
 	else if (binary_count == s.file_diff_nr)
 		fprintf(stderr, _("Only binary files changed.\n"));
 
-	strbuf_release(&s.answer);
-	strbuf_release(&s.buf);
-	strbuf_release(&s.plain);
-	strbuf_release(&s.colored);
-	clear_add_i_state(&s.s);
+	add_p_state_clear(&s);
 	return 0;
 }

base-commit: 3a238e539bcdfe3f9eb5010fd218640c1b499f7a
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] add -p: fix memory leak
  2020-09-07 15:04 [PATCH] add -p: fix memory leak Phillip Wood via GitGitGadget
@ 2020-09-08 12:09 ` Johannes Schindelin
  0 siblings, 0 replies; 2+ messages in thread
From: Johannes Schindelin @ 2020-09-08 12:09 UTC (permalink / raw)
  To: Phillip Wood via GitGitGadget; +Cc: git, Phillip Wood, Phillip Wood

Hi Phillip,

On Mon, 7 Sep 2020, Phillip Wood via GitGitGadget wrote:

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> asan reports that the C version of `add -p` is not freeing all the
> memory it allocates. Fix this by introducing a function to clear
> `struct add_p_state` and use it instead of freeing individual members.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>

ACK! And thanks for fixing bugs I introduced,
Dscho

> ---
>     add -p: fix memory leak
>
>     It seems to be the season for add -p fixes. This patch fixes a memory
>     leak in the C version found with asan.
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-729%2Fphillipwood%2Fwip%2Fadd-p-fix-memory-leak-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-729/phillipwood/wip/add-p-fix-memory-leak-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/729
>
>  add-patch.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
>
> diff --git a/add-patch.c b/add-patch.c
> index 457b8c550e..2fcab983a6 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -266,6 +266,20 @@ struct add_p_state {
>  	const char *revision;
>  };
>
> +static void add_p_state_clear(struct add_p_state *s)
> +{
> +	size_t i;
> +
> +	strbuf_release(&s->answer);
> +	strbuf_release(&s->buf);
> +	strbuf_release(&s->plain);
> +	strbuf_release(&s->colored);
> +	for (i = 0; i < s->file_diff_nr; i++)
> +		free(s->file_diff[i].hunk);
> +	free(s->file_diff);
> +	clear_add_i_state(&s->s);
> +}
> +
>  static void err(struct add_p_state *s, const char *fmt, ...)
>  {
>  	va_list args;
> @@ -1690,9 +1704,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
>  	     repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
>  					  NULL, NULL, NULL) < 0) ||
>  	    parse_diff(&s, ps) < 0) {
> -		strbuf_release(&s.plain);
> -		strbuf_release(&s.colored);
> -		clear_add_i_state(&s.s);
> +		add_p_state_clear(&s);
>  		return -1;
>  	}
>
> @@ -1707,10 +1719,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
>  	else if (binary_count == s.file_diff_nr)
>  		fprintf(stderr, _("Only binary files changed.\n"));
>
> -	strbuf_release(&s.answer);
> -	strbuf_release(&s.buf);
> -	strbuf_release(&s.plain);
> -	strbuf_release(&s.colored);
> -	clear_add_i_state(&s.s);
> +	add_p_state_clear(&s);
>  	return 0;
>  }
>
> base-commit: 3a238e539bcdfe3f9eb5010fd218640c1b499f7a
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-08 16:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-07 15:04 [PATCH] add -p: fix memory leak Phillip Wood via GitGitGadget
2020-09-08 12:09 ` Johannes Schindelin

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git