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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 10, 2014 at 17:39 UTC 10322a0aaf84382d8901f9ab59e59c39f0c035bb
2 files changed +2 -2
builtin/blame.c
+1 -1
@@ -2019,7 +2019,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
2019 struct strbuf msg = STRBUF_INIT;
2020
2021 time(&now);
2022 - commit = xcalloc(1, sizeof(*commit));
2022 + commit = alloc_commit_node();
2023 commit->object.parsed = 1;
2024 commit->date = now;
2025 commit->object.type = OBJ_COMMIT;
merge-recursive.c
+1 -1
@@ -40,7 +40,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
40
41 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
42 {
43 - struct commit *commit = xcalloc(1, sizeof(struct commit));
43 + struct commit *commit = alloc_commit_node();
44 struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
45
46 desc->name = comment;