Skip to content

Commit f35ccd9

Browse files
committed
Merge branch 'nd/war-on-nul-in-commit'
* nd/war-on-nul-in-commit: commit_tree(): refuse commit messages that contain NULs Convert commit_tree() to take strbuf as message merge: abort if fails to commit Conflicts: builtin/commit.c commit.c commit.h
2 parents 52b9d2c + 37576c1 commit f35ccd9

File tree

10 files changed

+33
-17
lines changed

10 files changed

+33
-17
lines changed

builtin/commit-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
9898
die_errno("git commit-tree: failed to read");
9999
}
100100

101-
if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
101+
if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, NULL)) {
102102
strbuf_release(&buffer);
103103
return 1;
104104
}

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14921492
append_merge_tag_headers(parents, &tail);
14931493
}
14941494

1495-
if (commit_tree_extended(sb.buf, active_cache_tree->sha1, parents, sha1,
1495+
if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
14961496
author_ident.buf, extra)) {
14971497
rollback_index_files();
14981498
die(_("failed to write commit object"));

builtin/merge.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ static int merge_trivial(struct commit *head)
910910
parent->next->item = remoteheads->item;
911911
parent->next->next = NULL;
912912
prepare_to_commit();
913-
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
913+
if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL))
914+
die(_("failed to write commit object"));
914915
finish(head, result_commit, "In-index merge");
915916
drop_save();
916917
return 0;
@@ -941,7 +942,8 @@ static int finish_automerge(struct commit *head,
941942
strbuf_addch(&merge_msg, '\n');
942943
prepare_to_commit();
943944
free_commit_list(remoteheads);
944-
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
945+
if (commit_tree(&merge_msg, result_tree, parents, result_commit, NULL))
946+
die(_("failed to write commit object"));
945947
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
946948
finish(head, result_commit, buf.buf);
947949
strbuf_release(&buf);

builtin/notes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ void commit_notes(struct notes_tree *t, const char *msg)
301301
return; /* don't have to commit an unchanged tree */
302302

303303
/* Prepare commit message and reflog message */
304-
strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
305304
strbuf_addstr(&buf, msg);
306305
if (buf.buf[buf.len - 1] != '\n')
307306
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
308307

309-
create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
308+
create_notes_commit(t, NULL, &buf, commit_sha1);
309+
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
310310
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
311311

312312
strbuf_release(&buf);

commit.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ void free_commit_extra_headers(struct commit_extra_header *extra)
973973
}
974974
}
975975

976-
int commit_tree(const char *msg, unsigned char *tree,
976+
int commit_tree(const struct strbuf *msg, unsigned char *tree,
977977
struct commit_list *parents, unsigned char *ret,
978978
const char *author)
979979
{
@@ -991,7 +991,7 @@ static const char commit_utf8_warn[] =
991991
"You may want to amend it after fixing the message, or set the config\n"
992992
"variable i18n.commitencoding to the encoding your project uses.\n";
993993

994-
int commit_tree_extended(const char *msg, unsigned char *tree,
994+
int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
995995
struct commit_list *parents, unsigned char *ret,
996996
const char *author, struct commit_extra_header *extra)
997997
{
@@ -1001,6 +1001,9 @@ int commit_tree_extended(const char *msg, unsigned char *tree,
10011001

10021002
assert_sha1_type(tree, OBJ_TREE);
10031003

1004+
if (memchr(msg->buf, '\0', msg->len))
1005+
return error("a NUL byte in commit log message not allowed.");
1006+
10041007
/* Not having i18n.commitencoding is the same as having utf-8 */
10051008
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
10061009

@@ -1037,7 +1040,7 @@ int commit_tree_extended(const char *msg, unsigned char *tree,
10371040
strbuf_addch(&buffer, '\n');
10381041

10391042
/* And add the comment */
1040-
strbuf_addstr(&buffer, msg);
1043+
strbuf_addbuf(&buffer, msg);
10411044

10421045
/* And check the encoding */
10431046
if (encoding_is_utf8 && !is_utf8(buffer.buf))

commit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ struct commit_extra_header {
191191
extern void append_merge_tag_headers(struct commit_list *parents,
192192
struct commit_extra_header ***tail);
193193

194-
extern int commit_tree(const char *msg, unsigned char *tree,
194+
extern int commit_tree(const struct strbuf *msg, unsigned char *tree,
195195
struct commit_list *parents, unsigned char *ret,
196196
const char *author);
197197

198-
extern int commit_tree_extended(const char *msg, unsigned char *tree,
198+
extern int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
199199
struct commit_list *parents, unsigned char *ret,
200200
const char *author,
201201
struct commit_extra_header *);

notes-cache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int notes_cache_write(struct notes_cache *c)
4848
{
4949
unsigned char tree_sha1[20];
5050
unsigned char commit_sha1[20];
51+
struct strbuf msg = STRBUF_INIT;
5152

5253
if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
5354
return -1;
@@ -56,7 +57,9 @@ int notes_cache_write(struct notes_cache *c)
5657

5758
if (write_notes_tree(&c->tree, tree_sha1))
5859
return -1;
59-
if (commit_tree(c->validity, tree_sha1, NULL, commit_sha1, NULL) < 0)
60+
strbuf_attach(&msg, c->validity,
61+
strlen(c->validity), strlen(c->validity) + 1);
62+
if (commit_tree(&msg, tree_sha1, NULL, commit_sha1, NULL) < 0)
6063
return -1;
6164
if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
6265
0, QUIET_ON_ERR) < 0)

notes-merge.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static int merge_from_diffs(struct notes_merge_options *o,
530530
}
531531

532532
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
533-
const char *msg, unsigned char *result_sha1)
533+
const struct strbuf *msg, unsigned char *result_sha1)
534534
{
535535
unsigned char tree_sha1[20];
536536

@@ -668,7 +668,7 @@ int notes_merge(struct notes_merge_options *o,
668668
struct commit_list *parents = NULL;
669669
commit_list_insert(remote, &parents); /* LIFO order */
670670
commit_list_insert(local, &parents);
671-
create_notes_commit(local_tree, parents, o->commit_msg.buf,
671+
create_notes_commit(local_tree, parents, &o->commit_msg,
672672
result_sha1);
673673
}
674674

@@ -695,7 +695,8 @@ int notes_merge_commit(struct notes_merge_options *o,
695695
struct dir_struct dir;
696696
char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
697697
int path_len = strlen(path), i;
698-
const char *msg = strstr(partial_commit->buffer, "\n\n");
698+
char *msg = strstr(partial_commit->buffer, "\n\n");
699+
struct strbuf sb_msg = STRBUF_INIT;
699700

700701
if (o->verbosity >= 3)
701702
printf("Committing notes in notes merge worktree at %.*s\n",
@@ -733,7 +734,8 @@ int notes_merge_commit(struct notes_merge_options *o,
733734
sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
734735
}
735736

736-
create_notes_commit(partial_tree, partial_commit->parents, msg,
737+
strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
738+
create_notes_commit(partial_tree, partial_commit->parents, &sb_msg,
737739
result_sha1);
738740
if (o->verbosity >= 4)
739741
printf("Finalized notes merge commit: %s\n",

notes-merge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void init_notes_merge_options(struct notes_merge_options *o);
3737
* The resulting commit SHA1 is stored in result_sha1.
3838
*/
3939
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
40-
const char *msg, unsigned char *result_sha1);
40+
const struct strbuf *msg, unsigned char *result_sha1);
4141

4242
/*
4343
* Merge notes from o->remote_ref into o->local_ref

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)