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

Re: [PATCH 0/4] use xmalloc in more places

From
Jeff King <peff@peff.net>
Date
Apr 11, 2019, 20:04 UTC
Message-ID
<20190411200452.GA19315@sigill.intra.peff.net>
In-Reply-To
<20190411194308.GA26327@D-10-18-172-132.dhcp4.washington.edu>
On Thu, Apr 11, 2019 at 12:43:08PM -0700, Taylor Blau wrote:
Show 9 quoted lines
> > I don't think we can ban malloc, since we have to use it ourselves. :)
> >
> > With some contortions, we probably could unban it specifically in
> > wrapper.c (though note there are a few other calls I've left which would
> > need to be handled somehow).
> 
> Right. I think that I should have made this point clearer in my initial
> reply. I was thinking that we could #undef the banned macro in
> wrapper.c, or some similar hula-hooping.

That _probably_ works, but I think technically falls afoul of platforms on which there's a malloc macro in the first place. We need to not just #undef it then, but restore the original macro, which is impossible. So you're better off just not fudging it in the first place. Which would probably be something like:

  #define SUPPRESS_BAN_MALLOC
  #include "git-compat-util.h"
or something, with the appropriate magic in banned.h.

This might be academic, but you wouldn't know until somebody's platform subtly breaks. ;) We did run into it already with strcpy(), I think, hence the defensive #undefs in banned.h.

Show 9 quoted lines
> Yeah... maybe that's the bigger question that I hadn't asked. I made the
> suggestion thinking that it would help newcomers avoid writing
> 'malloc()' and sending it if they didn't know we use our 'xmalloc()'
> instead.
> 
> But I'm not sure if the argument holds up. I think that in general
> exactly the sorts of new-comers that I'm thinking of would have more
> than one review cycle anyway, so it might not be worth the effort,
> anyway...

I think it's still a reasonable thought, even if I'm not sure the balance of cost/reward is quite there so far (but might change if it's an error we see people start to make). Compared to coccinelle, the banned-function approach is a little nicer for helping new submitters because it catches the problem during a normal compile (and we know nobody would ever submit a patch without having at least compiled it, right? ;) ).

-Peff
Previous: Taylor Blau
Message 9 of 9 in “use xmalloc in more places”
  1. 0/4 use xmalloc in more placesJeff King, Apr 11, 2019
  2. 1/4 test-prio-queue: use xmallocJeff King, Apr 11, 2019
  3. 2/4 xdiff: use git-compat-utilJeff King, Apr 11, 2019
  4. 3/4 xdiff: use xmalloc/xreallocJeff King, Apr 11, 2019
  5. 4/4 progress: use xmalloc/xcallocJeff King, Apr 11, 2019
  6. Taylor BlauApr 11, 2019
  7. Jeff KingApr 11, 2019
  8. Taylor BlauApr 11, 2019
  9. Jeff KingApr 11, 2019

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.