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

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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52604d7 commit fe24d39

File tree

8 files changed

+13
-12
lines changed

8 files changed

+13
-12
lines changed

alloc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,31 @@ static struct alloc_state blob_state;
5252
void *alloc_blob_node(void)
5353
{
5454
struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
55+
b->object.type = OBJ_BLOB;
5556
return b;
5657
}
5758

5859
static struct alloc_state tree_state;
5960
void *alloc_tree_node(void)
6061
{
6162
struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
63+
t->object.type = OBJ_TREE;
6264
return t;
6365
}
6466

6567
static struct alloc_state tag_state;
6668
void *alloc_tag_node(void)
6769
{
6870
struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
71+
t->object.type = OBJ_TAG;
6972
return t;
7073
}
7174

7275
static struct alloc_state object_state;
7376
void *alloc_object_node(void)
7477
{
7578
struct object *obj = alloc_node(&object_state, sizeof(union any_object));
79+
obj->type = OBJ_NONE;
7680
return obj;
7781
}
7882

@@ -82,6 +86,7 @@ void *alloc_commit_node(void)
8286
{
8387
static int commit_count;
8488
struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
89+
c->object.type = OBJ_COMMIT;
8590
c->index = commit_count++;
8691
return c;
8792
}

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
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct tree *lookup_tree(const unsigned char *sha1)
183183
{
184184
struct object *obj = lookup_object(sha1);
185185
if (!obj)
186-
return create_object(sha1, OBJ_TREE, alloc_tree_node());
186+
return create_object(sha1, alloc_tree_node());
187187
if (!obj->type)
188188
obj->type = OBJ_TREE;
189189
if (obj->type != OBJ_TREE) {

0 commit comments

Comments
 (0)