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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jan 13, 2017 at 13:00 UTC
cce044df7f2392d0c6cb21d6dca94f01ff838727
2 files changed
+27
sha1_file.c
+5
@@ -3847,6 +3847,11 @@ static int check_stream_sha1(git_zstream *stream,
3847
error("corrupt loose object '%s'", sha1_to_hex(expected_sha1));
3848
return -1;
3849
}
3850
+ if (stream->avail_in) {
3851
+ error("garbage at end of loose object '%s'",
3852
+ sha1_to_hex(expected_sha1));
3853
+ return -1;
3854
+ }
3855
3856
git_SHA1_Final(real_sha1, &c);
3857
if (hashcmp(expected_sha1, real_sha1)) {
t/t1450-fsck.sh
+22
@@ -597,4 +597,26 @@ test_expect_success 'fsck finds problems in duplicate loose objects' '
597
)
598
'
599
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
+
622
test_done