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

Re: [PATCH 2/2] sequencer.c: plug mem leak in git_sequencer_config

From
Jeff King <peff@peff.net>
Date
Jun 4, 2018, 05:00 UTC
Message-ID
<20180604050033.GA17818@sigill.intra.peff.net>
In-Reply-To
<20180604035637.GA15408@sigill.intra.peff.net>
On Sun, Jun 03, 2018 at 11:56:37PM -0400, Jeff King wrote:
Show 25 quoted lines
> So sometimes some_var needs to be freed and sometimes not (and every one
> of those uses is a potential leak, but it's OK because they're all
> program-lifetime globals anyway, and people don't _tend_ to set the same
> option over and over, leaking heap memory). And C being C, we can't
> convert a pointer-to-pointer-to-const into a pointer-to-pointer without
> an explicit cast.
> 
> Doing it "right" in C would probably involve two variables:
> 
>   const char *some_var = "default";
>   const char *some_var_storage = NULL;
> 
>   int git_config_string_smart(const char **ptr, char **storage,
>                               const char *var, const char *value)
>   {
>         ...
> 	free(*storage);
> 	*ptr = *storage = xstrdup(value);
> 	return 0;
>   }
> 
>   #define GIT_CONFIG_STRING(name, var, value) \
>   git_config_string_smart(&(name), &(name##_storage), var, value)
> 
> Or something like that.

Oh, one other option I forgot to mention: we already have an "intern" hashmap helper. So we could just replace the xstrdup() with strintern() and magically the memory isn't "leaked".

I think this is a little bit of a hack, though. It catches my:
  [core]
  editor = foo
  editor = foo

case, because we only ever make one copy of the string "foo". But if I do:

  [core]
  editor = 1
  editor = 2
  ...

then we get unique strings. And while they're not _technically_ leaked, since the hashmap still knows about them, they might as well be. They're still wasting space through the duration of the program run.

-Peff
Previous: Jeff King
Message 11 of 11 in “sequencer.c: plug leaks in do_pick_commit”
  1. 1/2 sequencer.c: plug leaks in do_pick_commitStefan Beller, Jun 1, 2018
  2. 2/2 sequencer.c: plug mem leak in git_sequencer_configStefan Beller, Jun 1, 2018
  3. Junio C HamanoJun 4, 2018
  4. Junio C HamanoJun 4, 2018
  5. Jeff KingJun 4, 2018
  6. Junio C HamanoJun 4, 2018
  7. Jeff KingJun 4, 2018
  8. Junio C HamanoJun 4, 2018
  9. Johannes SchindelinJun 21, 2018
  10. Jeff KingJun 21, 2018
  11. Jeff KingJun 4, 2018

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.