Skip to content

Commit cce044d

Browse files
peffgitster
authored andcommitted
fsck: detect trailing garbage in all object types
When a loose tree or commit is read by fsck (or any git program), unpack_sha1_rest() checks whether there is extra cruft at the end of the object file, after the zlib data. Blobs that are streamed, however, do not have this check. For normal git operations, it's not a big deal. We know the sha1 and size checked out, so we have the object bytes we wanted. The trailing garbage doesn't affect what we're trying to do. But since the point of fsck is to find corruption or other problems, it should be more thorough. This patch teaches its loose-sha1 reader to detect extra bytes after the zlib stream and complain. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c68b489 commit cce044d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

sha1_file.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,11 @@ static int check_stream_sha1(git_zstream *stream,
38473847
error("corrupt loose object '%s'", sha1_to_hex(expected_sha1));
38483848
return -1;
38493849
}
3850+
if (stream->avail_in) {
3851+
error("garbage at end of loose object '%s'",
3852+
sha1_to_hex(expected_sha1));
3853+
return -1;
3854+
}
38503855

38513856
git_SHA1_Final(real_sha1, &c);
38523857
if (hashcmp(expected_sha1, real_sha1)) {

t/t1450-fsck.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,26 @@ test_expect_success 'fsck finds problems in duplicate loose objects' '
597597
)
598598
'
599599

600+
test_expect_success 'fsck detects trailing loose garbage (commit)' '
601+
git cat-file commit HEAD >basis &&
602+
echo bump-commit-sha1 >>basis &&
603+
commit=$(git hash-object -w -t commit basis) &&
604+
file=$(sha1_file $commit) &&
605+
test_when_finished "remove_object $commit" &&
606+
chmod +w "$file" &&
607+
echo garbage >>"$file" &&
608+
test_must_fail git fsck 2>out &&
609+
test_i18ngrep "garbage.*$commit" out
610+
'
611+
612+
test_expect_success 'fsck detects trailing loose garbage (blob)' '
613+
blob=$(echo trailing | git hash-object -w --stdin) &&
614+
file=$(sha1_file $blob) &&
615+
test_when_finished "remove_object $blob" &&
616+
chmod +w "$file" &&
617+
echo garbage >>"$file" &&
618+
test_must_fail git fsck 2>out &&
619+
test_i18ngrep "garbage.*$blob" out
620+
'
621+
600622
test_done

0 commit comments

Comments
 (0)