Skip to content

Commit 6d2d780

Browse files
committed
fsck: detect and warn a commit with embedded NUL
Even though a Git commit object is designed to be capable of storing any binary data as its payload, in practice people use it to describe the changes in textual form, and tools like "git log" are designed to treat the payload as text. Detect and warn when we see any commit object with a NUL byte in it. Note that a NUL byte in the header part is already detected as a grave error. This change is purely about the message part. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5af2971 commit 6d2d780

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

fsck.c

Lines changed: 8 additions & 0 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;
@@ -671,6 +673,12 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
671673
if (err)
672674
return err;
673675
}
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+
}
674682
return 0;
675683
}
676684

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)