Skip to content

Commit 53602a9

Browse files
peffgitster
authored andcommitted
fsck: actually detect bad file modes in trees
We use the normal tree_desc code to iterate over trees in fsck, meaning we only see the canonicalized modes it returns. And hence we'd never see anything unexpected, since it will coerce literally any garbage into one of our normal and accepted modes. We can use the new RAW_MODES flag to see the real modes, and then use the existing code to actually analyze them. The existing code is written as allow-known-good, so there's not much point in testing a variety of breakages. The one tested here should be S_IFREG but with nonsense permissions. Do note that the error-reporting here isn't great. We don't mention the specific bad mode, but just that the tree has one or more broken modes. But when you go to look at it with "git ls-tree", we'll report the canonicalized mode! This isn't ideal, but given that this should come up rarely, and that any number of other tree corruptions might force you into looking at the binary bytes via "cat-file", it's not the end of the world. And it's something we can improve on top later if we choose. Reported-by: Xavier Morel <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec18b10 commit 53602a9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ static int fsck_tree(const struct object_id *tree_oid,
578578
const char *o_name;
579579
struct name_stack df_dup_candidates = { NULL };
580580

581-
if (init_tree_desc_gently(&desc, buffer, size, 0)) {
581+
if (init_tree_desc_gently(&desc, buffer, size, TREE_DESC_RAW_MODES)) {
582582
retval += report(options, tree_oid, OBJ_TREE,
583583
FSCK_MSG_BAD_TREE,
584584
"cannot be parsed as a tree");

t/t1450-fsck.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,20 @@ test_expect_success 'tree entry with type mismatch' '
364364
test_i18ngrep ! "dangling blob" out
365365
'
366366

367+
test_expect_success 'tree entry with bogus mode' '
368+
test_when_finished "remove_object \$blob" &&
369+
test_when_finished "remove_object \$tree" &&
370+
blob=$(echo blob | git hash-object -w --stdin) &&
371+
blob_oct=$(echo $blob | hex2oct) &&
372+
tree=$(printf "100000 foo\0${blob_oct}" |
373+
git hash-object -t tree --stdin -w --literally) &&
374+
git fsck 2>err &&
375+
cat >expect <<-EOF &&
376+
warning in tree $tree: badFilemode: contains bad file modes
377+
EOF
378+
test_cmp expect err
379+
'
380+
367381
test_expect_success 'tag pointing to nonexistent' '
368382
badoid=$(test_oid deadbeef) &&
369383
cat >invalid-tag <<-EOF &&

0 commit comments

Comments
 (0)