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

Re: [PATCH] Remove useless if-before-free tests.

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 22, 2008, 23:05 UTC
Message-ID
<7vd4qo7fsc.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<87tzk0tzjz.fsf@rho.meyering.net>
Jim Meyering <jim@meyering.net> writes:
Show 16 quoted lines
> This change removes all obvious useless if-before-free tests.
> E.g., it replaces code like this:
>
>         if (some_expression)
>                 free (some_expression);
>
> with the now-equivalent:
>
>         free (some_expression);
>
> ...
>
> If you're interested in automating detection of the useless
> tests, you might like the useless-if-before-free script in gnulib:
> [it *does* detect brace-enclosed free statements, and has a --name=S
>  option to make it detect free-like functions with different names]
While I have your attention ;-)

I am not interested in automating useless "if (x) free(x)" tests, but one thing I recently wanted but did not know a handy tool for was to find all the calls to free() that free a pointer to an object of a particular type. More specifically, we seem to allocate and free many "struct commit_list", and I wanted to introduce a custom bulk allocator. Allocate many of them in a block, hand out one by one, and tell callers to hand them back not to free() but to the allocator so that it can keep the returned ones on a linked list and hand them back again when the next call wanted to allocate one without actually calling xmalloc()). But in order to do so, missed conversion from malloc() to the custom allocator is not fatal (just wasteful), but forgetting to convert free() really is.

I guess sparse could be hacked to do that, but do GNU folks have some checker like that?

Previous: Jim MeyeringNext: Jim Meyering
Message 13 of 18 in “Remove useless if-before-free tests.”
  1. Remove useless if-before-free tests.Jim Meyering, Feb 17, 2008
  2. David SymondsFeb 17, 2008
  3. Jim MeyeringFeb 18, 2008
  4. Junio C HamanoFeb 18, 2008
  5. Johannes SchindelinFeb 17, 2008
  6. Jim MeyeringFeb 18, 2008
  7. Jean-Luc HerrenFeb 18, 2008
  8. Jim MeyeringFeb 20, 2008
  9. Junio C HamanoFeb 22, 2008
  10. Jim MeyeringFeb 22, 2008
  11. Junio C HamanoFeb 22, 2008
  12. Jim MeyeringFeb 22, 2008
  13. Junio C HamanoFeb 22, 2008
  14. Jim MeyeringFeb 24, 2008
  15. Mike RalphsonFeb 26, 2009
  16. Uwe Kleine-KönigFeb 26, 2008
  17. Morten WelinderFeb 23, 2008
  18. Johannes SchindelinFeb 24, 2008

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.