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>
Josh Micich committed
May 14, 2009 at 15:49 UTC
31c8221acb7c7acaea6ce2c680c2985581ce0066
1 file changed
+33
-10
builtin-mktree.c
+33
-10
@@ -71,7 +71,8 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
71
{
72
char *ptr, *ntr;
73
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 */
76
char *path;
77
unsigned char sha1[20];
78
@@ -94,17 +95,8 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
95
if (S_ISGITLINK(mode))
96
allow_missing = 1;
97
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));
98
99
*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));
100
101
path = ntr + 41; /* at the beginning of name */
102
if (line_termination && path[0] == '"') {
@@ -113,6 +105,37 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
105
die("invalid quoting");
106
path = strbuf_detach(&p_uq, NULL);
107
}
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
+
139
append_to_tree(mode, sha1, path);
140
}
141