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

Re: [PATCH 3/7] clone: free or UNLEAK further pointers when finished

From
Andrzej Hunt <andrzej@ahunt.org>
Date
Mar 14, 2021, 16:56 UTC
Message-ID
<9856ec4c-b8dc-3c93-ee20-f818672375b7@ahunt.org>
In-Reply-To
<YEZ3Gf0f/NfXiwW2@coredump.intra.peff.net>
On 08/03/2021 20:12, Jeff King wrote:
Show 14 quoted lines
> On Mon, Mar 08, 2021 at 06:36:16PM +0000, Andrzej Hunt via GitGitGadget wrote:
> 
>> @@ -1017,9 +1017,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>>   	repo_name = argv[0];
>>   
>>   	path = get_repo_path(repo_name, &is_bundle);
>> -	if (path)
>> +	if (path) {
>> +		free(path);
>>   		repo = absolute_pathdup(repo_name);
> 
> You mentioned that "path" gets reused again later. Should we use
> FREE_AND_NULL() to make sure that nobody tries to look at it in the
> meantime?

That sounds sensible - I wasn't too sure at first because we are unconditionally setting path later... but I can't see an reason not to make this safer.

But that makes me wonder if the definition of path should set it to NULL too. Setting it to NULL after freeing it guarantees that we can't read the old value anymore. However later code has no idea if path will be NULL or merely initialised (at least until we overwrite it with the new path).

I've provisionally updated my patch to also set path = NULL at point of definition, but I don't know if that's idiomatic in this scenario.

Show 13 quoted lines
> 
>> @@ -1393,6 +1394,12 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>>   	strbuf_release(&reflog_msg);
>>   	strbuf_release(&branch_top);
>>   	strbuf_release(&key);
>> +	free_refs(mapped_refs);
>> +	free_refs((void *)remote_head_points_at);
> 
> We should avoid casting away constness when possible (because it is
> often a sign that sometimes the variable _isn't_ pointing to owned
> memory). In this case, I think freeing is the right thing; our
> guess_remote_head() returns a copy of the struct (which is non-const).
> Should remote_head_points_at just be declared without const?
I think so - I'll change remote_head_points_at to non-const.
Show 10 quoted lines
>> +	free_refs((void *)refs);
> 
> This one is more questionable to me. It comes from
> transport_get_remote_refs(), which does return a const pointer. And it
> looks like that memory is owned by the transport struct. So presumably
> we need to tell the transport code to clean itself up (or mark it with
> UNLEAK). Or perhaps there's a bug in the transport code (e.g., should it
> be freeing transport->remote_refs in transport_disconnect()? You'd want
> to make sure that no other callers expect the ref list to live on past
> the disconnect).

There are indeed multiple locations where we store the fetched refs, followed by transport_disconnect(), followed by trying to use the refs (that are nomimanlly owned by the now disconnected transport):

https://git.kernel.org/pub/scm/git/git.git/tree/builtin/remote.c?h=next#n953 https://git.kernel.org/pub/scm/git/git.git/tree/builtin/ls-remote.c?h=next#n122

However all other locations could handle free()'ing during transport_disconnect - and the 2 I've linked to above are easy enough to fix. So I'll give up on free_refs() from cmd_clone(), and will move it into transport_disconnect() as suggested. I've added a new patch to the end of the series to take care of this change.

