fsck: detect null sha1 in tree entries

Short of somebody happening to beat the 1 in 2^160 odds of actually generating content that hashes to the null sha1, we should never see this value in a tree entry. So let's have fsck warn if it it seen. As in the previous commit, we test both blob and submodule entries to future-proof the test suite against the implementation depending on connectivity to notice the error. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 28, 2012 at 11:06 UTC c479d14a80743b1cb86d77695607f4c81f7d8797
2 files changed +33 -1
fsck.c
+7 -1
@@ -139,6 +139,7 @@ static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, con
139 static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
140 {
141 int retval;
142 + int has_null_sha1 = 0;
143 int has_full_path = 0;
144 int has_empty_name = 0;
145 int has_zero_pad = 0;
@@ -157,9 +158,12 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
158 while (desc.size) {
159 unsigned mode;
160 const char *name;
161 + const unsigned char *sha1;
162
161 - tree_entry_extract(&desc, &name, &mode);
163 + sha1 = tree_entry_extract(&desc, &name, &mode);
164
165 + if (is_null_sha1(sha1))
166 + has_null_sha1 = 1;
167 if (strchr(name, '/'))
168 has_full_path = 1;
169 if (!*name)
@@ -207,6 +211,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
211 }
212
213 retval = 0;
214 + if (has_null_sha1)
215 + retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
216 if (has_full_path)
217 retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
218 if (has_empty_name)
t/t1450-fsck.sh
+26
@@ -217,4 +217,30 @@ test_expect_success 'rev-list --verify-objects with bad sha1' '
217 grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
218 '
219
220 +_bz='\0'
221 +_bz5="$_bz$_bz$_bz$_bz$_bz"
222 +_bz20="$_bz5$_bz5$_bz5$_bz5"
223 +
224 +test_expect_success 'fsck notices blob entry pointing to null sha1' '
225 + (git init null-blob &&
226 + cd null-blob &&
227 + sha=$(printf "100644 file$_bz$_bz20" |
228 + git hash-object -w --stdin -t tree) &&
229 + git fsck 2>out &&
230 + cat out &&
231 + grep "warning.*null sha1" out
232 + )
233 +'
234 +
235 +test_expect_success 'fsck notices submodule entry pointing to null sha1' '
236 + (git init null-commit &&
237 + cd null-commit &&
238 + sha=$(printf "160000 submodule$_bz$_bz20" |
239 + git hash-object -w --stdin -t tree) &&
240 + git fsck 2>out &&
241 + cat out &&
242 + grep "warning.*null sha1" out
243 + )
244 +'
245 +
246 test_done