Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 10322a0

Browse files
peffgitster
authored andcommitted
do not create "struct commit" with xcalloc
In both blame and merge-recursive, we sometimes create a "fake" commit struct for convenience (e.g., to represent the HEAD state as if we would commit it). By allocating ourselves rather than using alloc_commit_node, we do not properly set the "index" field of the commit. This can produce subtle bugs if we then use commit-slab on the resulting commit, as we will share the "0" index with another commit. We can fix this by using alloc_commit_node() to allocate. Note that we cannot free the result, as it is part of our commit allocator. However, both cases were already leaking the allocated commit anyway, so there's nothing to fix up. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 969eba6 commit 10322a0

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
20192019
struct strbuf msg = STRBUF_INIT;
20202020

20212021
time(&now);
2022-
commit = xcalloc(1, sizeof(*commit));
2022+
commit = alloc_commit_node();
20232023
commit->object.parsed = 1;
20242024
commit->date = now;
20252025
commit->object.type = OBJ_COMMIT;

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
4040

4141
static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
4242
{
43-
struct commit *commit = xcalloc(1, sizeof(struct commit));
43+
struct commit *commit = alloc_commit_node();
4444
struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
4545

4646
desc->name = comment;

0 commit comments

Comments
 (0)