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

[PATCH 0/3] fix parse-opt string_list leaks

From
Jeff King <peff@peff.net>
Date
Jun 13, 2016, 05:32 UTC
Message-ID
<20160613053203.GB3950@sigill.intra.peff.net>
In-Reply-To
<CACsJy8C+NtiXRo8NcU3rtgWrMSj8Zv3mYtdYfyvzwYRHifKVCQ@mail.gmail.com>
On Mon, Jun 13, 2016 at 07:08:55AM +0700, Duy Nguyen wrote:
Show 13 quoted lines
> > 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);
> 
> I agree with the analysis. But this die() would hit all callers
> (except interpret-trailers) because they all initialize with _NODUP
> and setting strdup_strings may require auditing all access to the
> string list in question, e.g. to change string_list_append(v,
> xstrdup(xxx)) to string_list_append(xxx). it may cause side effects if
> we are not careful.

Yep. It is not really fixing anything, so much as alerting us to broken callers. We'd still have to fix the callers. :)

Show 8 quoted lines
> So far all callers are in builtin/, I think it will not take much time
> to verify that they all call parse_options() with global argv, then we
> can just lose extra xstrdup() and stick to string_list_append().
> OPTION_STRING already assumes that argument strings are stable because
> they are passed back as-is. Can we go with an easier route, adding a
> comment on top of parse_options() stating that argv[] pointers may be
> passed back as-is and it's up to the caller to xstrdup() appropriately
> before argv[] memory is freed?

Yeah, the two options I laid out were the "conservative" side, where we didn't make any assumptions about what is in passed into parse_options. But I agree in practice that it's not likely to be a problem to just point to the existing strings, and the fact that OPTION_STRING does so already makes me even more confident.

So I'd suggest these patches:
  [1/3]: parse_opt_string_list: stop allocating new strings
  [2/3]: interpret-trailers: don't duplicate option strings
  [3/3]: blame,shortlog: don't make local option variables static

The first one is what we've been discussing, and the others are just follow-on cleanups. I stopped short of a fourth patch to convert more cases of:

  static struct string_list foo;
to:
  static struct string_list foo = STRING_LIST_INIT_NODUP;

The two are equivalent (mostly due to historical reasons). I tend to think explicit is better than implicit for something like this (not because BSS auto-initialization isn't OK, but because there is an explicit choice of dup/nodup that the writer made, and it is good to communicate that). But maybe people don't want the extra noise.

-Peff
Previous: Duy NguyenNext: Jeff King
Message 4 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.