Skip to content

Commit 31c8221

Browse files
micichgitster
authored andcommitted
mktree: validate entry type in input
Previously mktree would accept tree entries which had a mismatch between the declared type and the actual type of object. Check the actual type of the object when it is available locally. Signed-off-by: Josh Micich <josh.micich@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f1cf2d8 commit 31c8221

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

builtin-mktree.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
7171
{
7272
char *ptr, *ntr;
7373
unsigned mode;
74-
enum object_type type;
74+
enum object_type mode_type; /* object type derived from mode */
75+
enum object_type obj_type; /* object type derived from sha */
7576
char *path;
7677
unsigned char sha1[20];
7778

@@ -94,17 +95,8 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
9495
if (S_ISGITLINK(mode))
9596
allow_missing = 1;
9697

97-
if (!allow_missing)
98-
type = sha1_object_info(sha1, NULL);
99-
else
100-
type = object_type(mode);
101-
102-
if (type < 0)
103-
die("object %s unavailable", sha1_to_hex(sha1));
10498

10599
*ntr++ = 0; /* now at the beginning of SHA1 */
106-
if (type != type_from_string(ptr))
107-
die("object type %s mismatch (%s)", ptr, typename(type));
108100

109101
path = ntr + 41; /* at the beginning of name */
110102
if (line_termination && path[0] == '"') {
@@ -113,6 +105,37 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
113105
die("invalid quoting");
114106
path = strbuf_detach(&p_uq, NULL);
115107
}
108+
109+
/*
110+
* Object type is redundantly derivable three ways.
111+
* These should all agree.
112+
*/
113+
mode_type = object_type(mode);
114+
if (mode_type != type_from_string(ptr)) {
115+
die("entry '%s' object type (%s) doesn't match mode type (%s)",
116+
path, ptr, typename(mode_type));
117+
}
118+
119+
/* Check the type of object identified by sha1 */
120+
obj_type = sha1_object_info(sha1, NULL);
121+
if (obj_type < 0) {
122+
if (allow_missing) {
123+
; /* no problem - missing objects are presumed to be of the right type */
124+
} else {
125+
die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1));
126+
}
127+
} else {
128+
if (obj_type != mode_type) {
129+
/*
130+
* The object exists but is of the wrong type.
131+
* This is a problem regardless of allow_missing
132+
* because the new tree entry will never be correct.
133+
*/
134+
die("entry '%s' object %s is a %s but specified type was (%s)",
135+
path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type));
136+
}
137+
}
138+
116139
append_to_tree(mode, sha1, path);
117140
}
118141

0 commit comments

Comments
 (0)