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

Re: [PATCH 03/12] ls-files: free max_prefix when done

From
Andrzej Hunt <andrzej@ahunt.org>
Date
Apr 25, 2021, 13:16 UTC
Message-ID
<11ae3ce9-997b-32fc-7bc3-ee95a3d99153@ahunt.org>
In-Reply-To
<6a72a920-134f-541b-7caa-debe24658005@web.de>
On 10/04/2021 10:12, René Scharfe wrote:
Show 21 quoted lines
> Am 09.04.21 um 20:47 schrieb Andrzej Hunt via GitGitGadget:
>> From: Andrzej Hunt <ajrhunt@google.com>
>> diff --git a/builtin/ls-files.c b/builtin/ls-files.c
>> index 60a2913a01e9..53e20bbf9cce 100644
>> --- a/builtin/ls-files.c
>> +++ b/builtin/ls-files.c
>> @@ -781,5 +781,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
>>   	}
>>
>>   	dir_clear(&dir);
>> +	free((void *)max_prefix);
> 
> This cast is necessary to ignore the const attribute of the pointer.
> It's scary, but safe here because this function owns the referenced
> object.
> 
> I think the promise to not modify the string given at the top of the
> function is not worth having to take back that promise forcefully at
> the end to dispose of it.  Determining the correctness of this cast
> requires reading the whole function.  Removing the const from the
> declaration (and the cast) would improve readability overall.  Thoughts?

I agree - I'll change this in V2 V2. In fact, Peff already given the following explanation for why non-const is preferred in this scenario on a previous patch of mine (which I failed to heed when preparing this patch):

 > If a variable is meant to take ownership of memory, our usual
 > convention is to not declare it as "const"."
https://lore.kernel.org/git/YEZ0jLppB9wOg%2Faf@coredump.intra.peff.net/
Show 5 quoted lines
> 
>>   	return 0;
>>   }
>>
> 
Previous: René ScharfeNext: Andrzej Hunt via GitGitGadget
Message 7 of 35 in “Fix all leaks in tests t0002-t0099: Part 1”
  1. 00/12 Fix all leaks in tests t0002-t0099: Part 1Andrzej Hunt via GitGitGadget, Apr 9, 2021
  2. 01/12 revision: free remainder of old commit list in limit_listAndrzej Hunt via GitGitGadget, Apr 9, 2021
  3. René ScharfeApr 10, 2021
  4. Andrzej HuntApr 25, 2021
  5. 03/12 ls-files: free max_prefix when doneAndrzej Hunt via GitGitGadget, Apr 9, 2021
  6. René ScharfeApr 10, 2021
  7. Andrzej HuntApr 25, 2021
  8. 02/12 wt-status: fix multiple small leaksAndrzej Hunt via GitGitGadget, Apr 9, 2021
  9. 05/12 branch: FREE_AND_NULL instead of NULL'ing real_refAndrzej Hunt via GitGitGadget, Apr 9, 2021
  10. 04/12 bloom: clear each bloom_key after useAndrzej Hunt via GitGitGadget, Apr 9, 2021
  11. SZEDER GáborApr 11, 2021
  12. Andrzej HuntApr 25, 2021
  13. 06/12 builtin/bugreport: don't leak prefixed filenameAndrzej Hunt via GitGitGadget, Apr 9, 2021
  14. 07/12 builtin/check-ignore: clear_pathspec before returningAndrzej Hunt via GitGitGadget, Apr 9, 2021
  15. 08/12 builtin/checkout: clear pending objects after diffingAndrzej Hunt via GitGitGadget, Apr 9, 2021
  16. 09/12 mailinfo: also free strbuf lists when clearing mailinfoAndrzej Hunt via GitGitGadget, Apr 9, 2021
  17. Junio C HamanoApr 11, 2021
  18. Andrzej HuntApr 25, 2021
  19. 10/12 builtin/for-each-ref: free filter and UNLEAK sorting.Andrzej Hunt via GitGitGadget, Apr 9, 2021
  20. 11/12 builtin/rebase: release git_format_patch_opt tooAndrzej Hunt via GitGitGadget, Apr 9, 2021
  21. 12/12 builtin/rm: avoid leaking pathspec and seenAndrzej Hunt via GitGitGadget, Apr 9, 2021
  22. 00/12 Fix all leaks in tests t0002-t0099: Part 1Andrzej Hunt via GitGitGadget, Apr 25, 2021
  23. 01/12 revision: free remainder of old commit list in limit_listAndrzej Hunt via GitGitGadget, Apr 25, 2021
  24. 02/12 wt-status: fix multiple small leaksAndrzej Hunt via GitGitGadget, Apr 25, 2021
  25. 03/12 ls-files: free max_prefix when doneAndrzej Hunt via GitGitGadget, Apr 25, 2021
  26. 05/12 branch: FREE_AND_NULL instead of NULL'ing real_refAndrzej Hunt via GitGitGadget, Apr 25, 2021
  27. 04/12 bloom: clear each bloom_key after useAndrzej Hunt via GitGitGadget, Apr 25, 2021
  28. 06/12 builtin/bugreport: don't leak prefixed filenameAndrzej Hunt via GitGitGadget, Apr 25, 2021
  29. 07/12 builtin/check-ignore: clear_pathspec before returningAndrzej Hunt via GitGitGadget, Apr 25, 2021
  30. 09/12 mailinfo: also free strbuf lists when clearing mailinfoAndrzej Hunt via GitGitGadget, Apr 25, 2021
  31. Junio C HamanoApr 28, 2021
  32. 10/12 builtin/for-each-ref: free filter and UNLEAK sorting.Andrzej Hunt via GitGitGadget, Apr 25, 2021
  33. 08/12 builtin/checkout: clear pending objects after diffingAndrzej Hunt via GitGitGadget, Apr 25, 2021
  34. 11/12 builtin/rebase: release git_format_patch_opt tooAndrzej Hunt via GitGitGadget, Apr 25, 2021
  35. 12/12 builtin/rm: avoid leaking pathspec and seenAndrzej Hunt via GitGitGadget, Apr 25, 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.