move setting of object->type to alloc_* functions · mobileAgent/git@fe24d39 · GitHub
Skip to content

Commit fe24d39

Browse files
peffgitster
authored andcommitted
move setting of object->type to alloc_* functions
The "struct object" type implements basic object polymorphism. Individual instances are allocated as concrete types (or as a union type that can store any object), and a "struct object *" can be cast into its real type after examining its "type" enum. This means it is dangerous to have a type field that does not match the allocation (e.g., setting the type field of a "struct blob" to "OBJ_COMMIT" would mean that a reader might read past the allocated memory). In most of the current code this is not a problem; the first thing we do after allocating an object is usually to set its type field by passing it to create_object. However, the virtual commits we create in merge-recursive.c do not ever get their type set. This does not seem to have caused problems in practice, though (presumably because we always pass around a "struct commit" pointer and never even look at the type). We can fix this oversight and also make it harder for future code to get it wrong by setting the type directly in the object allocation functions. This will also make it easier to fix problems with commit index allocation, as we know that any object allocated by alloc_commit_node will meet the invariant that an object with an OBJ_COMMIT type field will have a unique index number. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 52604d7 commit fe24d39

8 files changed

Lines changed: 13 additions & 12 deletions

File tree

alloc.c

Lines changed: 5 additions & 0 deletions

blob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct blob *lookup_blob(const unsigned char *sha1)
77
{
88
struct object *obj = lookup_object(sha1);
99
if (!obj)
10-
return create_object(sha1, OBJ_BLOB, alloc_blob_node());
10+
return create_object(sha1, alloc_blob_node());
1111
if (!obj->type)
1212
obj->type = OBJ_BLOB;
1313
if (obj->type != OBJ_BLOB) {

builtin/blame.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,6 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
20412041
commit = alloc_commit_node();
20422042
commit->object.parsed = 1;
20432043
commit->date = now;
2044-
commit->object.type = OBJ_COMMIT;
20452044
parent_tail = &commit->parents;
20462045

20472046
if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))

commit.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
6161
struct commit *lookup_commit(const unsigned char *sha1)
6262
{
6363
struct object *obj = lookup_object(sha1);
64-
if (!obj) {
65-
struct commit *c = alloc_commit_node();
66-
return create_object(sha1, OBJ_COMMIT, c);
67-
}
64+
if (!obj)
65+
return create_object(sha1, alloc_commit_node());
6866
if (!obj->type)
6967
obj->type = OBJ_COMMIT;
7068
return check_commit(obj, sha1, 0);

object.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ static void grow_object_hash(void)
141141
obj_hash_size = new_hash_size;
142142
}
143143

144-
void *create_object(const unsigned char *sha1, int type, void *o)
144+
void *create_object(const unsigned char *sha1, void *o)
145145
{
146146
struct object *obj = o;
147147

148148
obj->parsed = 0;
149149
obj->used = 0;
150-
obj->type = type;
151150
obj->flags = 0;
152151
hashcpy(obj->sha1, sha1);
153152

@@ -163,7 +162,7 @@ struct object *lookup_unknown_object(const unsigned char *sha1)
163162
{
164163
struct object *obj = lookup_object(sha1);
165164
if (!obj)
166-
obj = create_object(sha1, OBJ_NONE, alloc_object_node());
165+
obj = create_object(sha1, alloc_object_node());
167166
return obj;
168167
}
169168

object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extern struct object *get_indexed_object(unsigned int);
7979
*/
8080
struct object *lookup_object(const unsigned char *sha1);
8181

82-
extern void *create_object(const unsigned char *sha1, int type, void *obj);
82+
extern void *create_object(const unsigned char *sha1, void *obj);
8383

8484
/*
8585
* Returns the object, having parsed it to find out what it is.

tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct tag *lookup_tag(const unsigned char *sha1)
4040
{
4141
struct object *obj = lookup_object(sha1);
4242
if (!obj)
43-
return create_object(sha1, OBJ_TAG, alloc_tag_node());
43+
return create_object(sha1, alloc_tag_node());
4444
if (!obj->type)
4545
obj->type = OBJ_TAG;
4646
if (obj->type != OBJ_TAG) {

tree.c

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)