fsck: avoid looking at NULL blob->object

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test. Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously pass a NULL object to report(), which gets dereferenced and causes a segfault. It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type). So instead, let's just use lookup_unknown_object() to get the real "struct object", and pass that. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 11, 2018 at 04:35 UTC 47cc91310a81ca606e6bebcccf168bc1cb297d8e
2 files changed +20 -1
fsck.c
+2 -1
@@ -1032,7 +1032,8 @@ int fsck_finish(struct fsck_options *options)
1032
1033 blob = lookup_blob(oid);
1034 if (!blob) {
1035 - ret |= report(options, &blob->object,
1035 + struct object *obj = lookup_unknown_object(oid->hash);
1036 + ret |= report(options, obj,
1037 FSCK_MSG_GITMODULES_BLOB,
1038 "non-blob found at .gitmodules");
1039 continue;
t/t7415-submodule-names.sh
+18
@@ -148,4 +148,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
148 )
149 '
150
151 +test_expect_success 'fsck detects non-blob .gitmodules' '
152 + git init non-blob &&
153 + (
154 + cd non-blob &&
155 +
156 + # As above, make the funny tree directly to avoid index
157 + # restrictions.
158 + mkdir subdir &&
159 + cp ../.gitmodules subdir/file &&
160 + git add subdir/file &&
161 + git commit -m ok &&
162 + git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree &&
163 +
164 + test_must_fail git fsck 2>output &&
165 + grep gitmodulesBlob output
166 + )
167 +'
168 +
169 test_done