]> git.ipfire.org Git - thirdparty/git.git/commitdiff
read-cache: make `do_read_index()` always set up `istate->repo`
authorMartin Ågren <martin.agren@gmail.com>
Fri, 22 Jul 2022 21:22:32 +0000 (23:22 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 Jul 2022 21:51:00 +0000 (14:51 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
read-cache.c
t/t7063-status-untracked-cache.sh

index 3e0e7d418375ebbb916ab66735b5345342e8402b..68ed65035bbe5757a6714a4afb388609cb175cf8 100644 (file)
@@ -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);
        }
 
index 9936cc329ecd3f3d6cf96994de0209efd945dd55..c1f0d950363d311a4b8da3b6fde00b69cb2bdd74 100755 (executable)
@@ -985,4 +985,9 @@ 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 &&
+       git -C emptyrepo -c core.untrackedCache=true write-tree
+'
+
 test_done