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

Re: [PATCH v3 1/5] refs: add struct repository parameter to branchname helpers

From
Burak Kaan Karaçay <bkkaracay@gmail.com>
Date
Apr 2, 2026, 17:03 UTC
Message-ID
<ac6K5UnVdw67Rfpy@gmail.com>
In-Reply-To
<ac4aZRveWXjOtxgB@pks.im>
Hi,
On Thu, Apr 02, 2026 at 09:27:33AM +0200, Patrick Steinhardt wrote:
Show 30 quoted lines
>On Sun, Mar 29, 2026 at 03:46:39PM +0530, Shreyansh Paliwal wrote:
>> diff --git a/refs.c b/refs.c
>> index 685a0c247b..5cdc8858c5 100644
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -758,10 +758,10 @@ void copy_branchname(struct strbuf *sb, const char *name,
>>  	strbuf_add(sb, name + used, len - used);
>>  }
>>
>> -int check_branch_ref(struct strbuf *sb, const char *name)
>> +int check_branch_ref(struct repository *repo, struct strbuf *sb, const char *name)
>>  {
>>  	if (startup_info->have_repository)
>> -		copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
>> +		copy_branchname(repo, sb, name, INTERPRET_BRANCH_LOCAL);
>>  	else
>>  		strbuf_addstr(sb, name);
>>
>
>I have to agree with Tian's comment on v2, this part here looks wrong. I
>don't think we should depend on `startup_info` here, but we should
>exclusively rely on whether or not the caller has passed in a
>repository. And that will likely require a bit more scrutiny to figure
>out whether there are any callers that shouldn't pass in a repository
>because it's not initialized.
>
>Alternatively, we could go with Tian's suggestion of checking for `repo
>&& repo->gitdir`.
>
>Patrick

This approach actually leads to a bug and segfault in a specific edge case when running 'git check-ref-format'. The current tests don't cover this scenario, but they can be extended to catch it.

If GIT_DIR is set to a non-existent path, 'startup_info->have_repository' becomes '0' but 'repo->gitdir' still holds the invalid path. As a result, the code enters the first condition and crashes. The case can be tested with this command:

$ git --git-dir='non-existing' check-ref-format --branch @{-1}

Modifying the behavior of 'repo->gitdir' might solve the issue, but I belive that falls outside the scope of this patch. After a quick search, I found a prophecy from Peff about the 'startup_info->have_repository':

[1] https://lore.kernel.org/git/20190806124954.GA13649@sigill.intra.peff.net/

Thanks, Burak Kaan Karaçay

Previous: Patrick SteinhardtNext: Tian Yuchen
Message 28 of 48 in “refs: reduce reliance on the_repository global state”
  1. 0/5 refs: reduce reliance on the_repository global stateShreyansh Paliwal, Mar 25, 2026
  2. 1/5 refs: make branchname helpers repository awareShreyansh Paliwal, Mar 25, 2026
  3. Patrick SteinhardtMar 27, 2026
  4. Shreyansh PaliwalMar 28, 2026
  5. 2/5 refs: make get_files_ref_lock_timeout_ms() repostory awareShreyansh Paliwal, Mar 25, 2026
  6. Patrick SteinhardtMar 27, 2026
  7. Burak Kaan KaraçayMar 27, 2026
  8. Shreyansh PaliwalMar 28, 2026
  9. 3/5 refs: remove the_hash_algo global stateShreyansh Paliwal, Mar 25, 2026
  10. 4/5 refs/reftable-backend: drop uses of the_repositoryShreyansh Paliwal, Mar 25, 2026
  11. Patrick SteinhardtMar 27, 2026
  12. 5/5 refs/packed-backend: use ref_store->repo instead of the_repositoryShreyansh Paliwal, Mar 25, 2026
  13. 0/5 refs: reduce reliance on the_repository global stateShreyansh Paliwal, Mar 28, 2026
  14. 1/5 refs: make branchname helpers repository awareShreyansh Paliwal, Mar 28, 2026
  15. Tian YuchenMar 28, 2026
  16. Shreyansh PaliwalMar 29, 2026
  17. Tian YuchenMar 29, 2026
  18. 2/5 refs: make get_files_ref_lock_timeout_ms() repostory awareShreyansh Paliwal, Mar 28, 2026
  19. 3/5 refs: remove the_hash_algo global stateShreyansh Paliwal, Mar 28, 2026
  20. Tian YuchenMar 28, 2026
  21. 4/5 refs/reftable-backend: drop uses of the_repositoryShreyansh Paliwal, Mar 28, 2026
  22. 5/5 refs/packed-backend: use ref_store->repo instead of the_repositoryShreyansh Paliwal, Mar 28, 2026
  23. Tian YuchenMar 28, 2026
  24. Shreyansh PaliwalMar 29, 2026
  25. 0/5 replace the_repository with local repository instancesShreyansh Paliwal, Mar 29, 2026
  26. 1/5 refs: add struct repository parameter to branchname helpersShreyansh Paliwal, Mar 29, 2026
  27. Patrick SteinhardtApr 2, 2026
  28. Burak Kaan KaraçayApr 2, 2026
  29. Tian YuchenApr 2, 2026
  30. Patrick SteinhardtApr 2, 2026
  31. Shreyansh PaliwalApr 3, 2026
  32. 2/5 refs: add struct repository parameter in get_files_ref_lock_timeout_ms()Shreyansh Paliwal, Mar 29, 2026
  33. 3/5 refs: remove the_hash_algo global stateShreyansh Paliwal, Mar 29, 2026
  34. 4/5 refs/reftable-backend: drop uses of the_repositoryShreyansh Paliwal, Mar 29, 2026
  35. Patrick SteinhardtApr 2, 2026
  36. Shreyansh PaliwalApr 3, 2026
  37. 5/5 refs/packed-backend: use ref_store->repo instead of the_repositoryShreyansh Paliwal, Mar 29, 2026
  38. 0/3 refs: reduce reliance on global stateShreyansh Paliwal, Apr 3, 2026
  39. 1/3 refs: add struct repository parameter in get_files_ref_lock_timeout_ms()Shreyansh Paliwal, Apr 3, 2026
  40. Tian YuchenApr 3, 2026
  41. 2/3 refs: remove the_hash_algo global stateShreyansh Paliwal, Apr 3, 2026
  42. 3/3 refs/reftable-backend: drop uses of the_repositoryShreyansh Paliwal, Apr 3, 2026
  43. 0/3 refs: reduce reliance on global stateShreyansh Paliwal, Apr 4, 2026
  44. 1/3 refs: add struct repository parameter in get_files_ref_lock_timeout_ms()Shreyansh Paliwal, Apr 4, 2026
  45. 2/3 refs: remove the_hash_algo global stateShreyansh Paliwal, Apr 4, 2026
  46. 3/3 refs/reftable-backend: drop uses of the_repositoryShreyansh Paliwal, Apr 4, 2026
  47. Patrick SteinhardtApr 8, 2026
  48. Junio C HamanoApr 8, 2026

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.