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

Re: [PATCH 09/12] mailinfo: also free strbuf lists when clearing mailinfo

From
Andrzej Hunt <andrzej@ahunt.org>
Date
Apr 25, 2021, 13:15 UTC
Message-ID
<437e7381-7f93-b314-d9c6-d0f0e26ea683@ahunt.org>
In-Reply-To
<xmqq4kgdhws6.fsf@gitster.g>
On 11/04/2021 13:43, Junio C Hamano wrote:
Show 41 quoted lines
> "Andrzej Hunt via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>>   void clear_mailinfo(struct mailinfo *mi)
>>   {
>> -	int i;
>> -
>>   	strbuf_release(&mi->name);
>>   	strbuf_release(&mi->email);
>>   	strbuf_release(&mi->charset);
>>   	strbuf_release(&mi->inbody_header_accum);
>>   	free(mi->message_id);
>>   
>> -	if (mi->p_hdr_data)
>> -		for (i = 0; mi->p_hdr_data[i]; i++)
>> -			strbuf_release(mi->p_hdr_data[i]);
>> -	free(mi->p_hdr_data);
>> -	if (mi->s_hdr_data)
>> -		for (i = 0; mi->s_hdr_data[i]; i++)
>> -			strbuf_release(mi->s_hdr_data[i]);
>> -	free(mi->s_hdr_data);
> 
> So, the original allows mi->p_hdr_data to be NULL and does not do
> this freeing (the same for the .s_hdr_data member).
> 
>> +	strbuf_list_free(mi->p_hdr_data);
>> +	strbuf_list_free(mi->s_hdr_data);
> 
> Is it safe to feed NULL to the helper?
> 
>          void strbuf_list_free(struct strbuf **sbs)
>          {
>                  struct strbuf **s = sbs;
> 
>                  while (*s) {
>                          strbuf_release(*s);
>                          free(*s++);
>                  }
>                  free(sbs);
>          }
> 
> 
Indeed: AFAIUI dereferencing NULL is undefined 	behaviour. I think the 
best solution is to add a NULL check in strbuf_list_free() - which is 
the pattern I've seen in several other *_free() helpers (there are also 
quite a few examples of *_free() helpers that are not NULL safe, but 
IMHO having a NULL check will lead to fewer unpleasant surprises).

Incidentally I did run the entire test-suite against UBSAN, and it didn't find any issues here. This seems like something that UBSAN should be able to easily catch, so we probably don't have any tests exercising clear_mailinfo() with NULL p_hdr_info/s_hdr_info?

Previous: Junio C HamanoNext: Andrzej Hunt via GitGitGadget
Message 18 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.