Skip to content

Commit 37576c1

Browse files
pcloudsgitster
authored andcommitted
commit_tree(): refuse commit messages that contain NULs
Current implementation sees NUL as terminator. If users give a message with NUL byte in it (e.g. editor set to save as UTF-16), the new commit message will have NULs. However following operations (displaying or amending a commit for example) will not keep anything after the first NUL. Stop user right when they do this. If NUL is added by mistake, they have their chance to fix. Otherwise, log messages will no longer be text "git log" and friends would grok. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 13f8b72 commit 37576c1

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,9 @@ int commit_tree(const struct strbuf *msg, unsigned char *tree,
855855

856856
assert_sha1_type(tree, OBJ_TREE);
857857

858+
if (memchr(msg->buf, '\0', msg->len))
859+
return error("a NUL byte in commit log message not allowed.");
860+
858861
/* Not having i18n.commitencoding is the same as having utf-8 */
859862
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
860863

t/t3900-i18n-commit.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ test_expect_success 'no encoding header for base case' '
3434
test z = "z$E"
3535
'
3636

37+
test_expect_failure 'UTF-16 refused because of NULs' '
38+
echo UTF-16 >F &&
39+
git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
40+
'
41+
42+
3743
for H in ISO8859-1 eucJP ISO-2022-JP
3844
do
3945
test_expect_success "$H setup" '

0 commit comments

Comments
 (0)