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

Re: [PATCH] parse_object: check if buffer is non-NULL before freeing it

From
Junio C Hamano <junkio@cox.net>
Date
Aug 28, 2006, 05:46 UTC
Message-ID
<7v3bbhtntq.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0608272131020.27779@g5.osdl.org>
Linus Torvalds <torvalds@osdl.org> writes:
Show 11 quoted lines
> On Sun, 27 Aug 2006, Junio C Hamano wrote:
>> 
>> Eh, free(NULL) should work just fine.  It is "other places" that
>> is misguided and needs to be fixed.
>
> Well, some very old libraries will SIGSEGV on free(NULL). 
>
> Admittedly those libraries are either very old or _very_ broken, but if 
> you want to be strictly portable, you should not ever pass NULL to free(), 
> unless you actually got it from a malloc(0) (and even then, it might be a 
> really broken libc that just ran out of memory).

Fair enough, but I think there are many places we already assume the library handles free(NULL) sensibly.

> I actually suspect we should wrap all free() calls as "xfree()", which may 
> also help us some day if we want to do any memory usage statistics.
That sounds sensible.

Another thing I was thinking about was to extend the existing XMALLOC_POISON debugging to allow also xrealloc()'ed area. That would unfortunately involve wrapping strdup() and x*alloc() to make sure all allocations we do go through xmalloc() and then store the current allocation size somewhere hidden (immediately before, perhaps) in the area xmalloc() returns, but at that point running git under valgrind would probably be easier.

Previous: Linus Torvalds
Message 4 of 4 in “parse_object: check if buffer is non-NULL before freeing it”
  1. parse_object: check if buffer is non-NULL before freeing itJonas Fonseca, Aug 28, 2006
  2. Junio C HamanoAug 28, 2006
  3. Linus TorvaldsAug 28, 2006
  4. Junio C HamanoAug 28, 2006

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.