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

Re: [PATCH] revision: fix missing null for freed memory

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 8, 2025, 21:53 UTC
Message-ID
<xmqqldugdry6.fsf@gitster.g>
In-Reply-To
<20250208061702.88469-1-forivall@gmail.com>
Emily M Klassen <forivall@gmail.com> writes:
Show 12 quoted lines
> "git log --graph --no-graph" missed cleaning up the output_prefix and
> output_prefix_data pointers. This resulted in a segfault when using "--patch",
> "--name-status" or "--name-only", as the output_prefix_data continued to be in
> use after free()
>
> Signed-off-by: Emily M Klassen <forivall@gmail.com>
> ---
> I previously reported this a few hours ago, and ended up digging in and figuring
> it out. I'll make sure to bottom reply in the follow ups to this patch.
>
>  revision.c | 2 ++
>  1 file changed, 2 insertions(+)

Reading the symptom in the proposed log message (which is very clearly written, by the way), it seems that this is reproducible?

Can we have a test to make sure that the fix would not be broken later?

Show 13 quoted lines
> diff --git a/revision.c b/revision.c
> index 474fa1e767..84cb028e11 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -2615,6 +2615,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
>  		graph_clear(revs->graph);
>  		revs->graph = graph_init(revs);
>  	} else if (!strcmp(arg, "--no-graph")) {
> +		revs->diffopt.output_prefix = NULL;
> +		revs->diffopt.output_prefix_data = NULL;
>  		graph_clear(revs->graph);
>  		revs->graph = NULL;
>  	} else if (!strcmp(arg, "--encode-email-headers")) {
Interesting.

In response to "--graph" (the code we can see in the context before this part), we clear the revs->graph and then call graph_init(revs) for ourselves, and we do not have to futz with diffopt at all, and it works OK because output_prefix_data and output_prefix would be overwritten by the graph_init() to the value we want to use anyway.

But of course, after "--no-graph", nobody clears these two members for us, so we'd need to clear them here.

It might make the API less error-prone if the "clear" function cleared the .graph and diffopt->output_prefix{,_data} together but among three existing callers of graph_clear(), only this caller needs to clear these two members, so it probably would not matter.

So in short, this seems to be a good fix for the immediate issue, and it is unlikely that we'd need any follow-up work.

Previous: Emily M KlassenNext: Junio C Hamano
Message 2 of 12 in “revision: fix missing null for freed memory”
  1. revision: fix missing null for freed memoryEmily M Klassen, Feb 8, 2025
  2. Junio C HamanoFeb 8, 2025
  3. Junio C HamanoFeb 10, 2025
  4. Emily KlassenFeb 10, 2025
  5. Junio C HamanoFeb 13, 2025
  6. Patrick SteinhardtFeb 11, 2025
  7. D. Ben KnobleFeb 11, 2025
  8. D. Ben KnobleFeb 11, 2025
  9. Jeff KingFeb 11, 2025
  10. Junio C HamanoFeb 11, 2025
  11. Patrick SteinhardtFeb 12, 2025
  12. Ben KnobleFeb 13, 2025

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.