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

Re: [PATCH 0/17] removing questionable uses of git_path

From
Jeff King <peff@peff.net>
Date
Aug 10, 2015, 17:47 UTC
Message-ID
<20150810174743.GB20546@sigill.intra.peff.net>
In-Reply-To
<xmqqr3nbuk23.fsf@gitster.dls.corp.google.com>
On Mon, Aug 10, 2015 at 10:31:32AM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > The problem is that git_path uses a static buffer that gets overwritten
> > by subsequent calls.
> 
> As the rotating static buffer pattern used in get_pathname() was
> modeled after sha1_to_hex(), we have the same issue there.  They are
> troubles waiting to happen unless the callers are careful.

Yeah, I think Michael mentioned that to me off-list. I don't _think_ sha1_to_hex is nearly such a problem, because we tend to store sha1s in their binary form. So sha1_to_hex is almost always an argument to fprintf() or similar.

Of course there are some exceptions. :) I notice we pass one return value to add_pending_object, which was almost certainly horribly broken before 31faeb2 started strdup'ing object_array names.

So certainly it would be nice to audit them all, but there are over 600 calls. Given the likelihood of finding a useful bug, I'm not sure it's the greatest use of developer time.

Show 7 quoted lines
> > producing a fairly tame-looking set of function calls. It's OK to pass
> > the result of git_path() to a system call, or something that is a thin
> > wrapper around one (e.g., strbuf_read_file).
> 
> That is a short and good rule to follow, but the problem is that not
> everybody has good taste to interpret the above rule and apply it with
> an eye toward maintainability X-<.

Yeah. Part of me wants to eradicate git_path entirely. My series takes out over half of the calls, but there are still close to 100, I think. I think it would make the code worse to convert all of them naively. We could provide format-aware wrappers for some filesystem functions, like:

  git_stat(&st, "%s", refname);
or something. That feels horribly coupled compared to:
  stat(git_path("%s", refname), &st);
but it makes it very clear what the memory ownership/lifetime rules are.

Anyway, part of my goal with the series was not just to clean up the existing suspicious spots, but to raise awareness of the issue. At least for me, I know it's something I will look for more carefully when reviewing patches. Once bitten, twice shy. :)

-Peff
Previous: Junio C HamanoNext: Duy Nguyen
Message 33 of 34 in “removing questionable uses of git_path”
  1. 0/17 removing questionable uses of git_pathJeff King, Aug 10, 2015
  2. 01/17 cache.h: clarify documentation for git_path, et alJeff King, Aug 10, 2015
  3. 02/17 cache.h: complete set of git_path_submodule helpersJeff King, Aug 10, 2015
  4. 03/17 t5700: modernize styleJeff King, Aug 10, 2015
  5. 04/17 add_to_alternates_file: don't add duplicate entriesJeff King, Aug 10, 2015
  6. Michael HaggertyAug 11, 2015
  7. Jeff KingAug 11, 2015
  8. 05/17 remove hold_lock_file_for_appendJeff King, Aug 10, 2015
  9. Junio C HamanoAug 10, 2015
  10. Jeff KingAug 11, 2015
  11. 06/17 prefer git_pathdup to git_path in some possibly-dangerous casesJeff King, Aug 10, 2015
  12. 07/17 prefer mkpathdup to mkpath in assignmentsJeff King, Aug 10, 2015
  13. 08/17 remote.c: drop extraneous local variable from migrate_fileJeff King, Aug 10, 2015
  14. 09/17 refs.c: remove extra git_path calls from read_loose_refsJeff King, Aug 10, 2015
  15. 10/17 path.c: drop git_path_submoduleJeff King, Aug 10, 2015
  16. Junio C HamanoAug 10, 2015
  17. Junio C HamanoAug 10, 2015
  18. Junio C HamanoAug 10, 2015
  19. Jeff KingAug 11, 2015
  20. 11/17 refs.c: simplify strbufs in reflog setup and writingJeff King, Aug 10, 2015
  21. Michael HaggertyAug 10, 2015
  22. Jeff KingAug 10, 2015
  23. 12/17 refs.c: avoid repeated git_path calls in rename_tmp_logJeff King, Aug 10, 2015
  24. 13/17 refs.c: avoid git_path assignment in lock_ref_sha1_basicJeff King, Aug 10, 2015
  25. 14/17 refs.c: remove_empty_directories can take a strbufJeff King, Aug 10, 2015
  26. 15/17 find_hook: keep our own static bufferJeff King, Aug 10, 2015
  27. 16/17 get_repo_path: refactor path-allocationJeff King, Aug 10, 2015
  28. 17/17 memoize common git-path "constant" filesJeff King, Aug 10, 2015
  29. Michael HaggertyAug 10, 2015
  30. Jeff KingAug 10, 2015
  31. Michael HaggertyAug 10, 2015
  32. Junio C HamanoAug 10, 2015
  33. Jeff KingAug 10, 2015
  34. Duy NguyenAug 15, 2015

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.