(Which ultimately means we've now solved the same pattern of leak for all 5 users of transport_get_remote_refs().)

Previous: Jeff KingNext: Andrzej Hunt via GitGitGadget
Message 9 of 52 in “Fix all leaks in t0001”
  1. 0/7 Fix all leaks in t0001Andrzej Hunt via GitGitGadget, Mar 8, 2021
  2. 1/7 symbolic-ref: don't leak shortened refname in check_symref()Andrzej Hunt via GitGitGadget, Mar 8, 2021
  3. Jeff KingMar 8, 2021
  4. Andrzej HuntMar 14, 2021
  5. 2/7 reset: free instead of leaking unneeded refAndrzej Hunt via GitGitGadget, Mar 8, 2021
  6. Jeff KingMar 8, 2021
  7. 3/7 clone: free or UNLEAK further pointers when finishedAndrzej Hunt via GitGitGadget, Mar 8, 2021
  8. Jeff KingMar 8, 2021
  9. Andrzej HuntMar 14, 2021
  10. 6/7 init-db: silence template_dir leak when converting to absolute pathAndrzej Hunt via GitGitGadget, Mar 8, 2021
  11. Jeff KingMar 8, 2021
  12. 4/7 worktree: fix leak in dwim_branch()Andrzej Hunt via GitGitGadget, Mar 8, 2021
  13. Jeff KingMar 8, 2021
  14. Andrzej HuntMar 14, 2021
  15. 7/7 parse-options: don't leak alias help messagesAndrzej Hunt via GitGitGadget, Mar 8, 2021
  16. Jeff KingMar 8, 2021
  17. Andrzej HuntMar 14, 2021
  18. 5/7 init: remove git_init_db_config() while fixing leaksAndrzej Hunt via GitGitGadget, Mar 8, 2021
  19. Jeff KingMar 8, 2021
  20. Jeff KingMar 8, 2021
  21. Junio C HamanoMar 12, 2021
  22. Andrzej HuntMar 14, 2021
  23. Andrzej HuntMar 15, 2021
  24. Junio C HamanoMar 8, 2021
  25. Andrzej HuntMar 14, 2021
  26. 0/9 Fix all leaks in t0001Andrzej Hunt via GitGitGadget, Mar 14, 2021
  27. 8/9 parse-options: don't leak alias help messagesAndrzej Hunt via GitGitGadget, Mar 14, 2021
  28. Eric SunshineMar 14, 2021
  29. Andrzej HuntMar 15, 2021
  30. Andrzej HuntMar 14, 2021
  31. 1/9 symbolic-ref: don't leak shortened refname in check_symref()Andrzej Hunt via GitGitGadget, Mar 14, 2021
  32. 7/9 parse-options: convert bitfield values to use binary shiftAndrzej Hunt via GitGitGadget, Mar 14, 2021
  33. Martin ÅgrenMar 14, 2021
  34. Junio C HamanoMar 14, 2021
  35. Andrzej HuntMar 15, 2021
  36. 4/9 worktree: fix leak in dwim_branch()Andrzej Hunt via GitGitGadget, Mar 14, 2021
  37. 5/9 init: remove git_init_db_config() while fixing leaksAndrzej Hunt via GitGitGadget, Mar 14, 2021
  38. 3/9 clone: free or UNLEAK further pointers when finishedAndrzej Hunt via GitGitGadget, Mar 14, 2021
  39. 2/9 reset: free instead of leaking unneeded refAndrzej Hunt via GitGitGadget, Mar 14, 2021
  40. 9/9 transport: also free remote_refs in transport_disconnect()Andrzej Hunt via GitGitGadget, Mar 14, 2021
  41. 6/9 init-db: silence template_dir leak when converting to absolute pathAndrzej Hunt via GitGitGadget, Mar 14, 2021
  42. 0/9 Fix all leaks in t0001Andrzej Hunt via GitGitGadget, Mar 21, 2021
  43. 1/9 symbolic-ref: don't leak shortened refname in check_symref()Andrzej Hunt via GitGitGadget, Mar 21, 2021
  44. 2/9 reset: free instead of leaking unneeded refAndrzej Hunt via GitGitGadget, Mar 21, 2021
  45. 4/9 worktree: fix leak in dwim_branch()Andrzej Hunt via GitGitGadget, Mar 21, 2021
  46. 3/9 clone: free or UNLEAK further pointers when finishedAndrzej Hunt via GitGitGadget, Mar 21, 2021
  47. 7/9 parse-options: convert bitfield values to use binary shiftAndrzej Hunt via GitGitGadget, Mar 21, 2021
  48. 6/9 init-db: silence template_dir leak when converting to absolute pathAndrzej Hunt via GitGitGadget, Mar 21, 2021
  49. 5/9 init: remove git_init_db_config() while fixing leaksAndrzej Hunt via GitGitGadget, Mar 21, 2021
  50. 9/9 transport: also free remote_refs in transport_disconnect()Andrzej Hunt via GitGitGadget, Mar 21, 2021
  51. 8/9 parse-options: don't leak alias help messagesAndrzej Hunt via GitGitGadget, Mar 21, 2021
  52. Junio C HamanoMar 21, 2021

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.