git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Alex Henrie <alexhenrie24@gmail.com>
Cc: git@vger.kernel.org, paulus@ozlabs.org
Subject: Re: [PATCH v3 1/4] log: fix memory leak if --graph is passed multiple times
Date: Fri, 11 Feb 2022 10:51:12 -0800	[thread overview]
Message-ID: <xmqqsfspb5jz.fsf@gitster.g> (raw)
In-Reply-To: <20220211163627.598166-1-alexhenrie24@gmail.com> (Alex Henrie's message of "Fri, 11 Feb 2022 09:36:24 -0700")

Alex Henrie <alexhenrie24@gmail.com> writes:

> +void graph_clear(struct git_graph *graph)
> +{
> +	if (!graph)
> +		return;
> +
> +	free(graph->columns);
> +	free(graph->new_columns);
> +	free(graph->mapping);
> +	free(graph->old_mapping);

These four are pointer members that graph_init() allocates storage
for, so releasing resources held by these four members in clear()
makes it symmetrical.

The .columns and .new_columns members are arrays of "struct column",
but each element in the array does not own any external memory, so
freeing the arrays is sufficient.  The .mapping and .old_mapping
members are arrays of int and freeing them is also sufficient.

Looking good.

> +	free(graph);
> +}
> +
>  static void graph_update_state(struct git_graph *graph, enum graph_state s)
>  {
>  	graph->prev_state = graph->state;
> diff --git a/graph.h b/graph.h
> index 8313e293c7..e88632a014 100644
> --- a/graph.h
> +++ b/graph.h
> @@ -139,6 +139,11 @@ void graph_set_column_colors(const char **colors, unsigned short colors_max);
>   */
>  struct git_graph *graph_init(struct rev_info *opt);
>  
> +/*
> + * Free a struct git_graph.
> + */
> +void graph_clear(struct git_graph *graph);
> +
>  /*
>   * Update a git_graph with a new commit.
>   * This will cause the graph to begin outputting lines for the new commit
> diff --git a/revision.c b/revision.c
> index ad4286fbdd..816061f3d9 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -2426,6 +2426,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
>  	} else if (!strcmp(arg, "--graph")) {
>  		revs->topo_order = 1;
>  		revs->rewrite_parents = 1;
> +		graph_clear(revs->graph);
>  		revs->graph = graph_init(revs);

Makes sense.  Will queue.

>  	} else if (!strcmp(arg, "--encode-email-headers")) {
>  		revs->encode_email_headers = 1;

      parent reply	other threads:[~2022-02-11 18:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11 16:36 [PATCH v3 1/4] log: fix memory leak if --graph is passed multiple times Alex Henrie
2022-02-11 16:36 ` [PATCH v3 2/4] log: add a --no-graph option Alex Henrie
2022-02-11 19:02   ` Junio C Hamano
2022-02-11 19:20     ` Alex Henrie
2022-02-11 16:36 ` [PATCH v3 3/4] log: add a log.graph config option Alex Henrie
2022-02-11 16:36 ` [PATCH v3 4/4] gitk: pass --no-graph to `git log` Alex Henrie
2022-02-11 18:12   ` Junio C Hamano
2022-02-11 19:05     ` Alex Henrie
2022-02-11 19:20       ` Junio C Hamano
2022-02-11 20:00         ` Junio C Hamano
2022-02-11 20:11           ` Alex Henrie
2022-02-11 20:08         ` Alex Henrie
2022-02-11 18:51 ` Junio C Hamano [this message]

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=xmqqsfspb5jz.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=alexhenrie24@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=paulus@ozlabs.org \
    /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.
Code repositories for project(s) associated with this public inbox

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

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 read-only IMAP folder(s) and NNTP newsgroup(s).