Skip to contentSkip to conversation

tree-walk: be more specific about corrupt tree errors

PATCH Superseded David Turner 3 participants 7 messages Sep 27, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 tree-walk: be more specific about corrupt tree errors

From: Jeff King <[email protected]>

When the tree-walker runs into an error, it just calls die(), and the message is always "corrupt tree file". However, we are actually covering several cases here; let's give the user a hint about what happened.

Let's also avoid using the word "corrupt", which makes it seem like the data bit-rotted on disk. Our sha1 check would already have found that. These errors are ones of data that is malformed in the first place.

---
 t/t1007-hash-object.sh | 21 +++++++++++++++++++--
 tree-walk.c            | 12 +++++++-----
 2 files changed, 26 insertions(+), 7 deletions(-)
t/t1007-hash-object.sh +19 -2
index acca9ac..0691b88 100755
@@ -183,9 +183,26 @@ for args in "-w --stdin-paths" "--stdin-paths -w"; do
 	pop_repo
 done
 
-test_expect_success 'corrupt tree' '
+test_expect_success 'too-short tree' '
 	echo abc >malformed-tree &&
-	test_must_fail git hash-object -t tree malformed-tree
+	test_must_fail git hash-object -t tree malformed-tree 2>err &&
+	test_i18ngrep "too-short tree object" err
+'
+
+test_expect_success 'malformed mode in tree' '
+	hex_sha1=$(echo foo | git hash-object --stdin -w) &&
+	bin_sha1=$(echo $hex_sha1 | perl -ne "printf \"\\\\%03o\", ord for /../g") &&
+	printf "9100644 \0$bin_sha1" >tree-with-malformed-mode &&
+	test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
+	test_i18ngrep "malformed mode in tree entry" err
+'
+
+test_expect_success 'empty filename in tree' '
+	hex_sha1=$(echo foo | git hash-object --stdin -w) &&
+	bin_sha1=$(echo $hex_sha1 | perl -ne "printf \"\\\\%03o\", ord for /../g") &&
+	printf "100644 \0$bin_sha1" >tree-with-empty-filename &&
+	test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
+	test_i18ngrep "empty filename in tree entry" err
 '
 
 test_expect_success 'corrupt commit' '
tree-walk.c +7 -5
index ce27842..24f9a0f 100644
@@ -27,12 +27,14 @@ static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned
 	const char *path;
 	unsigned int mode, len;
 
-	if (size < 24 || buf[size - 21])
-		die("corrupt tree file");
+	if (size < 23 || buf[size - 21])
+		die(_("too-short tree object"));
 
 	path = get_mode(buf, &mode);
-	if (!path || !*path)
-		die("corrupt tree file");
+	if (!path)
+		die(_("malformed mode in tree entry for tree"));
+	if (!*path)
+		die(_("empty filename in tree entry for tree"));
 	len = strlen(path) + 1;
 
 	/* Initialize the descriptor entry */
@@ -81,7 +83,7 @@ void update_tree_entry(struct tree_desc *desc)
 	unsigned long len = end - (const unsigned char *)buf;
 
 	if (size < len)
-		die("corrupt tree file");
+		die(_("too-short tree file"));
 	buf = end;
 	size -= len;
 	desc->buffer = buf;
Signature · 1 line
2.8.0.rc4.22.g8ae061a
#1 lore raw

PATCH v3 2/3 fsck: handle bad trees like other errors

Instead of dying when fsck hits a malformed tree object, log the error like any other and continue. Now fsck can tell the user which tree is bad, too.

---
 fsck.c          | 18 ++++++++-----
 t/t1450-fsck.sh | 16 +++++++++--
 tree-walk.c     | 83 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 tree-walk.h     |  8 ++++++
 4 files changed, 106 insertions(+), 19 deletions(-)
fsck.c +12 -6
index c9cf3de..4a3069e 100644
@@ -347,8 +347,9 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
 		return -1;
 
 	name = get_object_name(options, &tree->object);
