Skip to content

Commit 2edffef

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6fe1b14 commit 2edffef

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

t/t1007-hash-object.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,30 @@ for args in "-w --stdin-paths" "--stdin-paths -w"; do
183183
pop_repo
184184
done
185185

186-
test_expect_success 'corrupt tree' '
186+
test_expect_success 'too-short tree' '
187187
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
189210
'
190211

191212
test_expect_success 'corrupt commit' '

tree-walk.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned
2727
const char *path;
2828
unsigned int mode, len;
2929

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"));
3232

3333
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"));
3638
len = strlen(path) + 1;
3739

3840
/* Initialize the descriptor entry */
@@ -81,7 +83,7 @@ void update_tree_entry(struct tree_desc *desc)
8183
unsigned long len = end - (const unsigned char *)buf;
8284

8385
if (size < len)
84-
die("corrupt tree file");
86+
die(_("too-short tree file"));
8587
buf = end;
8688
size -= len;
8789
desc->buffer = buf;

0 commit comments

Comments
 (0)