Skip to content

Commit c479d14

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4337b58 commit c479d14

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

fsck.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, con
139139
static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
140140
{
141141
int retval;
142+
int has_null_sha1 = 0;
142143
int has_full_path = 0;
143144
int has_empty_name = 0;
144145
int has_zero_pad = 0;
@@ -157,9 +158,12 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
157158
while (desc.size) {
158159
unsigned mode;
159160
const char *name;
161+
const unsigned char *sha1;
160162

161-
tree_entry_extract(&desc, &name, &mode);
163+
sha1 = tree_entry_extract(&desc, &name, &mode);
162164

165+
if (is_null_sha1(sha1))
166+
has_null_sha1 = 1;
163167
if (strchr(name, '/'))
164168
has_full_path = 1;
165169
if (!*name)
@@ -207,6 +211,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
207211
}
208212

209213
retval = 0;
214+
if (has_null_sha1)
215+
retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
210216
if (has_full_path)
211217
retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
212218
if (has_empty_name)

t/t1450-fsck.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,30 @@ test_expect_success 'rev-list --verify-objects with bad sha1' '
217217
grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
218218
'
219219

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+
220246
test_done

0 commit comments

Comments
 (0)