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

Re: [PATCH v3 4/4] stash: implement builtin stash

From
Jeff King <peff@peff.net>
Date
Jun 1, 2017, 04:07 UTC
Message-ID
<20170601040717.63ajkcgr7ihhvgmz@sigill.intra.peff.net>
In-Reply-To
<CA+CzEk9uV5Oqa=NMhd1bjnXP-vjUqVUMH9KSzW6ehi67hqU2+Q@mail.gmail.com>
On Wed, May 31, 2017 at 08:29:43PM -0700, Joel Teichroeb wrote:
> I'm running into a lot of trouble using argv_array_clear. It seems
> that some of the builtin git cmd functions move the parameters around,
> and write new pointers to argv. There's three options I have now, and
> I'm not sure which is the best one.

Hrm. It's normal for parsing to reorder the parameters (e.g., shifting non-options to the front), but that should still allow a clear at the end. New pointers would definitely cause a problem, though. I don't know of any cases where we do that, but on the other hand I wouldn't be too surprised to find that the revision.c options parser does some nasty tricks.

Do you have a specific example? I'd be curious to see if we can just fix the parser to be less surprising (i.e., your (1) below).

> 1. Fix all the builtin cmd functions that I use to not mess around with argv
If it's just one or two spots, this might be viable.
> 2. Stop using the builtin cmd functions, and use child processes exclusively

That might not be the worst thing in the world for a first cut at a shell to C transition, because it eliminates a whole class of possible problems. But it really just side-steps the problem, as we'd want to eventually deal with it and reduce the process count.

> 3. Don't worry about clearing the memory used for these function calls.

That might be do-able, as long as the leaks are O(1) for a program run (and not say, a leak per commit). At the very least we should mark those spots with a "NEEDSWORK" comment and an explanation of the issue so that your work in finding them isn't wasted.

> It looks like the rest of the code generally does #3.

It looks like we don't actually pass argv arrays to setup_revisions() all that often. The three I see are:

  - bisect_rev_setup(), which is a known leak. This is trickier, though,
    because we actually pass the initialized rev_info out of the
    function, and the memory needs to last until we're done with the
    traversal
  - http-push, which does seem to free the memory
  - stat_tracking_info(), which does seem to free

I could well believe there are places where we leak, though, especially for top-level functions that exit the program when they're done.

A fourth option is to massage the argv array into something that can be massaged by the callee, and retain the original array for freeing. I.e., something like:

  struct argv_array argv = ARGV_ARRAY_INIT;
  const char **massaged;
  argv_array_pushl(&argv, ...whatever...);
  ALLOC_ARRAY(massaged, argc);
  COPY_ARRAY(massaged, argv, argc);
  setup_revisions(argv.argc, massaged, &revs, NULL);
  /*
   * No clue what's in "massaged" now, as setup_revisions() may have
   * reordered things, added new elements, deleted some, etc. But we
   * don't have to care because any pointers we need to free are still
   * in the original argv struct, and we should be safe to free the
   * massaged array itself.
   */
  free(massaged);
  argv_array_clear(&argv);

That's pretty horrible, though. If setup_revisions() is requiring us to do that, I'd really prefer to look into fixing it.

-Peff
Previous: Joel TeichroebNext: Junio C Hamano
Message 21 of 25 in “Implement git stash as a builtin command”
  1. 0/4 Implement git stash as a builtin commandJoel Teichroeb, May 28, 2017
  2. 1/4 stash: add test for stash create with no filesJoel Teichroeb, May 28, 2017
  3. Ævar Arnfjörð BjarmasonMay 28, 2017
  4. Junio C HamanoMay 29, 2017
  5. 2/4 stash: add test for stashing in a detached stateJoel Teichroeb, May 28, 2017
  6. Ævar Arnfjörð BjarmasonMay 28, 2017
  7. Junio C HamanoMay 29, 2017
  8. 3/4 close the index lock when not writing the new indexJoel Teichroeb, May 28, 2017
  9. Ævar Arnfjörð BjarmasonMay 28, 2017
  10. Junio C HamanoMay 29, 2017
  11. 4/4 stash: implement builtin stashJoel Teichroeb, May 28, 2017
  12. Christian CouderMay 28, 2017
  13. Ævar Arnfjörð BjarmasonMay 28, 2017
  14. Joel TeichroebMay 28, 2017
  15. Jeff KingMay 28, 2017
  16. Ævar Arnfjörð BjarmasonMay 28, 2017
  17. Jeff KingMay 28, 2017
  18. Joel TeichroebMay 29, 2017
  19. Ævar Arnfjörð BjarmasonMay 29, 2017
  20. Joel TeichroebJun 1, 2017
  21. Jeff KingJun 1, 2017
  22. Junio C HamanoMay 29, 2017
  23. Ævar Arnfjörð BjarmasonMay 28, 2017
  24. Johannes SchindelinOct 23, 2017
  25. Joel TeichroebOct 23, 2017

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.