-	init_tree_desc(&desc, tree->buffer, tree->size);
-	while (tree_entry(&desc, &entry)) {
+	if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
+		return -1;
+	while (tree_entry_gently(&desc, &entry)) {
 		struct object *obj;
 		int result;
 
@@ -520,7 +521,7 @@ static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, con
 
 static int fsck_tree(struct tree *item, struct fsck_options *options)
 {
-	int retval;
+	int retval = 0;
 	int has_null_sha1 = 0;
 	int has_full_path = 0;
 	int has_empty_name = 0;
@@ -535,7 +536,10 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
 	unsigned o_mode;
 	const char *o_name;
 
-	init_tree_desc(&desc, item->buffer, item->size);
+	if (init_tree_desc_gently(&desc, item->buffer, item->size)) {
+		retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
+		return retval;
+	}
 
 	o_mode = 0;
 	o_name = NULL;
@@ -556,7 +560,10 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
 			       is_hfs_dotgit(name) ||
 			       is_ntfs_dotgit(name));
 		has_zero_pad |= *(char *)desc.buffer == '0';
-		update_tree_entry(&desc);
+		if (update_tree_entry_gently(&desc)) {
+			retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
+			break;
+		}
 
 		switch (mode) {
 		/*
@@ -597,7 +604,6 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
 		o_name = name;
 	}
 
-	retval = 0;
 	if (has_null_sha1)
 		retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
 	if (has_full_path)
t/t1450-fsck.sh +14 -2
index 8f52da2..0fcd38a 100755
@@ -188,8 +188,7 @@ test_expect_success 'commit with NUL in header' '
 	grep "error in commit $new.*unterminated header: NUL at offset" out
 '
 
-test_expect_success 'malformatted tree object' '
-	test_when_finished "git update-ref -d refs/tags/wrong" &&
+test_expect_success 'tree object with duplicate entries' '
 	test_when_finished "remove_object \$T" &&
 	T=$(
 		GIT_INDEX_FILE=test-index &&
@@ -208,6 +207,19 @@ test_expect_success 'malformatted tree object' '
 	grep "error in tree .*contains duplicate file entries" out
 '
 
+test_expect_success 'unparseable tree object' '
+	test_when_finished "git update-ref -d refs/heads/wrong" &&
+	test_when_finished "remove_object \$tree_sha1" &&
+	test_when_finished "remove_object \$commit_sha1" &&
+	tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
+	commit_sha1=$(git commit-tree $tree_sha1) &&
+	git update-ref refs/heads/wrong $commit_sha1 &&
+	test_must_fail git fsck 2>out &&
+	test_i18ngrep "error: empty filename in tree entry" out &&
+	test_i18ngrep "$tree_sha1" out &&
+	! test_i18ngrep "fatal: empty filename in tree entry" out
+'
+
 test_expect_success 'tag pointing to nonexistent' '
 	cat >invalid-tag <<-\EOF &&
 	object ffffffffffffffffffffffffffffffffffffffff
tree-walk.c +72 -11

125 lines not on this page · raw message

tree-walk.h +8 -0
index 97a7d69..68bb78b 100644
@@ -25,14 +25,22 @@ static inline int tree_entry_len(const struct name_entry *ne)
 	return (const char *)ne->oid - ne->path - 1;
 }
 
+/*
+ * The _gently versions of these functions warn and return false on a
+ * corrupt tree entry rather than dying,
+ */
+
 void update_tree_entry(struct tree_desc *);
+int update_tree_entry_gently(struct tree_desc *);
 void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
+int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size);
 
 /*
  * Helper function that does both tree_entry_extract() and update_tree_entry()
  * and returns true for success
  */
 int tree_entry(struct tree_desc *, struct name_entry *);
+int tree_entry_gently(struct tree_desc *, struct name_entry *);
 
 void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
 
Signature · 1 line
2.8.0.rc4.22.g8ae061a
#2 lore raw

Re: [PATCH v3 2/3] fsck: handle bad trees like other errors

t/t1450-fsck.sh test_expect_success 'malformatted tree object' ' · line 221

Quoted 12 lines · David Turner
t/t1450-fsck.sh +12 -0
+test_expect_success 'unparseable tree object' '
+	test_when_finished "git update-ref -d refs/heads/wrong" &&
+	test_when_finished "remove_object \$tree_sha1" &&
+	test_when_finished "remove_object \$commit_sha1" &&
+	tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
+	commit_sha1=$(git commit-tree $tree_sha1) &&
+	git update-ref refs/heads/wrong $commit_sha1 &&
+	test_must_fail git fsck 2>out &&
+	test_i18ngrep "error: empty filename in tree entry" out &&
+	test_i18ngrep "$tree_sha1" out &&
+	! test_i18ngrep "fatal: empty filename in tree entry" out
+'

Unfortunately this last one needs to be spelled as:

test_i18ngrep ! "fatal: empty filename in tree entry" out

because the function always pretends to match when GETTEXT_POISON is set.

Other than the minor test fixups, this all looks good to me.

-Peff

#3 lore raw

PATCH v3 3/3 add David Turner's Two Sigma address

In reply to David Turner

From: David Turner <[email protected]>

---
 .mailmap | 1 +
 1 file changed, 1 insertion(+)
.mailmap +1 -0
index 9441a54..9cc33e9 100644
@@ -48,6 +48,7 @@ David Kågedal <[email protected]>
 David Reiss <[email protected]> <dreiss@dreiss-vmware.(none)>
 David S. Miller <[email protected]>
 David Turner <[email protected]> <[email protected]>
+David Turner <[email protected]> <[email protected]>
 Deskin Miller <[email protected]>
 Dirk Süsserott <[email protected]>
 Eric Blake <[email protected]> <[email protected]>
Signature · 1 line
2.8.0.rc4.22.g8ae061a
#4 lore raw

Re: [PATCH v3 3/3] add David Turner's Two Sigma address

Quoted 6 lines · David Turner

From: David Turner <[email protected]>

---
 .mailmap | 1 +
 1 file changed, 1 insertion(+)

Thanks. Queued separately in order to merge to master much earlier than the tree-fsck topic.

Quoted 13 lines
.mailmap +1 -0
index 9441a54..9cc33e9 100644
@@ -48,6 +48,7 @@ David Kågedal <[email protected]>
 David Reiss <[email protected]> <dreiss@dreiss-vmware.(none)>
 David S. Miller <[email protected]>
 David Turner <[email protected]> <[email protected]>
+David Turner <[email protected]> <[email protected]>
 Deskin Miller <[email protected]>
 Dirk Süsserott <[email protected]>
 Eric Blake <[email protected]> <[email protected]>
#5 lore raw
In reply to David Turner
Quoted 12 lines · David Turner

From: Jeff King <[email protected]>

When the tree-walker runs into an error, it just calls die(), and the message is always "corrupt tree file". However, we are actually covering several cases here; let's give the user a hint about what happened.

Let's also avoid using the word "corrupt", which makes it seem like the data bit-rotted on disk. Our sha1 check would already have found that. These errors are ones of data that is malformed in the first place.

Quoted 6 lines
---
 t/t1007-hash-object.sh | 21 +++++++++++++++++++--
 tree-walk.c            | 12 +++++++-----
 2 files changed, 26 insertions(+), 7 deletions(-)

Nice that we now prepare the test data ourselves without shipping as part of the source.

#6 lore raw
In reply to David Turner

t/t1007-hash-object.sh for args in "-w --stdin-paths" "--stdin-paths -w"; do · line 194

Quoted 3 lines · David Turner
t/t1007-hash-object.sh +3 -0
+test_expect_success 'malformed mode in tree' '
+	hex_sha1=$(echo foo | git hash-object --stdin -w) &&
+	bin_sha1=$(echo $hex_sha1 | perl -ne "printf \"\\\\%03o\", ord for /../g") &&

Sorry, the perl snippet I posted earlier was totally braindead. It gives you the octal for the raw bytes, but we really just want to convert the hex to octal (we could also print the raw bytes from the hex, but I didn't want to take chances on shells that can't handle NULs in environment variables).

I also find it helps to define a helper function outside of the test block to avoid quoting hell. So something like:

  hex2oct () {
	perl -ne 'printf "\\%03o", hex for /../g'

}

test_expect_success ... '

	bin_sha1=$(echo $hex_sha1 | hex2oct)

'

-Peff