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

[PATCH] read-cache: make `do_read_index()` always set up `istate->repo`

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Jul 22, 2022, 21:22 UTC
Message-ID
<20220722212232.833188-1-martin.agren@gmail.com>
In-Reply-To
<xmqq35etc4vm.fsf@gitster.g>
On Fri, 22 Jul 2022 at 21:41, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
>
> Martin Ågren <martin.agren@gmail.com> writes:
>
> > I can't help but think that e6a653554b was just unlucky enough to
> > dereference `istate->repo` and that the real issue is that we're missing
> >
> >       if (!istate->repo)
> >               istate->repo = the_repository;
> >
> > in some strategic place a fair bit earlier. It seems to me like the diff
>
> Perhaps backtrace to see where the istate came from would quickly
> reveal where the real issue lies?

Here's an attempt at finding a proper spot for such a check. We end up with a small amount of duplicated code, but I think it should be ok, especially for a bugfix.

This is on top of tk/untracked-cache-with-uall and conflicts with 491df5f679 ("read-cache: set sparsity when index is new", 2022-05-10). The conflict could be resolved by taking my hunk before Victoria's [after which her helper function could be simplified accordingly] -- at least that passes all the tests.

I'm a bit out of my depth here. Hopefully one of the area experts can say more about this approach.

Martin
-- >8 --

If there is no index file, e.g., because the repository has just been created, we return zero early (unless `must_exist` makes us die instead.)

This early return means we do not set up `istate->repo`. With `core.untrackedCache=true`, the recent e6a653554b ("untracked-cache: support '--untracked-files=all' if configured", 2022-03-31) will eventually pass down `istate->repo` as a null pointer to `repo_config_get_string()`, causing a segmentation fault.

If we do hit this early return, set up `istate->repo` similar to when we actually read the index.

Reported-by: Joey Hess <id@joeyh.name>
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 read-cache.c                      | 5 ++++-
 t/t7063-status-untracked-cache.sh | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/read-cache.c b/read-cache.c
index 3e0e7d4183..68ed65035b 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2268,8 +2268,11 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
 	istate->timestamp.nsec = 0;
 	fd = open(path, O_RDONLY);
 	if (fd < 0) {
-		if (!must_exist && errno == ENOENT)
+		if (!must_exist && errno == ENOENT) {
+			if (!istate->repo)
+				istate->repo = the_repository;
 			return 0;
+		}
 		die_errno(_("%s: index file open failed"), path);
 	}
 
diff --git a/t/t7063-status-untracked-cache.sh b/t/t7063-status-untracked-cache.sh
index 9936cc329e..7dc5631f3d 100755
--- a/t/t7063-status-untracked-cache.sh
+++ b/t/t7063-status-untracked-cache.sh
@@ -985,4 +985,10 @@ test_expect_success '"status" after file replacement should be clean with UC=fal
 	status_is_clean
 '
 
+test_expect_success 'empty repo (no index) and core.untrackedCache' '
+	git init emptyrepo &&
+	cd emptyrepo/ &&
+	git -c core.untrackedCache=true write-tree
+'
+
 test_done
-- 
2.37.1.455.g008518b4e5
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 6 in “git write-tree segfault with core.untrackedCache true and nonexistent index”
  1. Joey HessJul 22, 2022
  2. Martin ÅgrenJul 22, 2022
  3. Junio C HamanoJul 22, 2022
  4. read-cache: make `do_read_index()` always set up `istate->repo`Martin Ågren, Jul 22, 2022
  5. Junio C HamanoJul 22, 2022
  6. Martin ÅgrenJul 22, 2022

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.