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

Re: [PATCH 07/13] dir: free allocations on parse-error paths in read_one_dir()

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jul 4, 2026, 08:58 UTC
Message-ID
<0278c01a-5a7d-e6a4-bee3-4df6df7e5276@gmx.de>
In-Reply-To
<akTIOeXFhNjJ7V3i@pks.im>
Hi Patrick,
On Wed, 1 Jul 2026, Patrick Steinhardt wrote:
Show 30 quoted lines
> On Wed, Jul 01, 2026 at 07:04:25AM +0000, Johannes Schindelin via GitGitGadget wrote:
> > diff --git a/dir.c b/dir.c
> > index 32430090dc..23335b9f7a 100644
> > --- a/dir.c
> > +++ b/dir.c
> > @@ -3792,13 +3792,18 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
> >  		ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
> >  
> >  	ud.dirs_alloc = ud.dirs_nr = decode_varint(&data);
> > -	if (data > end)
> > +	if (data > end) {
> > +		free(ud.untracked);
> >  		return -1;
> > +	}
> >  	ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
> >  
> >  	eos = memchr(data, '\0', end - data);
> > -	if (!eos || eos == end)
> > +	if (!eos || eos == end) {
> > +		free(ud.untracked);
> > +		free(ud.dirs);
> >  		return -1;
> > +	}
> >  
> >  	*untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1));
> >  	memcpy(untracked, &ud, sizeof(ud));
> 
> Hm. Here we assign ownership to the caller, but this still feels quite
> off to me as we also have two more early returns after this point that
> seem to leak memory. Do the callers make sure to always free the data?

Ownership transfers to the caller on the `xmalloc`/`memcpy` line: the `memcpy` copies the `ud.untracked` and `ud.dirs` pointers into the freshly xmalloc'd struct that becomes `*untracked_`. From there, any subsequent failure in the caller reaches `free_untracked_cache()` and then `free_untracked()`, which releases both arrays. So the two further early returns are correct as-are.

I will fold that reasoning into the v2 commit message so a future reader does not have to re-derive it.

Incidentally, and orthogonal to Coverity's leak report: on those same failure paths, individual slots of `->dirs` and `->untracked` remain uninitialised, so `free_untracked()` walks garbage pointers before it ever reaches the two `free()` calls above. That is a separate crash-on-cleanup bug and I would prefer to address it in a follow-up rather than widen the scope of this series.

Ciao, Johannes

Previous: Patrick SteinhardtNext: Johannes Schindelin via GitGitGadget
Message 18 of 44 in “coverity: fix leaks and error paths”
  1. 00/13 coverity: fix leaks and error pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  2. 01/13 load_one_loose_object_map(): fix resource leakJohannes Schindelin via GitGitGadget, Jul 1, 2026
  3. Patrick SteinhardtJul 1, 2026
  4. Junio C HamanoJul 1, 2026
  5. Johannes SchindelinJul 4, 2026
  6. 02/13 loose: avoid closing invalid fd on error pathJohannes Schindelin via GitGitGadget, Jul 1, 2026
  7. Patrick SteinhardtJul 1, 2026
  8. 03/13 download_https_uri_to_file(): do not leak fd upon failureJohannes Schindelin via GitGitGadget, Jul 1, 2026
  9. 04/13 run-command: avoid close(-1) in start_command() error pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  10. Patrick SteinhardtJul 1, 2026
  11. 05/13 run_diff_files: avoid memory leakJohannes Schindelin via GitGitGadget, Jul 1, 2026
  12. Patrick SteinhardtJul 1, 2026
  13. Johannes SchindelinJul 4, 2026
  14. 06/13 line-log: avoid redundant copy that leaks in process_rangesJohannes Schindelin via GitGitGadget, Jul 1, 2026
  15. Jeff KingJul 1, 2026
  16. 07/13 dir: free allocations on parse-error paths in read_one_dir()Johannes Schindelin via GitGitGadget, Jul 1, 2026
  17. Patrick SteinhardtJul 1, 2026
  18. Johannes SchindelinJul 4, 2026
  19. 08/13 submodule: fix cwd leak in get_superproject_working_tree()Johannes Schindelin via GitGitGadget, Jul 1, 2026
  20. Patrick SteinhardtJul 1, 2026
  21. Johannes SchindelinJul 4, 2026
  22. 09/13 worktree: fix resource leaks when branch creation failsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  23. 10/13 imap-send: avoid leaking the IMAP upload bufferJohannes Schindelin via GitGitGadget, Jul 1, 2026
  24. 11/13 reftable/table: release filter on error pathJohannes Schindelin via GitGitGadget, Jul 1, 2026
  25. 12/13 fsmonitor: plug token-data leak on early daemon-startup failuresJohannes Schindelin via GitGitGadget, Jul 1, 2026
  26. 13/13 mingw: make exit_process() own the process handle on all pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  27. Junio C HamanoJul 1, 2026
  28. 00/12 coverity: fix leaks and error pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  29. 01/12 load_one_loose_object_map(): fix resource leakJohannes Schindelin via GitGitGadget, Jul 5, 2026
  30. Patrick SteinhardtJul 7, 2026
  31. 02/12 loose: avoid closing invalid fd on error pathJohannes Schindelin via GitGitGadget, Jul 5, 2026
  32. 03/12 download_https_uri_to_file(): do not leak fd upon failureJohannes Schindelin via GitGitGadget, Jul 5, 2026
  33. 04/12 run-command: avoid `close(-1)` in `start_command()` error pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  34. 05/12 line-log: avoid redundant copy that leaks in process_rangesJohannes Schindelin via GitGitGadget, Jul 5, 2026
  35. 06/12 dir: free allocations on parse-error paths in `read_one_dir()`Johannes Schindelin via GitGitGadget, Jul 5, 2026
  36. 07/12 submodule: fix cwd leak in `get_superproject_working_tree()`Johannes Schindelin via GitGitGadget, Jul 5, 2026
  37. Patrick SteinhardtJul 7, 2026
  38. 08/12 worktree: fix resource leaks when branch creation failsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  39. 09/12 imap-send: avoid leaking the IMAP upload bufferJohannes Schindelin via GitGitGadget, Jul 5, 2026
  40. 10/12 reftable/table: release filter on error pathJohannes Schindelin via GitGitGadget, Jul 5, 2026
  41. 11/12 fsmonitor: plug token-data leak on early daemon-startup failuresJohannes Schindelin via GitGitGadget, Jul 5, 2026
  42. 12/12 mingw: make `exit_process()` own the process handle on all pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  43. Patrick SteinhardtJul 7, 2026
  44. Junio C HamanoJul 7, 2026

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.