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

Re: [PATCH v3 1/4] log: fix memory leak if --graph is passed multiple times

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 11, 2022, 18:51 UTC
Message-ID
<xmqqsfspb5jz.fsf@gitster.g>
In-Reply-To
<20220211163627.598166-1-alexhenrie24@gmail.com>
Alex Henrie <alexhenrie24@gmail.com> writes:
Show 9 quoted lines
> +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.
Show 32 quoted lines
> +	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;
Previous: Alex Henrie
Message 13 of 13 in “log: fix memory leak if --graph is passed multiple times”
  1. 1/4 log: fix memory leak if --graph is passed multiple timesAlex Henrie, Feb 11, 2022
  2. 2/4 log: add a --no-graph optionAlex Henrie, Feb 11, 2022
  3. Junio C HamanoFeb 11, 2022
  4. Alex HenrieFeb 11, 2022
  5. 4/4 gitk: pass --no-graph to `git log`Alex Henrie, Feb 11, 2022
  6. Junio C HamanoFeb 11, 2022
  7. Alex HenrieFeb 11, 2022
  8. Junio C HamanoFeb 11, 2022
  9. Junio C HamanoFeb 11, 2022
  10. Alex HenrieFeb 11, 2022
  11. Alex HenrieFeb 11, 2022
  12. 3/4 log: add a log.graph config optionAlex Henrie, Feb 11, 2022
  13. Junio C HamanoFeb 11, 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.