Skip to content

Commit f14acab

Browse files
committed
Merge branch 'jc/fsck-nul-in-commit' into maint
"git fsck" learned to catch NUL byte in a commit object as potential error and warn. * jc/fsck-nul-in-commit: fsck: detect and warn a commit with embedded NUL fsck_commit_buffer(): do not special case the last validation
2 parents cca9253 + 6d2d780 commit f14acab

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

fsck.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
FUNC(HAS_DOTGIT, WARN) \
6060
FUNC(NULL_SHA1, WARN) \
6161
FUNC(ZERO_PADDED_FILEMODE, WARN) \
62+
FUNC(NUL_IN_COMMIT, WARN) \
6263
/* infos (reported as warnings, but ignored by default) */ \
6364
FUNC(BAD_TAG_NAME, INFO) \
6465
FUNC(MISSING_TAGGER_ENTRY, INFO)
@@ -610,6 +611,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
610611
struct commit_graft *graft;
611612
unsigned parent_count, parent_line_count = 0, author_count;
612613
int err;
614+
const char *buffer_begin = buffer;
613615

614616
if (verify_headers(buffer, size, &commit->object, options))
615617
return -1;
@@ -666,9 +668,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
666668
err = fsck_ident(&buffer, &commit->object, options);
667669
if (err)
668670
return err;
669-
if (!commit->tree)
670-
return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
671-
671+
if (!commit->tree) {
672+
err = report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
673+
if (err)
674+
return err;
675+
}
676+
if (memchr(buffer_begin, '\0', size)) {
677+
err = report(options, &commit->object, FSCK_MSG_NUL_IN_COMMIT,
678+
"NUL byte in the commit object body");
679+
if (err)
680+
return err;
681+
}
672682
return 0;
673683
}
674684

t/t1450-fsck.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,24 @@ test_expect_success 'fsck allows .Ňit' '
427427
)
428428
'
429429

430+
test_expect_success 'NUL in commit' '
431+
rm -fr nul-in-commit &&
432+
git init nul-in-commit &&
433+
(
434+
cd nul-in-commit &&
435+
git commit --allow-empty -m "initial commitQNUL after message" &&
436+
git cat-file commit HEAD >original &&
437+
q_to_nul <original >munged &&
438+
git hash-object -w -t commit --stdin <munged >name &&
439+
git branch bad $(cat name) &&
440+
441+
test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
442+
grep nulInCommit warn.1 &&
443+
git fsck 2>warn.2 &&
444+
grep nulInCommit warn.2
445+
)
446+
'
447+
430448
# create a static test repo which is broken by omitting
431449
# one particular object ($1, which is looked up via rev-parse
432450
# in the new repository).

0 commit comments

Comments
 (0)