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

Re: [PATCH] parse-options-cb.c: use string_list_append_nodup in OPT_STRING_LIST()

From
Jeff King <peff@peff.net>
Date
Jun 12, 2016, 22:03 UTC
Message-ID
<20160612220316.GB5428@sigill.intra.peff.net>
In-Reply-To
<20160610115726.4805-1-pclouds@gmail.com>
On Fri, Jun 10, 2016 at 06:57:26PM +0700, Nguyễn Thái Ngọc Duy wrote:
Show 20 quoted lines
> If the given string list has strdup_strings set (*), the string will be
> duplicated again. Pointless and leak memory. Ignore that flag.
> 
> (*) only interpret-trailers.c does it at the moment
> 
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  parse-options-cb.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/parse-options-cb.c b/parse-options-cb.c
> index 239898d..8a1b6e6 100644
> --- a/parse-options-cb.c
> +++ b/parse-options-cb.c
> @@ -144,7 +144,7 @@ int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
>  	if (!arg)
>  		return -1;
>  
> -	string_list_append(v, xstrdup(arg));
> +	string_list_append_nodup(v, xstrdup(arg));

Hmm. So I agree this is an improvement, in the sense that we are double-allocating when v->strdup_strings is set. But I think there's a deeper issue here. Why are we always allocating in the first place?

If the memory we are getting in "arg" is not stable, then we _do_ need to make a copy of it. But in that case, we want "strdup_strings" to be set; without it any time we later run string_list_clear(), we leak the allocated memory, because the struct has no idea that it is the owner of the memory (and we do call string_list_clear() when we see "--no-foo").

If the memory _is_ stable, then we are fine to add a direct reference to it, and can lose the extra xstrdup() here. Only the caller knows for sure, so we should be respecting their value of strdup_strings (so lose the xstrdup, but keep calling string_list_append()).

In practice, I suspect the memory _is_ stable, because we are generally parsing command-line arguments. But it does not hurt to stay on the conservative side, and always make a copy (in case we are parsing something besides the global argv array) . Apparently I am the original author of this code, in c8ba163 (parse-options: add OPT_STRING_LIST helper, 2011-06-09), but there's no mention of this point there, in the list archives, or in my brain.

So if we are doing the conservative thing, then I think the resulting code should either look like:

  if (!v->strdup_strings)
	die("BUG: OPT_STRING_LIST should always use strdup_strings");
  string_list_append(v, arg);
or:
  /* silently enable for convenience */
  v->strdup_strings = 1;
  string_list_append(v, arg);

Of the two, I like the top one as it is less magical, but it would require adjusting the initialization of the string-list for most of the callers.

-Peff
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 2 of 15 in “parse-options-cb.c: use string_list_append_nodup in OPT_STRING_LIST()”
  1. parse-options-cb.c: use string_list_append_nodup in OPT_STRING_LIST()Nguyễn Thái Ngọc Duy, Jun 10, 2016
  2. Jeff KingJun 12, 2016
  3. Duy NguyenJun 13, 2016
  4. 0/3 fix parse-opt string_list leaksJeff King, Jun 13, 2016
  5. 1/3 parse_opt_string_list: stop allocating new stringsJeff King, Jun 13, 2016
  6. 2/3 interpret-trailers: don't duplicate option stringsJeff King, Jun 13, 2016
  7. 3/3 blame,shortlog: don't make local option variables staticJeff King, Jun 13, 2016
  8. Eric SunshineJun 14, 2016
  9. Jeff KingJun 14, 2016
  10. blame: drop strdup of string literalJeff King, Aug 2, 2016
  11. Eric SunshineAug 3, 2016
  12. Duy NguyenJun 13, 2016
  13. 4/3 use string_list initializer consistentlyJeff King, Jun 13, 2016
  14. Duy NguyenJun 13, 2016
  15. Jeff KingJun 13, 2016

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.