summaryrefslogtreecommitdiff
path: root/sha1-name.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2019-05-14 13:54:55 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-05-15 05:08:42 (GMT)
commit581d2fd9f2d66ec5cb1859e0b6aef7c459a0d3a9 (patch)
tree3bd873444b90a8df6ea47f3a5591ec06efb9336f /sha1-name.c
parentab15ad1a3b4b04a29415aef8c9afa2f64fc194a2 (diff)
downloadgit-581d2fd9f2d66ec5cb1859e0b6aef7c459a0d3a9.zip
git-581d2fd9f2d66ec5cb1859e0b6aef7c459a0d3a9.tar.gz
git-581d2fd9f2d66ec5cb1859e0b6aef7c459a0d3a9.tar.bz2
get_oid: handle NULL repo->index
When get_oid() and its helpers see an index name like ":.gitmodules", they try to load the index on demand, like: if (repo->index->cache) repo_read_index(repo); However, that misses the case when "repo->index" itself is NULL; we'll segfault in the conditional. This never happens with the_repository; there we always point its index field to &the_index. But a submodule repository may have a NULL index field until somebody calls repo_read_index(). This bug is triggered by t7411, but it was hard to notice because it's in an expect_failure block. That test was added by 2b1257e463 (t/helper: add test-submodule-nested-repo-config, 2018-10-25). Back then we had no easy way to access the .gitmodules blob of a submodule repo, so we expected (and got) an error message to that effect. Later, d9b8b8f896 (submodule-config.c: use repo_get_oid for reading .gitmodules, 2019-04-16) started looking in the correct repo, which is when we started triggering the segfault. With this fix, the test starts passing (once we clean it up as its comment instructs). Note that as far as I know, this bug could not be triggered outside of the test suite. It requires resolving an index name in a submodule, and all of the code paths (aside from test-tool) which do that either load the index themselves, or always pass the_repository. Ultimately it comes from 3a7a698e93 (sha1-name.c: remove implicit dependency on the_index, 2019-01-12), which replaced a check of "the_index.cache" with "repo->index->cache". So even if there is another way to trigger it, it wouldn't affect any versions before then. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1-name.c')
-rw-r--r--sha1-name.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/sha1-name.c b/sha1-name.c
index 775a73d..455e9fb 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1837,7 +1837,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
if (flags & GET_OID_RECORD_PATH)
oc->path = xstrdup(cp);
- if (!repo->index->cache)
+ if (!repo->index || !repo->index->cache)
repo_read_index(repo);
pos = index_name_pos(repo->index, cp, namelen);
if (pos < 0)