From: Jeff King <hidden> Date: 2017-01-16 21:22:41
I came across a repository today that was missing an object, and for
which "git fsck" reported the error but "git fsck --connectivity-check"
did not. It turns out that the shortcut taken by --connectivity-check
violates some assumptions made by the rest of fsck (namely, that every
object in the repo has a "struct object" loaded).
And fsck being a generally neglected tool, I couldn't help but find
several more bugs on the way. :)
[1/6]: t1450: clean up sub-objects in duplicate-entry test
[2/6]: fsck: report trees as dangling
[3/6]: fsck: prepare dummy objects for --connectivity-check
[4/6]: fsck: tighten error-checks of "git fsck <head>"
[5/6]: fsck: do not fallback "git fsck <bogus>" to "git fsck"
[6/6]: fsck: check HAS_OBJ more consistently
builtin/fsck.c | 131 ++++++++++++++++++++++++++++++++++++++++++++------------
t/t1450-fsck.sh | 70 ++++++++++++++++++++++++++++--
2 files changed, 171 insertions(+), 30 deletions(-)
-Peff
From: Jeff King <hidden> Date: 2017-01-16 21:24:10
This test creates a multi-level set of trees, but its
cleanup routine only removes the top-level tree. After the
test finishes, the inner tree and the blob it points to
remain, making the inner tree dangling.
A later test ("cleaned up") verifies that we've removed any
cruft and "git fsck" output is clean. This passes only
because of a bug in git-fsck which fails to notice dangling
trees.
In preparation for fixing the bug, let's teach this earlier
test to clean up after itself correctly. We have to remove
the inner tree (and therefore the blob, too, which becomes
dangling after removing that tree).
Since the setup code happens inside a subshell, we can't
just set a variable for each object. However, we can stuff
all of the sha1s into the $T output variable, which is not
used for anything except cleanup.
Signed-off-by: Jeff King <redacted>
---
t/t1450-fsck.sh | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
@@ -189,14 +189,16 @@ test_expect_success 'commit with NUL in header' '' test_expect_success'tree object with duplicate entries''-test_when_finished"remove_object \$T"&&+test_when_finished"for i in \$T; do remove_object \$i; done"&&T=$(GIT_INDEX_FILE=test-index&&exportGIT_INDEX_FILE&&rm-ftest-index&&>x&&gitaddx&&+gitrev-parse:x&&T=$(gitwrite-tree)&&+echo$T&&(gitcat-filetree$T&&gitcat-filetree$T
From: Jeff King <hidden> Date: 2017-01-16 21:25:42
After checking connectivity, fsck looks through the list of
any objects we've seen mentioned, and reports unreachable
and un-"used" ones as dangling. However, it skips any object
which is not marked as "parsed", as that is an object that
we _don't_ have (but that somebody mentioned).
Since 6e454b9a3 (clear parsed flag when we free tree
buffers, 2013-06-05), that flag can't be relied on, and the
correct method is to check the HAS_OBJ flag. The cleanup in
that commit missed this callsite, though. As a result, we
would generally fail to report dangling trees.
We never noticed because there were no tests in this area
(for trees or otherwise). Let's add some.
Signed-off-by: Jeff King <redacted>
---
builtin/fsck.c | 2 +-
t/t1450-fsck.sh | 27 +++++++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
@@ -559,4 +559,31 @@ test_expect_success 'fsck --name-objects' ')'+# for each of type, we have one version which is referenced by another object+# (and so while unreachable, not dangling), and another variant which really is+# dangling.+test_expect_success'fsck notices dangling objects''+gitinitdangling&&+(+cddangling&&+blob=$(echonot-dangling|githash-object-w--stdin)&&+dblob=$(echodangling|githash-object-w--stdin)&&+tree=$(printf"100644 blob %s\t%s\n"$blobone|gitmktree)&&+dtree=$(printf"100644 blob %s\t%s\n"$blobtwo|gitmktree)&&+commit=$(gitcommit-tree$tree)&&+dcommit=$(gitcommit-tree-p$commit$tree)&&++cat>expect<<-EOF&&+danglingblob$dblob+danglingcommit$dcommit+danglingtree$dtree+EOF++gitfsck>actual&&+# the output order is non-deterministic, as it comes from a hash+sort<actual>actual.sorted&&+test_cmpexpectactual.sorted+)+'+ test_done
From: Jeff King <hidden> Date: 2017-01-16 21:34:47
Since fsck tries to continue as much as it can after seeing
an error, we still do the reachability check even if some
heads we were given on the command-line are bogus. But if
_none_ of the heads is is valid, we fallback to checking all
refs and the index, which is not what the user asked for at
all.
Instead of checking "heads", the number of successful heads
we got, check "argc" (which we know only has non-options in
it, because parse_options removed the others).
Signed-off-by: Jeff King <redacted>
---
builtin/fsck.c | 2 +-
t/t1450-fsck.sh | 11 +++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
@@ -610,4 +610,15 @@ test_expect_success 'fsck $name notices bogus $name' 'test_must_failgitfsck$_z40'+test_expect_success'bogus head does not fallback to all heads''+# set up a case that will cause a reachability complaint+echoto-be-deleted>foo&&+gitaddfoo&&+blob=$(gitrev-parse:foo)&&+test_when_finished"git rm --cached foo"&&+remove_object$blob&&+test_must_failgitfsck$_z40>out2>&1&&+!grep$blobout+'+ test_done
From: Jeff King <hidden> Date: 2017-01-16 21:35:10
There are two spots that call lookup_object() and assume
that a non-NULL result means we have the object:
1. When we're checking the objects given to us by the user
on the command line.
2. When we're checking if a reflog entry is valid.
This generally follows fsck's mental model that we will have
looked at and loaded a "struct object" for each object in
the repository. But it misses one case: if another object
_mentioned_ an object, but we didn't actually parse it or
verify that it exists, it will still have a struct.
It's not clear if this is a triggerable bug or not.
Certainly the later parts of the reachability check need to
be careful of this, and do so by checking the HAS_OBJ flag.
But both of these steps happen before we start traversing,
so probably we won't have followed any links yet. Still,
it's easy enough to be defensive here.
Signed-off-by: Jeff King <redacted>
---
builtin/fsck.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Jeff King <hidden> Date: 2017-01-16 21:39:01
Normally fsck makes a pass over all objects to check their
integrity, and then follows up with a reachability check to
make sure we have all of the referenced objects (and to know
which ones are dangling). The latter checks for the HAS_OBJ
flag in obj->flags to see if we found the object in the
first pass.
Commit 02976bf85 (fsck: introduce `git fsck --connectivity-only`,
2015-06-22) taught fsck to skip the initial pass, and to
fallback to has_sha1_file() instead of the HAS_OBJ check.
However, it converted only one HAS_OBJ check to use
has_sha1_file(). But there are many other places in
builtin/fsck.c that assume that the flag is set (or that
lookup_object() will return an object at all). This leads to
several bugs with --connectivity-only:
1. mark_object() will not queue objects for examination,
so recursively following links from commits to trees,
etc, did nothing. I.e., we were checking the
reachability of hardly anything at all.
2. When a set of heads is given on the command-line, we
use lookup_object() to see if they exist. But without
the initial pass, we assume nothing exists.
3. When loading reflog entries, we do a similar
lookup_object() check, and complain that the reflog is
broken if the object doesn't exist in our hash.
So in short, --connectivity-only is broken pretty badly, and
will claim that your repository is fine when it's not.
Presumably nobody noticed for a few reasons.
One is that the embedded test does not actually test the
recursive nature of the reachability check. All of the
missing objects are still in the index, and we directly
check items from the index. This patch modifies the test to
delete the index, which shows off breakage (1).
Another is that --connectivity-only just skips the initial
pass for loose objects. So on a real repository, the packed
objects were still checked correctly. But on the flipside,
it means that "git fsck --connectivity-only" still checks
the sha1 of all of the packed objects, nullifying its
original purpose of being a faster git-fsck.
And of course the final problem is that the bug only shows
up when there _is_ corruption, which is rare. So anybody
running "git fsck --connectivity-only" proactively would
assume it was being thorough, when it was not.
One possibility for fixing this is to find all of the spots
that rely on HAS_OBJ and tweak them for the connectivity-only
case. But besides the risk that we might miss a spot (and I
found three already, corresponding to the three bugs above),
there are other parts of fsck that _can't_ work without a
full list of objects. E.g., the list of dangling objects.
Instead, let's make the connectivity-only case look more
like the normal case. Rather than skip the initial pass
completely, we'll do an abbreviated one that sets up the
HAS_OBJ flag for each object, without actually loading the
object data.
That's simple and fast, and we don't have to care about the
connectivity_only flag in the rest of the code at all.
While we're at it, let's make sure we treat loose and packed
objects the same (i.e., setting up dummy objects for both
and skipping the actual sha1 check). That makes the
connectivity-only check actually fast on a real repo (40
seconds versus 180 seconds on my copy of linux.git).
Signed-off-by: Jeff King <redacted>
---
The cmd_fsck() part of the diff is pretty nasty without
"-b".
I had originally planned to pull the "stop calling
verify_pack()" bit out into its own patch. But doing this
without treating loose and packed objects the same raises a
lot of questions about why one and not the other. It seemed
simpler to just explain it all together.
builtin/fsck.c | 118 +++++++++++++++++++++++++++++++++++++++++++++-----------
t/t1450-fsck.sh | 23 ++++++++++-
2 files changed, 117 insertions(+), 24 deletions(-)
@@ -205,8 +205,6 @@ static void check_reachable_object(struct object *obj)if(!(obj->flags&HAS_OBJ)){if(has_sha1_pack(obj->oid.hash))return;/* it is in pack - forget about it */-if(connectivity_only&&has_object_file(&obj->oid))-return;printf("missing %s %s\n",typename(obj->type),describe_object(obj));errors_found|=ERROR_REACHABLE;
@@ -584,6 +582,79 @@ static int fsck_cache_tree(struct cache_tree *it)returnerr;}+staticvoidmark_object_for_connectivity(constunsignedchar*sha1)+{+structobject*obj=lookup_object(sha1);++/*+*Settingtheobjecttypehereisn'tstrictlynecessaryherefora+*connectivitycheck.Inmostcases,ourwalkwillexpectacertain+*type(e.g.,atreereferencingablob)andwilluselookup_blob()to+*assignthetype.Butdoingitherehastwoadvantages:+*+*1.Whenthefsck_walkcodelooksatobjectsthat_don't_comefrom+*links(e.g.,thetipofaref),itmaycomplainaboutthe+*"unknown object type".+*+*2.Thisservesasanicecross-checkthatthegraphlinksare+*sane.So--connectivity-onlydoesnotcheckthatthebitsof+*blobsarenotcorrupted,butit_does_checkthat100644tree+*entriespointtoblobs,andsoforth.+*+*Unfortunatelywecan'tjustuseparse_object()here,becausethe+*wholepointof--connectivity-onlyistoavoidreadingtheobject+*datamorethannecessary.+*/+if(!obj||obj->type==OBJ_NONE){+enumobject_typetype=sha1_object_info(sha1,NULL);+switch(type){+caseOBJ_BAD:+error("%s: unable to read object type",+sha1_to_hex(sha1));+break;+caseOBJ_COMMIT:+obj=(structobject*)lookup_commit(sha1);+break;+caseOBJ_TREE:+obj=(structobject*)lookup_tree(sha1);+break;+caseOBJ_BLOB:+obj=(structobject*)lookup_blob(sha1);+break;+caseOBJ_TAG:+obj=(structobject*)lookup_tag(sha1);+break;+default:+error("%s: unknown object type %d",+sha1_to_hex(sha1),type);+}++if(!obj||obj->type==OBJ_NONE){+errors_found|=ERROR_OBJECT;+return;+}+}++obj->flags|=HAS_OBJ;+}++staticintmark_loose_for_connectivity(constunsignedchar*sha1,+constchar*path,+void*data)+{+mark_object_for_connectivity(sha1);+return0;+}++staticintmark_packed_for_connectivity(constunsignedchar*sha1,+structpacked_git*pack,+uint32_tpos,+void*data)+{+mark_object_for_connectivity(sha1);+return0;+}+staticcharconst*constfsck_usage[]={N_("git fsck [<options>] [<object>...]"),NULL
@@ -523,9 +523,21 @@ test_expect_success 'fsck --connectivity-only' 'touchempty&&gitaddempty&&test_commitempty&&++# Drop the index now; we want to be sure that we+# recursively notice that we notice the broken objects+# because they are reachable from refs, not because+# they are in the index.+rm-f.git/index&&++# corrupt the blob, but in a way that we can still identify+# its type. That lets us see that --connectivity-only is+# not actually looking at the contents, but leaves it+# free to examine the type if it chooses.empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391&&-rm-f$empty&&-echoinvalid>$empty&&+blob=$(echounrelated|githash-object-w--stdin)&&+mv$(sha1_file$blob)$empty&&+test_must_failgitfsck--strict&&gitfsck--strict--connectivity-only&&tree=$(gitrev-parseHEAD:)&&
From: Jeff King <hidden> Date: 2017-01-16 21:40:17
Instead of checking reachability from the refs, you can ask
fsck to check from a particular set of heads. However, the
error checking here is quite lax. In particular:
1. It claims lookup_object() will report an error, which
is not true. It only does a hash lookup, and the user
has no clue that their argument was skipped.
2. When either the name or sha1 cannot be resolved, we
continue to exit with a successful error code, even
though we didn't check what the user asked us to.
This patch fixes both of these cases.
Signed-off-by: Jeff King <redacted>
---
builtin/fsck.c | 7 +++++--
t/t1450-fsck.sh | 5 +++++
2 files changed, 10 insertions(+), 2 deletions(-)
From: Jeff King <hidden> Date: 2017-01-16 23:33:22
On Mon, Jan 16, 2017 at 04:22:31PM -0500, Jeff King wrote:
I came across a repository today that was missing an object, and for
which "git fsck" reported the error but "git fsck --connectivity-check"
did not. It turns out that the shortcut taken by --connectivity-check
violates some assumptions made by the rest of fsck (namely, that every
object in the repo has a "struct object" loaded).
And fsck being a generally neglected tool, I couldn't help but find
several more bugs on the way. :)
[1/6]: t1450: clean up sub-objects in duplicate-entry test
[2/6]: fsck: report trees as dangling
[3/6]: fsck: prepare dummy objects for --connectivity-check
[4/6]: fsck: tighten error-checks of "git fsck <head>"
[5/6]: fsck: do not fallback "git fsck <bogus>" to "git fsck"
[6/6]: fsck: check HAS_OBJ more consistently
builtin/fsck.c | 131 ++++++++++++++++++++++++++++++++++++++++++++------------
t/t1450-fsck.sh | 70 ++++++++++++++++++++++++++++--
2 files changed, 171 insertions(+), 30 deletions(-)
Oh, one thing I forgot to mention: I tried test-merging this with my
other recent topic in the area, jk/loose-object-fsck. There are a few
conflicts in the test script, but nothing hard to resolve (just both of
them adding tests at the end, but both are careful to clean up after
themselves).
-Peff