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

Re: [PATCH v2 01/17] refs/files: simplify iterating through root refs

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jan 12, 2026, 09:56 UTC
Message-ID
<CAOLa=ZSu3MGejqN9n4NaytANNbU4bY1GEAK84-W0B94jcCddpA@mail.gmail.com>
In-Reply-To
<20260112-pks-refs-verify-fixes-v2-1-2e9e453bd6c3@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 45 quoted lines
> When iterating through root refs we first need to determine the
> directory in which the refs live. This is done by retrieving the root of
> the loose refs via `refs->loose->root->name`, and putting it through
> `files_ref_path()` to derive the final path.
>
> This is somewhat redundant though: the root name of the loose files
> cache is always going to be the empty string. As such, we always end up
> passing that empty string to `files_ref_path()` as the ref hierarchy we
> want to start. And this actually makes sense: `files_ref_path()` already
> computes the location of the root directory, so of course we need to
> pass the empty string for the ref hierarchy itself. So going via the
> loose ref cache to figure out that the root of a ref hierarchy is empty
> is only causing confusion.
>
> But next to the added confusion, it can also lead to a segfault. The
> loose ref cache is populated lazily, so it may not always be set. It
> seems to be sheer luck that this is a condition we do not currently hit.
> The right thing to do would be to call `get_loose_ref_cache()`, which
> knows to populate the cache if required.
>
> Simplify the code and fix the potential segfault by simply removing the
> indirection via the loose ref cache completely.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  refs/files-backend.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index 6f6f76a8d8..297739f203 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -354,13 +354,11 @@ static int for_each_root_ref(struct files_ref_store *refs,
>  			     void *cb_data)
>  {
>  	struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
> -	const char *dirname = refs->loose->root->name;
>  	struct dirent *de;
> -	size_t dirnamelen;
>  	int ret;
>  	DIR *d;
>
> -	files_ref_path(refs, &path, dirname);
> +	files_ref_path(refs, &path, "");
>

Since refs->loose->root->name is always `""`, we directly pass that instead. Makes sense.

Show 9 quoted lines
>  	d = opendir(path.buf);
>  	if (!d) {
> @@ -368,9 +366,6 @@ static int for_each_root_ref(struct files_ref_store *refs,
>  		return -1;
>  	}
>
> -	strbuf_addstr(&refname, dirname);
> -	dirnamelen = refname.len;
> -
This too is unnecessary since the len here is 0.
Show 18 quoted lines
>  	while ((de = readdir(d)) != NULL) {
>  		unsigned char dtype;
>
> @@ -378,6 +373,8 @@ static int for_each_root_ref(struct files_ref_store *refs,
>  			continue;
>  		if (ends_with(de->d_name, ".lock"))
>  			continue;
> +
> +		strbuf_reset(&refname);
>  		strbuf_addstr(&refname, de->d_name);
>
>  		dtype = get_dtype(de, &path, 1);
> @@ -386,8 +383,6 @@ static int for_each_root_ref(struct files_ref_store *refs,
>  			if (ret)
>  				goto done;
>  		}
> -
> -		strbuf_setlen(&refname, dirnamelen);

Earlier we were setting the length to 0, but thats the same as strbuf_reset(), so we do that now. This gets rid of the `dirnamelen` varible. Looks good.

Show 6 quoted lines
>  	}
>
>  	ret = 0;
>
> --
> 2.52.0.590.g1f87b77810.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 34 of 61 in “Fixes and improvements for ref consistency checks”
  1. 00/17 Fixes and improvements for ref consistency checksPatrick Steinhardt, Jan 9, 2026
  2. 01/17 refs/files: simplify iterating through root refsPatrick Steinhardt, Jan 9, 2026
  3. shejialuoJan 10, 2026
  4. 02/17 refs/files: move fsck functions into global scopePatrick Steinhardt, Jan 9, 2026
  5. 03/17 refs/files: remove `refs_check_dir` parameterPatrick Steinhardt, Jan 9, 2026
  6. 04/17 refs/files: remove useless indirectionPatrick Steinhardt, Jan 9, 2026
  7. 05/17 refs/files: extract function to check single refPatrick Steinhardt, Jan 9, 2026
  8. 06/17 refs/files: improve error handling when verifying symrefsPatrick Steinhardt, Jan 9, 2026
  9. shejialuoJan 10, 2026
  10. 07/17 refs/files: perform consistency checks for root refsPatrick Steinhardt, Jan 9, 2026
  11. shejialuoJan 10, 2026
  12. Patrick SteinhardtJan 12, 2026
  13. 08/17 fsck: drop unused fields from `struct fsck_ref_report`Patrick Steinhardt, Jan 9, 2026
  14. 09/17 refs/files: extract generic symref target checksPatrick Steinhardt, Jan 9, 2026
  15. shejialuoJan 10, 2026
  16. Patrick SteinhardtJan 12, 2026
  17. 10/17 refs/files: introduce function to perform normal ref checksPatrick Steinhardt, Jan 9, 2026
  18. shejialuoJan 10, 2026
  19. Patrick SteinhardtJan 12, 2026
  20. 11/17 refs/reftable: adapt includes to become consistentPatrick Steinhardt, Jan 9, 2026
  21. 12/17 refs/reftable: extract function to retrieve backend for worktreePatrick Steinhardt, Jan 9, 2026
  22. 13/17 refs/reftable: fix consistency checks with worktreesPatrick Steinhardt, Jan 9, 2026
  23. 14/17 refs/reftable: introduce generic checks for refsPatrick Steinhardt, Jan 9, 2026
  24. 15/17 builtin/fsck: move generic object ID checks into `refs_fsck()`Patrick Steinhardt, Jan 9, 2026
  25. 16/17 builtin/fsck: move generic HEAD check into `refs_fsck()`Patrick Steinhardt, Jan 9, 2026
  26. shejialuoJan 10, 2026
  27. Patrick SteinhardtJan 12, 2026
  28. shejialuoJan 15, 2026
  29. 17/17 builtin/fsck: drop `fsck_head_link()`Patrick Steinhardt, Jan 9, 2026
  30. shejialuoJan 10, 2026
  31. Patrick SteinhardtJan 12, 2026
  32. 00/17 Fixes and improvements for ref consistency checksPatrick Steinhardt, Jan 12, 2026
  33. 01/17 refs/files: simplify iterating through root refsPatrick Steinhardt, Jan 12, 2026
  34. Karthik NayakJan 12, 2026
  35. 02/17 refs/files: move fsck functions into global scopePatrick Steinhardt, Jan 12, 2026
  36. 03/17 refs/files: remove `refs_check_dir` parameterPatrick Steinhardt, Jan 12, 2026
  37. 04/17 refs/files: remove useless indirectionPatrick Steinhardt, Jan 12, 2026
  38. Karthik NayakJan 12, 2026
  39. 05/17 refs/files: extract function to check single refPatrick Steinhardt, Jan 12, 2026
  40. 06/17 refs/files: improve error handling when verifying symrefsPatrick Steinhardt, Jan 12, 2026
  41. 07/17 refs/files: perform consistency checks for root refsPatrick Steinhardt, Jan 12, 2026
  42. 08/17 fsck: drop unused fields from `struct fsck_ref_report`Patrick Steinhardt, Jan 12, 2026
  43. 09/17 refs/files: extract generic symref target checksPatrick Steinhardt, Jan 12, 2026
  44. 10/17 refs/files: introduce function to perform normal ref checksPatrick Steinhardt, Jan 12, 2026
  45. Karthik NayakJan 12, 2026
  46. Patrick SteinhardtJan 12, 2026
  47. Junio C HamanoJan 12, 2026
  48. Junio C HamanoJan 12, 2026
  49. Patrick SteinhardtJan 12, 2026
  50. 11/17 refs/reftable: adapt includes to become consistentPatrick Steinhardt, Jan 12, 2026
  51. 12/17 refs/reftable: extract function to retrieve backend for worktreePatrick Steinhardt, Jan 12, 2026
  52. 13/17 refs/reftable: fix consistency checks with worktreesPatrick Steinhardt, Jan 12, 2026
  53. Karthik NayakJan 12, 2026
  54. 14/17 refs/reftable: introduce generic checks for refsPatrick Steinhardt, Jan 12, 2026
  55. 15/17 builtin/fsck: move generic object ID checks into `refs_fsck()`Patrick Steinhardt, Jan 12, 2026
  56. 16/17 builtin/fsck: move generic HEAD check into `refs_fsck()`Patrick Steinhardt, Jan 12, 2026
  57. 17/17 builtin/fsck: drop `fsck_head_link()`Patrick Steinhardt, Jan 12, 2026
  58. Karthik NayakJan 12, 2026
  59. Patrick SteinhardtJan 12, 2026
  60. shejialuoJan 15, 2026
  61. Patrick SteinhardtJan 16, 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.