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

Re: [PATCH 0/12] git_config_string() considered harmful

From
Jeff King <peff@peff.net>
Date
Apr 8, 2024, 20:55 UTC
Message-ID
<20240408205511.GB1639295@coredump.intra.peff.net>
In-Reply-To
<ef8e5f43-5f27-4f0a-acf5-cf4f8281a8f8@gmail.com>
On Sun, Apr 07, 2024 at 07:58:02PM +0200, Rubén Justo wrote:
Show 24 quoted lines
> After Junio's series and yours, I'm on the fence now, but my envision was
> to introduce:
> 
> --- >8 ---
> diff --git a/config.c b/config.c
> index eebce8c7e0..7322bdfb94 100644
> --- a/config.c
> +++ b/config.c
> @@ -1345,6 +1345,15 @@ int git_config_string(const char **dest, const char *var, const char *value)
>  	return 0;
>  }
>  
> +int git_config_strbuf(struct strbuf *dest, const char *var, const char *value)
> +{
> +	if (!value)
> +		return config_error_nonbool(var);
> +	strbuf_reset(dest);
> +	strbuf_addstr(dest, value);
> +	return 0;
> +}
> +
>  int git_config_pathname(const char **dest, const char *var, const char *value)
>  {
>  	if (!value)

Hmm. I think that is nice in some ways, because it is a much bigger signal about memory ownership than just dropping "const" from the pointer.

But it is less nice in other ways. Every _user_ of the value now needs to care that it is a strbuf, and use foo.buf consistently (which you obviously noticed). Likewise, any downstream writers of the variable need to treat it like a strbuf, too. So the parse-options OPT_FILENAME() macro, for example, needs to be replaced with a strbuf-aware variant (though arguably that is an improvement, as using the wrong one would fail catastrophically, whereas using a non-const pointer with OPT_FILENAME() creates a subtle bug).

I'm also not sure what the solution is for setting default values, like:
  const char *my_config = "default";

Of course that is a problem with my solution, too. Perhaps in the long run we need to accept that they should always default to NULL, and readers of the variable need to fill in the default when they look at it (possibly with an accessor function).

Or I guess the alternative is to stop using bare pointers, and carry a bit which tells us if something is heap-allocated. Which starts to look an awful lot like a strbuf. ;)

I think in the past we've talked about being able to initialize a strbuf like:

  struct strbuf foo = STRBUF_INIT_VAL("bar");

That would use "bar" instead of the usual slopbuf, but we can still tell it's not a heap buffer by the fact that foo.alloc is 0.

-Peff
Previous: Rubén JustoNext: Rubén Justo
Message 22 of 23 in “config: do not leak excludes_file”
  1. config: do not leak excludes_fileJunio C Hamano, Apr 6, 2024
  2. [WIP] git_config_pathname() leakfixJunio C Hamano, Apr 6, 2024
  3. 0/12 git_config_string() considered harmfulJeff King, Apr 7, 2024
  4. 01/11 config: make sequencer.c's git_config_string_dup() publicJeff King, Apr 7, 2024
  5. 02/11 config: add git_config_pathname_dup()Jeff King, Apr 7, 2024
  6. 03/11 config: prefer git_config_string_dup() for temp variablesJeff King, Apr 7, 2024
  7. Eric SunshineApr 7, 2024
  8. Jeff KingApr 7, 2024
  9. 04/11 config: use git_config_string_dup() for open-coded equivalentsJeff King, Apr 7, 2024
  10. 05/11 config: use git_config_string_dup() to fix leaky open codingJeff King, Apr 7, 2024
  11. 06/11 config: use git_config_string() in easy casesJeff King, Apr 7, 2024
  12. Eric SunshineApr 7, 2024
  13. Jeff KingApr 7, 2024
  14. 07/11 config: use git_config_pathname_dup() in easy casesJeff King, Apr 7, 2024
  15. 08/11 http: use git_config_string_dup()Jeff King, Apr 7, 2024
  16. 09/11 merge: use git_config_string_dup() for pull strategiesJeff King, Apr 7, 2024
  17. 10/11 userdiff: use git_config_string_dup() when we canJeff King, Apr 7, 2024
  18. 11/11 blame: use "dup" string_list for ignore-revs filesJeff King, Apr 7, 2024
  19. Eric SunshineApr 7, 2024
  20. Jeff KingApr 7, 2024
  21. Rubén JustoApr 7, 2024
  22. Jeff KingApr 8, 2024
  23. Rubén JustoApr 11, 2024

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.