tree-walk: be more specific about corrupt tree errors
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. Signed-off-by: David Turner <dturner@twosigma.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 27, 2016 at 16:59 UTC
2edffef2337eae691a454a072e0f9b9538725317
2 files changed
+30
-7
t/t1007-hash-object.sh
+23
-2
@@ -183,9 +183,30 @@ for args in "-w --stdin-paths" "--stdin-paths -w"; do
183
pop_repo
184
done
185
186
-test_expect_success 'corrupt tree' '
186
+test_expect_success 'too-short tree' '
187
echo abc >malformed-tree &&
188
- test_must_fail git hash-object -t tree malformed-tree
188
+ test_must_fail git hash-object -t tree malformed-tree 2>err &&
189
+ test_i18ngrep "too-short tree object" err
190
+'
191
+
192
+hex2oct() {
193
+ perl -ne 'printf "\\%03o", hex for /../g'
194
+}
195
+
196
+test_expect_success 'malformed mode in tree' '
197
+ hex_sha1=$(echo foo | git hash-object --stdin -w) &&
198
+ bin_sha1=$(echo $hex_sha1 | hex2oct) &&
199
+ printf "9100644 \0$bin_sha1" >tree-with-malformed-mode &&
200
+ test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
201
+ test_i18ngrep "malformed mode in tree entry" err
202
+'
203
+
204
+test_expect_success 'empty filename in tree' '
205
+ hex_sha1=$(echo foo | git hash-object --stdin -w) &&
206
+ bin_sha1=$(echo $hex_sha1 | hex2oct) &&
207
+ printf "100644 \0$bin_sha1" >tree-with-empty-filename &&
208
+ test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
209
+ test_i18ngrep "empty filename in tree entry" err
210
'
211
212
test_expect_success 'corrupt commit' '
tree-walk.c
+7
-5
@@ -27,12 +27,14 @@ static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned
27
const char *path;
28
unsigned int mode, len;
29
30
- if (size < 24 || buf[size - 21])
31
- die("corrupt tree file");
30
+ if (size < 23 || buf[size - 21])
31
+ die(_("too-short tree object"));
32
33
path = get_mode(buf, &mode);
34
- if (!path || !*path)
35
- die("corrupt tree file");
34
+ if (!path)
35
+ die(_("malformed mode in tree entry for tree"));
36
+ if (!*path)
37
+ die(_("empty filename in tree entry for tree"));
38
len = strlen(path) + 1;
39
40
/* Initialize the descriptor entry */
@@ -81,7 +83,7 @@ void update_tree_entry(struct tree_desc *desc)
83
unsigned long len = end - (const unsigned char *)buf;
84
85
if (size < len)
84
- die("corrupt tree file");
86
+ die(_("too-short tree file"));
87
buf = end;
88
size -= len;
89
desc->buffer = buf;