object_as_type: initialize commit-graph-related fields of 'struct commit'

When the commit graph and generation numbers were introduced in commits 177722b344 (commit: integrate commit graph with commit parsing, 2018-04-10) and 83073cc994 (commit: add generation number to struct commit, 2018-04-25), they tried to make sure that the corresponding 'graph_pos' and 'generation' fields of 'struct commit' are initialized conservatively, as if the commit were not included in the commit-graph file. Alas, initializing those fields only in alloc_commit_node() missed the case when an object that happens to be a commit is first looked up via lookup_unknown_object(), and is then later converted to a 'struct commit' via the object_as_type() helper function (either calling it directly, or as part of a subsequent lookup_commit() call). Consequently, both of those fields incorrectly remain set to zero, which means e.g. that the commit is present in and is the first entry of the commit-graph file. This will result in wrong timestamp, parent and root tree hashes, if such a 'struct commit' instance is later filled from the commit-graph. Extract the initialization of 'struct commit's fields from alloc_commit_node() into a helper function, and call it from object_as_type() as well, to make sure that it properly initializes the two commit-graph-related fields, too. With this helper function it is hopefully less likely that any new fields added to 'struct commit' in the future would remain uninitialized. With this change alloc_commit_index() won't have any remaining callers outside of 'alloc.c', so mark it as static. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jan 27, 2019 at 14:08 UTC 4468d4435c44d50723e96e3416f8b5da97a1806f
3 files changed +12 -6
alloc.c
+8 -3
@@ -99,18 +99,23 @@ void *alloc_object_node(struct repository *r)
99 return obj;
100 }
101
102 -unsigned int alloc_commit_index(struct repository *r)
102 +static unsigned int alloc_commit_index(struct repository *r)
103 {
104 return r->parsed_objects->commit_count++;
105 }
106
107 -void *alloc_commit_node(struct repository *r)
107 +void init_commit_node(struct repository *r, struct commit *c)
108 {
109 - struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
109 c->object.type = OBJ_COMMIT;
110 c->index = alloc_commit_index(r);
111 c->graph_pos = COMMIT_NOT_FROM_GRAPH;
112 c->generation = GENERATION_NUMBER_INFINITY;
113 +}
114 +
115 +void *alloc_commit_node(struct repository *r)
116 +{
117 + struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
118 + init_commit_node(r, c);
119 return c;
120 }
121
alloc.h
+1 -1
@@ -9,11 +9,11 @@ struct repository;
9
10 void *alloc_blob_node(struct repository *r);
11 void *alloc_tree_node(struct repository *r);
12 +void init_commit_node(struct repository *r, struct commit *c);
13 void *alloc_commit_node(struct repository *r);
14 void *alloc_tag_node(struct repository *r);
15 void *alloc_object_node(struct repository *r);
16 void alloc_report(struct repository *r);
16 -unsigned int alloc_commit_index(struct repository *r);
17
18 struct alloc_state *allocate_alloc_state(void);
19 void clear_alloc_state(struct alloc_state *s);
object.c
+3 -2
@@ -164,8 +164,9 @@ void *object_as_type(struct repository *r, struct object *obj, enum object_type
164 return obj;
165 else if (obj->type == OBJ_NONE) {
166 if (type == OBJ_COMMIT)
167 - ((struct commit *)obj)->index = alloc_commit_index(r);
168 - obj->type = type;
167 + init_commit_node(r, (struct commit *) obj);
168 + else
169 + obj->type = type;
170 return obj;
171 }
172 else {