]>
Commit | Line | Data |
---|---|---|
355885d5 | 1 | #include "cache.h" |
cbd53a21 | 2 | #include "object-store.h" |
109cd76d | 3 | #include "repository.h" |
355885d5 MK |
4 | #include "object.h" |
5 | #include "blob.h" | |
6 | #include "tree.h" | |
7 | #include "tree-walk.h" | |
8 | #include "commit.h" | |
9 | #include "tag.h" | |
10 | #include "fsck.h" | |
cec097be | 11 | #include "refs.h" |
a2b26ffb | 12 | #include "url.h" |
a18fcc9f | 13 | #include "utf8.h" |
7b35efd7 | 14 | #include "decorate.h" |
159e7b08 | 15 | #include "oidset.h" |
27387444 | 16 | #include "packfile.h" |
ed8b10f6 JK |
17 | #include "submodule-config.h" |
18 | #include "config.h" | |
07259e74 | 19 | #include "credential.h" |
3ac68a93 | 20 | #include "help.h" |
159e7b08 JK |
21 | |
22 | static struct oidset gitmodules_found = OIDSET_INIT; | |
23 | static struct oidset gitmodules_done = OIDSET_INIT; | |
355885d5 | 24 | |
f50c4407 | 25 | #define FSCK_FATAL -1 |
f27d05b1 | 26 | #define FSCK_INFO -2 |
f50c4407 | 27 | |
c99ba492 | 28 | #define FOREACH_MSG_ID(FUNC) \ |
f50c4407 JS |
29 | /* fatal errors */ \ |
30 | FUNC(NUL_IN_HEADER, FATAL) \ | |
31 | FUNC(UNTERMINATED_HEADER, FATAL) \ | |
c99ba492 JS |
32 | /* errors */ \ |
33 | FUNC(BAD_DATE, ERROR) \ | |
34 | FUNC(BAD_DATE_OVERFLOW, ERROR) \ | |
35 | FUNC(BAD_EMAIL, ERROR) \ | |
36 | FUNC(BAD_NAME, ERROR) \ | |
37 | FUNC(BAD_OBJECT_SHA1, ERROR) \ | |
38 | FUNC(BAD_PARENT_SHA1, ERROR) \ | |
39 | FUNC(BAD_TAG_OBJECT, ERROR) \ | |
40 | FUNC(BAD_TIMEZONE, ERROR) \ | |
41 | FUNC(BAD_TREE, ERROR) \ | |
42 | FUNC(BAD_TREE_SHA1, ERROR) \ | |
43 | FUNC(BAD_TYPE, ERROR) \ | |
44 | FUNC(DUPLICATE_ENTRIES, ERROR) \ | |
45 | FUNC(MISSING_AUTHOR, ERROR) \ | |
46 | FUNC(MISSING_COMMITTER, ERROR) \ | |
47 | FUNC(MISSING_EMAIL, ERROR) \ | |
c99ba492 JS |
48 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ |
49 | FUNC(MISSING_OBJECT, ERROR) \ | |
c99ba492 JS |
50 | FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \ |
51 | FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \ | |
52 | FUNC(MISSING_TAG, ERROR) \ | |
53 | FUNC(MISSING_TAG_ENTRY, ERROR) \ | |
c99ba492 | 54 | FUNC(MISSING_TREE, ERROR) \ |
159e7b08 | 55 | FUNC(MISSING_TREE_OBJECT, ERROR) \ |
c99ba492 JS |
56 | FUNC(MISSING_TYPE, ERROR) \ |
57 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ | |
c9ad147f | 58 | FUNC(MULTIPLE_AUTHORS, ERROR) \ |
c99ba492 JS |
59 | FUNC(TREE_NOT_SORTED, ERROR) \ |
60 | FUNC(UNKNOWN_TYPE, ERROR) \ | |
c99ba492 | 61 | FUNC(ZERO_PADDED_DATE, ERROR) \ |
159e7b08 JK |
62 | FUNC(GITMODULES_MISSING, ERROR) \ |
63 | FUNC(GITMODULES_BLOB, ERROR) \ | |
0d68764d | 64 | FUNC(GITMODULES_LARGE, ERROR) \ |
ed8b10f6 | 65 | FUNC(GITMODULES_NAME, ERROR) \ |
b7b1fca1 | 66 | FUNC(GITMODULES_SYMLINK, ERROR) \ |
a124133e | 67 | FUNC(GITMODULES_URL, ERROR) \ |
1a7fd1fb | 68 | FUNC(GITMODULES_PATH, ERROR) \ |
bb92255e | 69 | FUNC(GITMODULES_UPDATE, ERROR) \ |
c99ba492 JS |
70 | /* warnings */ \ |
71 | FUNC(BAD_FILEMODE, WARN) \ | |
c99ba492 JS |
72 | FUNC(EMPTY_NAME, WARN) \ |
73 | FUNC(FULL_PATHNAME, WARN) \ | |
74 | FUNC(HAS_DOT, WARN) \ | |
75 | FUNC(HAS_DOTDOT, WARN) \ | |
76 | FUNC(HAS_DOTGIT, WARN) \ | |
c99ba492 | 77 | FUNC(NULL_SHA1, WARN) \ |
f27d05b1 | 78 | FUNC(ZERO_PADDED_FILEMODE, WARN) \ |
6d2d780f | 79 | FUNC(NUL_IN_COMMIT, WARN) \ |
f27d05b1 | 80 | /* infos (reported as warnings, but ignored by default) */ \ |
64eb14d3 | 81 | FUNC(GITMODULES_PARSE, INFO) \ |
f27d05b1 | 82 | FUNC(BAD_TAG_NAME, INFO) \ |
acf9de4c ÆAB |
83 | FUNC(MISSING_TAGGER_ENTRY, INFO) \ |
84 | /* ignored (elevated when requested) */ \ | |
85 | FUNC(EXTRA_HEADER_ENTRY, IGNORE) | |
c99ba492 JS |
86 | |
87 | #define MSG_ID(id, msg_type) FSCK_MSG_##id, | |
88 | enum fsck_msg_id { | |
89 | FOREACH_MSG_ID(MSG_ID) | |
90 | FSCK_MSG_MAX | |
91 | }; | |
92 | #undef MSG_ID | |
93 | ||
f417eed8 | 94 | #define STR(x) #x |
a4a9cc19 | 95 | #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type }, |
c99ba492 | 96 | static struct { |
f417eed8 JS |
97 | const char *id_string; |
98 | const char *downcased; | |
a4a9cc19 | 99 | const char *camelcased; |
c99ba492 JS |
100 | int msg_type; |
101 | } msg_id_info[FSCK_MSG_MAX + 1] = { | |
102 | FOREACH_MSG_ID(MSG_ID) | |
a4a9cc19 | 103 | { NULL, NULL, NULL, -1 } |
c99ba492 JS |
104 | }; |
105 | #undef MSG_ID | |
106 | ||
a46baac6 | 107 | static void prepare_msg_ids(void) |
f417eed8 JS |
108 | { |
109 | int i; | |
110 | ||
a46baac6 NTND |
111 | if (msg_id_info[0].downcased) |
112 | return; | |
113 | ||
114 | /* convert id_string to lower case, without underscores. */ | |
115 | for (i = 0; i < FSCK_MSG_MAX; i++) { | |
116 | const char *p = msg_id_info[i].id_string; | |
117 | int len = strlen(p); | |
118 | char *q = xmalloc(len); | |
119 | ||
120 | msg_id_info[i].downcased = q; | |
121 | while (*p) | |
122 | if (*p == '_') | |
123 | p++; | |
124 | else | |
125 | *(q)++ = tolower(*(p)++); | |
126 | *q = '\0'; | |
a4a9cc19 NTND |
127 | |
128 | p = msg_id_info[i].id_string; | |
129 | q = xmalloc(len); | |
130 | msg_id_info[i].camelcased = q; | |
131 | while (*p) { | |
132 | if (*p == '_') { | |
133 | p++; | |
134 | if (*p) | |
135 | *q++ = *p++; | |
136 | } else { | |
137 | *q++ = tolower(*p++); | |
138 | } | |
f417eed8 | 139 | } |
a4a9cc19 | 140 | *q = '\0'; |
f417eed8 | 141 | } |
a46baac6 NTND |
142 | } |
143 | ||
144 | static int parse_msg_id(const char *text) | |
145 | { | |
146 | int i; | |
147 | ||
148 | prepare_msg_ids(); | |
f417eed8 JS |
149 | |
150 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
151 | if (!strcmp(text, msg_id_info[i].downcased)) | |
152 | return i; | |
153 | ||
154 | return -1; | |
155 | } | |
156 | ||
3ac68a93 NTND |
157 | void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) |
158 | { | |
159 | int i; | |
160 | ||
161 | prepare_msg_ids(); | |
162 | ||
3ac68a93 | 163 | for (i = 0; i < FSCK_MSG_MAX; i++) |
a4a9cc19 | 164 | list_config_item(list, prefix, msg_id_info[i].camelcased); |
3ac68a93 NTND |
165 | } |
166 | ||
c99ba492 JS |
167 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
168 | struct fsck_options *options) | |
169 | { | |
0282f4dc JS |
170 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); |
171 | ||
e35d65a7 ÆAB |
172 | if (!options->msg_type) { |
173 | int msg_type = msg_id_info[msg_id].msg_type; | |
174 | ||
0282f4dc JS |
175 | if (options->strict && msg_type == FSCK_WARN) |
176 | msg_type = FSCK_ERROR; | |
e35d65a7 | 177 | return msg_type; |
0282f4dc | 178 | } |
c99ba492 | 179 | |
e35d65a7 | 180 | return options->msg_type[msg_id]; |
c99ba492 JS |
181 | } |
182 | ||
0282f4dc JS |
183 | static int parse_msg_type(const char *str) |
184 | { | |
185 | if (!strcmp(str, "error")) | |
186 | return FSCK_ERROR; | |
187 | else if (!strcmp(str, "warn")) | |
188 | return FSCK_WARN; | |
efaba7cc JS |
189 | else if (!strcmp(str, "ignore")) |
190 | return FSCK_IGNORE; | |
0282f4dc JS |
191 | else |
192 | die("Unknown fsck message type: '%s'", str); | |
193 | } | |
194 | ||
5d477a33 JS |
195 | int is_valid_msg_type(const char *msg_id, const char *msg_type) |
196 | { | |
197 | if (parse_msg_id(msg_id) < 0) | |
198 | return 0; | |
199 | parse_msg_type(msg_type); | |
200 | return 1; | |
201 | } | |
202 | ||
0282f4dc | 203 | void fsck_set_msg_type(struct fsck_options *options, |
f1abc2d0 | 204 | const char *msg_id_str, const char *msg_type_str) |
0282f4dc | 205 | { |
f1abc2d0 | 206 | int msg_id = parse_msg_id(msg_id_str), msg_type; |
0282f4dc | 207 | |
f1abc2d0 ÆAB |
208 | if (msg_id < 0) |
209 | die("Unhandled message id: %s", msg_id_str); | |
210 | msg_type = parse_msg_type(msg_type_str); | |
0282f4dc | 211 | |
f1abc2d0 ÆAB |
212 | if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL) |
213 | die("Cannot demote %s to %s", msg_id_str, msg_type_str); | |
f50c4407 | 214 | |
0282f4dc JS |
215 | if (!options->msg_type) { |
216 | int i; | |
f1abc2d0 ÆAB |
217 | int *severity; |
218 | ALLOC_ARRAY(severity, FSCK_MSG_MAX); | |
0282f4dc | 219 | for (i = 0; i < FSCK_MSG_MAX; i++) |
f1abc2d0 ÆAB |
220 | severity[i] = fsck_msg_type(i, options); |
221 | options->msg_type = severity; | |
0282f4dc JS |
222 | } |
223 | ||
f1abc2d0 | 224 | options->msg_type[msg_id] = msg_type; |
0282f4dc JS |
225 | } |
226 | ||
227 | void fsck_set_msg_types(struct fsck_options *options, const char *values) | |
228 | { | |
229 | char *buf = xstrdup(values), *to_free = buf; | |
230 | int done = 0; | |
231 | ||
232 | while (!done) { | |
233 | int len = strcspn(buf, " ,|"), equal; | |
234 | ||
235 | done = !buf[len]; | |
236 | if (!len) { | |
237 | buf++; | |
238 | continue; | |
239 | } | |
240 | buf[len] = '\0'; | |
241 | ||
242 | for (equal = 0; | |
243 | equal < len && buf[equal] != '=' && buf[equal] != ':'; | |
244 | equal++) | |
245 | buf[equal] = tolower(buf[equal]); | |
246 | buf[equal] = '\0'; | |
247 | ||
cd94c6f9 JS |
248 | if (!strcmp(buf, "skiplist")) { |
249 | if (equal == len) | |
250 | die("skiplist requires a path"); | |
24eb33eb | 251 | oidset_parse_file(&options->skiplist, buf + equal + 1); |
cd94c6f9 JS |
252 | buf += len + 1; |
253 | continue; | |
254 | } | |
255 | ||
0282f4dc JS |
256 | if (equal == len) |
257 | die("Missing '=': '%s'", buf); | |
355885d5 | 258 | |
0282f4dc JS |
259 | fsck_set_msg_type(options, buf, buf + equal + 1); |
260 | buf += len + 1; | |
261 | } | |
262 | free(to_free); | |
263 | } | |
264 | ||
f5979376 JK |
265 | static int object_on_skiplist(struct fsck_options *opts, |
266 | const struct object_id *oid) | |
fb162877 | 267 | { |
f5979376 | 268 | return opts && oid && oidset_contains(&opts->skiplist, oid); |
fb162877 RJ |
269 | } |
270 | ||
38370253 JK |
271 | __attribute__((format (printf, 5, 6))) |
272 | static int report(struct fsck_options *options, | |
273 | const struct object_id *oid, enum object_type object_type, | |
35af754b | 274 | enum fsck_msg_id msg_id, const char *fmt, ...) |
c99ba492 JS |
275 | { |
276 | va_list ap; | |
277 | struct strbuf sb = STRBUF_INIT; | |
35af754b | 278 | int msg_type = fsck_msg_type(msg_id, options), result; |
c99ba492 | 279 | |
efaba7cc JS |
280 | if (msg_type == FSCK_IGNORE) |
281 | return 0; | |
282 | ||
38370253 | 283 | if (object_on_skiplist(options, oid)) |
cd94c6f9 JS |
284 | return 0; |
285 | ||
f50c4407 JS |
286 | if (msg_type == FSCK_FATAL) |
287 | msg_type = FSCK_ERROR; | |
f27d05b1 JS |
288 | else if (msg_type == FSCK_INFO) |
289 | msg_type = FSCK_WARN; | |
f50c4407 | 290 | |
034a7b7b | 291 | prepare_msg_ids(); |
35af754b | 292 | strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased); |
71ab8fa8 | 293 | |
c99ba492 JS |
294 | va_start(ap, fmt); |
295 | strbuf_vaddf(&sb, fmt, ap); | |
38370253 | 296 | result = options->error_func(options, oid, object_type, |
5afc4b1d | 297 | msg_type, sb.buf); |
c99ba492 JS |
298 | strbuf_release(&sb); |
299 | va_end(ap); | |
300 | ||
301 | return result; | |
302 | } | |
303 | ||
a59cfb32 JK |
304 | void fsck_enable_object_names(struct fsck_options *options) |
305 | { | |
306 | if (!options->object_names) | |
73390290 | 307 | options->object_names = kh_init_oid_map(); |
a59cfb32 JK |
308 | } |
309 | ||
73390290 JK |
310 | const char *fsck_get_object_name(struct fsck_options *options, |
311 | const struct object_id *oid) | |
7b35efd7 | 312 | { |
73390290 | 313 | khiter_t pos; |
7b35efd7 JS |
314 | if (!options->object_names) |
315 | return NULL; | |
73390290 JK |
316 | pos = kh_get_oid_map(options->object_names, *oid); |
317 | if (pos >= kh_end(options->object_names)) | |
318 | return NULL; | |
319 | return kh_value(options->object_names, pos); | |
7b35efd7 JS |
320 | } |
321 | ||
73390290 JK |
322 | void fsck_put_object_name(struct fsck_options *options, |
323 | const struct object_id *oid, | |
a59cfb32 | 324 | const char *fmt, ...) |
7b35efd7 JS |
325 | { |
326 | va_list ap; | |
327 | struct strbuf buf = STRBUF_INIT; | |
73390290 JK |
328 | khiter_t pos; |
329 | int hashret; | |
7b35efd7 JS |
330 | |
331 | if (!options->object_names) | |
332 | return; | |
73390290 JK |
333 | |
334 | pos = kh_put_oid_map(options->object_names, *oid, &hashret); | |
335 | if (!hashret) | |
7b35efd7 JS |
336 | return; |
337 | va_start(ap, fmt); | |
338 | strbuf_vaddf(&buf, fmt, ap); | |
73390290 | 339 | kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL); |
7b35efd7 JS |
340 | va_end(ap); |
341 | } | |
342 | ||
a59cfb32 | 343 | const char *fsck_describe_object(struct fsck_options *options, |
73390290 | 344 | const struct object_id *oid) |
90cf590f | 345 | { |
a59cfb32 JK |
346 | static struct strbuf bufs[] = { |
347 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
348 | }; | |
349 | static int b = 0; | |
350 | struct strbuf *buf; | |
73390290 | 351 | const char *name = fsck_get_object_name(options, oid); |
a59cfb32 JK |
352 | |
353 | buf = bufs + b; | |
354 | b = (b + 1) % ARRAY_SIZE(bufs); | |
355 | strbuf_reset(buf); | |
73390290 | 356 | strbuf_addstr(buf, oid_to_hex(oid)); |
a59cfb32 JK |
357 | if (name) |
358 | strbuf_addf(buf, " (%s)", name); | |
90cf590f | 359 | |
a59cfb32 | 360 | return buf->buf; |
90cf590f JS |
361 | } |
362 | ||
22410549 | 363 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) |
355885d5 MK |
364 | { |
365 | struct tree_desc desc; | |
366 | struct name_entry entry; | |
367 | int res = 0; | |
7b35efd7 | 368 | const char *name; |
355885d5 MK |
369 | |
370 | if (parse_tree(tree)) | |
371 | return -1; | |
372 | ||
73390290 | 373 | name = fsck_get_object_name(options, &tree->object.oid); |
8354fa3d DT |
374 | if (init_tree_desc_gently(&desc, tree->buffer, tree->size)) |
375 | return -1; | |
376 | while (tree_entry_gently(&desc, &entry)) { | |
7b35efd7 | 377 | struct object *obj; |
355885d5 MK |
378 | int result; |
379 | ||
380 | if (S_ISGITLINK(entry.mode)) | |
381 | continue; | |
7b35efd7 JS |
382 | |
383 | if (S_ISDIR(entry.mode)) { | |
ea82b2a0 | 384 | obj = (struct object *)lookup_tree(the_repository, &entry.oid); |
2720f6db | 385 | if (name && obj) |
73390290 | 386 | fsck_put_object_name(options, &entry.oid, "%s%s/", |
a59cfb32 | 387 | name, entry.path); |
7b35efd7 JS |
388 | result = options->walk(obj, OBJ_TREE, data, options); |
389 | } | |
390 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) { | |
ea82b2a0 | 391 | obj = (struct object *)lookup_blob(the_repository, &entry.oid); |
2720f6db | 392 | if (name && obj) |
73390290 | 393 | fsck_put_object_name(options, &entry.oid, "%s%s", |
a59cfb32 | 394 | name, entry.path); |
7b35efd7 JS |
395 | result = options->walk(obj, OBJ_BLOB, data, options); |
396 | } | |
355885d5 | 397 | else { |
82247e9b | 398 | result = error("in tree %s: entry %s has bad mode %.6o", |
73390290 | 399 | fsck_describe_object(options, &tree->object.oid), |
a59cfb32 | 400 | entry.path, entry.mode); |
355885d5 MK |
401 | } |
402 | if (result < 0) | |
403 | return result; | |
404 | if (!res) | |
405 | res = result; | |
406 | } | |
407 | return res; | |
408 | } | |
409 | ||
22410549 | 410 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) |
355885d5 | 411 | { |
7b35efd7 | 412 | int counter = 0, generation = 0, name_prefix_len = 0; |
355885d5 MK |
413 | struct commit_list *parents; |
414 | int res; | |
415 | int result; | |
7b35efd7 | 416 | const char *name; |
355885d5 MK |
417 | |
418 | if (parse_commit(commit)) | |
419 | return -1; | |
420 | ||
73390290 | 421 | name = fsck_get_object_name(options, &commit->object.oid); |
7b35efd7 | 422 | if (name) |
73390290 | 423 | fsck_put_object_name(options, get_commit_tree_oid(commit), |
a59cfb32 | 424 | "%s:", name); |
7b35efd7 | 425 | |
2e27bd77 DS |
426 | result = options->walk((struct object *)get_commit_tree(commit), |
427 | OBJ_TREE, data, options); | |
355885d5 MK |
428 | if (result < 0) |
429 | return result; | |
430 | res = result; | |
431 | ||
432 | parents = commit->parents; | |
7b35efd7 JS |
433 | if (name && parents) { |
434 | int len = strlen(name), power; | |
435 | ||
436 | if (len && name[len - 1] == '^') { | |
437 | generation = 1; | |
438 | name_prefix_len = len - 1; | |
439 | } | |
440 | else { /* parse ~<generation> suffix */ | |
441 | for (generation = 0, power = 1; | |
442 | len && isdigit(name[len - 1]); | |
443 | power *= 10) | |
444 | generation += power * (name[--len] - '0'); | |
445 | if (power > 1 && len && name[len - 1] == '~') | |
446 | name_prefix_len = len - 1; | |
e89f8936 JS |
447 | else { |
448 | /* Maybe a non-first parent, e.g. HEAD^2 */ | |
449 | generation = 0; | |
450 | name_prefix_len = len; | |
451 | } | |
7b35efd7 JS |
452 | } |
453 | } | |
454 | ||
355885d5 | 455 | while (parents) { |
7b35efd7 | 456 | if (name) { |
73390290 | 457 | struct object_id *oid = &parents->item->object.oid; |
7b35efd7 | 458 | |
b84c7838 | 459 | if (counter++) |
73390290 | 460 | fsck_put_object_name(options, oid, "%s^%d", |
a59cfb32 | 461 | name, counter); |
7b35efd7 | 462 | else if (generation > 0) |
73390290 | 463 | fsck_put_object_name(options, oid, "%.*s~%d", |
a59cfb32 JK |
464 | name_prefix_len, name, |
465 | generation + 1); | |
7b35efd7 | 466 | else |
73390290 | 467 | fsck_put_object_name(options, oid, "%s^", name); |
7b35efd7 | 468 | } |
22410549 | 469 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
355885d5 MK |
470 | if (result < 0) |
471 | return result; | |
472 | if (!res) | |
473 | res = result; | |
474 | parents = parents->next; | |
475 | } | |
476 | return res; | |
477 | } | |
478 | ||
22410549 | 479 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) |
355885d5 | 480 | { |
73390290 | 481 | const char *name = fsck_get_object_name(options, &tag->object.oid); |
7b35efd7 | 482 | |
355885d5 MK |
483 | if (parse_tag(tag)) |
484 | return -1; | |
7b35efd7 | 485 | if (name) |
73390290 | 486 | fsck_put_object_name(options, &tag->tagged->oid, "%s", name); |
22410549 | 487 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
355885d5 MK |
488 | } |
489 | ||
22410549 | 490 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
355885d5 MK |
491 | { |
492 | if (!obj) | |
493 | return -1; | |
a2b22854 JK |
494 | |
495 | if (obj->type == OBJ_NONE) | |
109cd76d | 496 | parse_object(the_repository, &obj->oid); |
a2b22854 | 497 | |
355885d5 MK |
498 | switch (obj->type) { |
499 | case OBJ_BLOB: | |
500 | return 0; | |
501 | case OBJ_TREE: | |
22410549 | 502 | return fsck_walk_tree((struct tree *)obj, data, options); |
355885d5 | 503 | case OBJ_COMMIT: |
22410549 | 504 | return fsck_walk_commit((struct commit *)obj, data, options); |
355885d5 | 505 | case OBJ_TAG: |
22410549 | 506 | return fsck_walk_tag((struct tag *)obj, data, options); |
355885d5 | 507 | default: |
a59cfb32 | 508 | error("Unknown object type for %s", |
73390290 | 509 | fsck_describe_object(options, &obj->oid)); |
355885d5 MK |
510 | return -1; |
511 | } | |
512 | } | |
ba002f3b | 513 | |
9068cfb2 RS |
514 | struct name_stack { |
515 | const char **names; | |
516 | size_t nr, alloc; | |
517 | }; | |
518 | ||
519 | static void name_stack_push(struct name_stack *stack, const char *name) | |
520 | { | |
521 | ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc); | |
522 | stack->names[stack->nr++] = name; | |
523 | } | |
524 | ||
525 | static const char *name_stack_pop(struct name_stack *stack) | |
526 | { | |
527 | return stack->nr ? stack->names[--stack->nr] : NULL; | |
528 | } | |
529 | ||
530 | static void name_stack_clear(struct name_stack *stack) | |
531 | { | |
532 | FREE_AND_NULL(stack->names); | |
533 | stack->nr = stack->alloc = 0; | |
534 | } | |
535 | ||
ba002f3b MK |
536 | /* |
537 | * The entries in a tree are ordered in the _path_ order, | |
538 | * which means that a directory entry is ordered by adding | |
539 | * a slash to the end of it. | |
540 | * | |
541 | * So a directory called "a" is ordered _after_ a file | |
542 | * called "a.c", because "a/" sorts after "a.c". | |
543 | */ | |
544 | #define TREE_UNORDERED (-1) | |
545 | #define TREE_HAS_DUPS (-2) | |
546 | ||
9068cfb2 RS |
547 | static int is_less_than_slash(unsigned char c) |
548 | { | |
549 | return '\0' < c && c < '/'; | |
550 | } | |
551 | ||
552 | static int verify_ordered(unsigned mode1, const char *name1, | |
553 | unsigned mode2, const char *name2, | |
554 | struct name_stack *candidates) | |
ba002f3b MK |
555 | { |
556 | int len1 = strlen(name1); | |
557 | int len2 = strlen(name2); | |
558 | int len = len1 < len2 ? len1 : len2; | |
559 | unsigned char c1, c2; | |
560 | int cmp; | |
561 | ||
562 | cmp = memcmp(name1, name2, len); | |
563 | if (cmp < 0) | |
564 | return 0; | |
565 | if (cmp > 0) | |
566 | return TREE_UNORDERED; | |
567 | ||
568 | /* | |
569 | * Ok, the first <len> characters are the same. | |
570 | * Now we need to order the next one, but turn | |
571 | * a '\0' into a '/' for a directory entry. | |
572 | */ | |
573 | c1 = name1[len]; | |
574 | c2 = name2[len]; | |
575 | if (!c1 && !c2) | |
576 | /* | |
577 | * git-write-tree used to write out a nonsense tree that has | |
578 | * entries with the same name, one blob and one tree. Make | |
579 | * sure we do not have duplicate entries. | |
580 | */ | |
581 | return TREE_HAS_DUPS; | |
582 | if (!c1 && S_ISDIR(mode1)) | |
583 | c1 = '/'; | |
584 | if (!c2 && S_ISDIR(mode2)) | |
585 | c2 = '/'; | |
9068cfb2 RS |
586 | |
587 | /* | |
588 | * There can be non-consecutive duplicates due to the implicitly | |
86715592 | 589 | * added slash, e.g.: |
9068cfb2 RS |
590 | * |
591 | * foo | |
592 | * foo.bar | |
593 | * foo.bar.baz | |
594 | * foo.bar/ | |
595 | * foo/ | |
596 | * | |
597 | * Record non-directory candidates (like "foo" and "foo.bar" in | |
598 | * the example) on a stack and check directory candidates (like | |
599 | * foo/" and "foo.bar/") against that stack. | |
600 | */ | |
601 | if (!c1 && is_less_than_slash(c2)) { | |
602 | name_stack_push(candidates, name1); | |
603 | } else if (c2 == '/' && is_less_than_slash(c1)) { | |
604 | for (;;) { | |
605 | const char *p; | |
606 | const char *f_name = name_stack_pop(candidates); | |
607 | ||
608 | if (!f_name) | |
609 | break; | |
610 | if (!skip_prefix(name2, f_name, &p)) | |
fe747043 | 611 | continue; |
9068cfb2 RS |
612 | if (!*p) |
613 | return TREE_HAS_DUPS; | |
614 | if (is_less_than_slash(*p)) { | |
615 | name_stack_push(candidates, f_name); | |
616 | break; | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
ba002f3b MK |
621 | return c1 < c2 ? 0 : TREE_UNORDERED; |
622 | } | |
623 | ||
b2f2039c | 624 | static int fsck_tree(const struct object_id *oid, |
23a173a7 JK |
625 | const char *buffer, unsigned long size, |
626 | struct fsck_options *options) | |
ba002f3b | 627 | { |
8354fa3d | 628 | int retval = 0; |
c479d14a | 629 | int has_null_sha1 = 0; |
ba002f3b MK |
630 | int has_full_path = 0; |
631 | int has_empty_name = 0; | |
5d34a435 JK |
632 | int has_dot = 0; |
633 | int has_dotdot = 0; | |
5c17f512 | 634 | int has_dotgit = 0; |
ba002f3b MK |
635 | int has_zero_pad = 0; |
636 | int has_bad_modes = 0; | |
637 | int has_dup_entries = 0; | |
638 | int not_properly_sorted = 0; | |
639 | struct tree_desc desc; | |
640 | unsigned o_mode; | |
641 | const char *o_name; | |
9068cfb2 | 642 | struct name_stack df_dup_candidates = { NULL }; |
ba002f3b | 643 | |
23a173a7 | 644 | if (init_tree_desc_gently(&desc, buffer, size)) { |
b2f2039c | 645 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
8354fa3d DT |
646 | return retval; |
647 | } | |
ba002f3b MK |
648 | |
649 | o_mode = 0; | |
650 | o_name = NULL; | |
ba002f3b MK |
651 | |
652 | while (desc.size) { | |
5ec1e728 | 653 | unsigned short mode; |
288a74bc | 654 | const char *name, *backslash; |
ce6663a9 | 655 | const struct object_id *oid; |
ba002f3b | 656 | |
ce6663a9 | 657 | oid = tree_entry_extract(&desc, &name, &mode); |
ba002f3b | 658 | |
ce6663a9 | 659 | has_null_sha1 |= is_null_oid(oid); |
effd12ec HS |
660 | has_full_path |= !!strchr(name, '/'); |
661 | has_empty_name |= !*name; | |
662 | has_dot |= !strcmp(name, "."); | |
663 | has_dotdot |= !strcmp(name, ".."); | |
ed9c3220 | 664 | has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name); |
ba002f3b | 665 | has_zero_pad |= *(char *)desc.buffer == '0'; |
159e7b08 | 666 | |
b7b1fca1 JK |
667 | if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) { |
668 | if (!S_ISLNK(mode)) | |
669 | oidset_insert(&gitmodules_found, oid); | |
670 | else | |
38370253 | 671 | retval += report(options, |
b2f2039c | 672 | oid, OBJ_TREE, |
b7b1fca1 JK |
673 | FSCK_MSG_GITMODULES_SYMLINK, |
674 | ".gitmodules is a symbolic link"); | |
675 | } | |
159e7b08 | 676 | |
288a74bc JS |
677 | if ((backslash = strchr(name, '\\'))) { |
678 | while (backslash) { | |
679 | backslash++; | |
680 | has_dotgit |= is_ntfs_dotgit(backslash); | |
bdfef049 JS |
681 | if (is_ntfs_dotgitmodules(backslash)) { |
682 | if (!S_ISLNK(mode)) | |
683 | oidset_insert(&gitmodules_found, oid); | |
684 | else | |
7034cd09 | 685 | retval += report(options, oid, OBJ_TREE, |
bdfef049 JS |
686 | FSCK_MSG_GITMODULES_SYMLINK, |
687 | ".gitmodules is a symbolic link"); | |
688 | } | |
288a74bc JS |
689 | backslash = strchr(backslash, '\\'); |
690 | } | |
691 | } | |
692 | ||
8354fa3d | 693 | if (update_tree_entry_gently(&desc)) { |
b2f2039c | 694 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
8354fa3d DT |
695 | break; |
696 | } | |
ba002f3b MK |
697 | |
698 | switch (mode) { | |
699 | /* | |
700 | * Standard modes.. | |
701 | */ | |
702 | case S_IFREG | 0755: | |
703 | case S_IFREG | 0644: | |
704 | case S_IFLNK: | |
705 | case S_IFDIR: | |
706 | case S_IFGITLINK: | |
707 | break; | |
708 | /* | |
709 | * This is nonstandard, but we had a few of these | |
710 | * early on when we honored the full set of mode | |
711 | * bits.. | |
712 | */ | |
713 | case S_IFREG | 0664: | |
22410549 | 714 | if (!options->strict) |
ba002f3b | 715 | break; |
1cf01a34 | 716 | /* fallthrough */ |
ba002f3b MK |
717 | default: |
718 | has_bad_modes = 1; | |
719 | } | |
720 | ||
721 | if (o_name) { | |
9068cfb2 RS |
722 | switch (verify_ordered(o_mode, o_name, mode, name, |
723 | &df_dup_candidates)) { | |
ba002f3b MK |
724 | case TREE_UNORDERED: |
725 | not_properly_sorted = 1; | |
726 | break; | |
727 | case TREE_HAS_DUPS: | |
728 | has_dup_entries = 1; | |
729 | break; | |
730 | default: | |
731 | break; | |
732 | } | |
733 | } | |
734 | ||
735 | o_mode = mode; | |
736 | o_name = name; | |
ba002f3b MK |
737 | } |
738 | ||
9068cfb2 RS |
739 | name_stack_clear(&df_dup_candidates); |
740 | ||
c479d14a | 741 | if (has_null_sha1) |
b2f2039c | 742 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); |
ba002f3b | 743 | if (has_full_path) |
b2f2039c | 744 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); |
ba002f3b | 745 | if (has_empty_name) |
b2f2039c | 746 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); |
5d34a435 | 747 | if (has_dot) |
b2f2039c | 748 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'"); |
5d34a435 | 749 | if (has_dotdot) |
b2f2039c | 750 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
5c17f512 | 751 | if (has_dotgit) |
b2f2039c | 752 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
ba002f3b | 753 | if (has_zero_pad) |
b2f2039c | 754 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); |
ba002f3b | 755 | if (has_bad_modes) |
b2f2039c | 756 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); |
ba002f3b | 757 | if (has_dup_entries) |
b2f2039c | 758 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); |
ba002f3b | 759 | if (not_properly_sorted) |
b2f2039c | 760 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); |
ba002f3b MK |
761 | return retval; |
762 | } | |
763 | ||
84d18c0b | 764 | static int verify_headers(const void *data, unsigned long size, |
cc579000 JK |
765 | const struct object_id *oid, enum object_type type, |
766 | struct fsck_options *options) | |
4d0d8975 JS |
767 | { |
768 | const char *buffer = (const char *)data; | |
769 | unsigned long i; | |
770 | ||
771 | for (i = 0; i < size; i++) { | |
772 | switch (buffer[i]) { | |
773 | case '\0': | |
cc579000 | 774 | return report(options, oid, type, |
c99ba492 JS |
775 | FSCK_MSG_NUL_IN_HEADER, |
776 | "unterminated header: NUL at offset %ld", i); | |
4d0d8975 JS |
777 | case '\n': |
778 | if (i + 1 < size && buffer[i + 1] == '\n') | |
779 | return 0; | |
780 | } | |
781 | } | |
782 | ||
84d18c0b JH |
783 | /* |
784 | * We did not find double-LF that separates the header | |
785 | * and the body. Not having a body is not a crime but | |
786 | * we do want to see the terminating LF for the last header | |
787 | * line. | |
788 | */ | |
789 | if (size && buffer[size - 1] == '\n') | |
790 | return 0; | |
791 | ||
cc579000 | 792 | return report(options, oid, type, |
c99ba492 | 793 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); |
4d0d8975 JS |
794 | } |
795 | ||
78543993 JK |
796 | static int fsck_ident(const char **ident, |
797 | const struct object_id *oid, enum object_type type, | |
798 | struct fsck_options *options) | |
daae1922 | 799 | { |
e6826e33 | 800 | const char *p = *ident; |
d4b8de04 JK |
801 | char *end; |
802 | ||
e6826e33 JS |
803 | *ident = strchrnul(*ident, '\n'); |
804 | if (**ident == '\n') | |
805 | (*ident)++; | |
806 | ||
807 | if (*p == '<') | |
78543993 | 808 | return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
809 | p += strcspn(p, "<>\n"); |
810 | if (*p == '>') | |
78543993 | 811 | return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); |
e6826e33 | 812 | if (*p != '<') |
78543993 | 813 | return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); |
e6826e33 | 814 | if (p[-1] != ' ') |
78543993 | 815 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
816 | p++; |
817 | p += strcspn(p, "<>\n"); | |
818 | if (*p != '>') | |
78543993 | 819 | return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); |
e6826e33 JS |
820 | p++; |
821 | if (*p != ' ') | |
78543993 | 822 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); |
e6826e33 JS |
823 | p++; |
824 | if (*p == '0' && p[1] != ' ') | |
78543993 | 825 | return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); |
1aeb7e75 | 826 | if (date_overflows(parse_timestamp(p, &end, 10))) |
78543993 | 827 | return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); |
e6826e33 | 828 | if ((end == p || *end != ' ')) |
78543993 | 829 | return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); |
e6826e33 JS |
830 | p = end + 1; |
831 | if ((*p != '+' && *p != '-') || | |
832 | !isdigit(p[1]) || | |
833 | !isdigit(p[2]) || | |
834 | !isdigit(p[3]) || | |
835 | !isdigit(p[4]) || | |
836 | (p[5] != '\n')) | |
78543993 | 837 | return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); |
e6826e33 | 838 | p += 6; |
daae1922 JN |
839 | return 0; |
840 | } | |
841 | ||
c5b4269b JK |
842 | static int fsck_commit(const struct object_id *oid, |
843 | const char *buffer, unsigned long size, | |
844 | struct fsck_options *options) | |
ba002f3b | 845 | { |
f648ee70 | 846 | struct object_id tree_oid, parent_oid; |
ec652315 | 847 | unsigned author_count; |
daae1922 | 848 | int err; |
6d2d780f | 849 | const char *buffer_begin = buffer; |
c54f5ca9 | 850 | const char *p; |
ba002f3b | 851 | |
c5b4269b | 852 | if (verify_headers(buffer, size, oid, OBJ_COMMIT, options)) |
4d0d8975 JS |
853 | return -1; |
854 | ||
cf4fff57 | 855 | if (!skip_prefix(buffer, "tree ", &buffer)) |
c5b4269b | 856 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); |
c54f5ca9 | 857 | if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') { |
c5b4269b | 858 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); |
b3584761 JS |
859 | if (err) |
860 | return err; | |
861 | } | |
c54f5ca9 | 862 | buffer = p + 1; |
cf4fff57 | 863 | while (skip_prefix(buffer, "parent ", &buffer)) { |
f648ee70 | 864 | if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') { |
c5b4269b | 865 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); |
b3584761 JS |
866 | if (err) |
867 | return err; | |
868 | } | |
c54f5ca9 | 869 | buffer = p + 1; |
ba002f3b | 870 | } |
c9ad147f JS |
871 | author_count = 0; |
872 | while (skip_prefix(buffer, "author ", &buffer)) { | |
873 | author_count++; | |
c5b4269b | 874 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); |
c9ad147f JS |
875 | if (err) |
876 | return err; | |
ba002f3b | 877 | } |
c9ad147f | 878 | if (author_count < 1) |
c5b4269b | 879 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); |
c9ad147f | 880 | else if (author_count > 1) |
c5b4269b | 881 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines"); |
daae1922 JN |
882 | if (err) |
883 | return err; | |
cf4fff57 | 884 | if (!skip_prefix(buffer, "committer ", &buffer)) |
c5b4269b JK |
885 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); |
886 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); | |
daae1922 JN |
887 | if (err) |
888 | return err; | |
6d2d780f | 889 | if (memchr(buffer_begin, '\0', size)) { |
c5b4269b | 890 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT, |
6d2d780f JH |
891 | "NUL byte in the commit object body"); |
892 | if (err) | |
893 | return err; | |
894 | } | |
ba002f3b MK |
895 | return 0; |
896 | } | |
897 | ||
103fb6d4 | 898 | static int fsck_tag(const struct object_id *oid, const char *buffer, |
2175a0c6 | 899 | unsigned long size, struct fsck_options *options) |
cec097be | 900 | { |
f648ee70 | 901 | struct object_id tagged_oid; |
acf9de4c ÆAB |
902 | int tagged_type; |
903 | return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid, | |
904 | &tagged_type); | |
905 | } | |
906 | ||
907 | int fsck_tag_standalone(const struct object_id *oid, const char *buffer, | |
908 | unsigned long size, struct fsck_options *options, | |
909 | struct object_id *tagged_oid, | |
910 | int *tagged_type) | |
911 | { | |
cec097be | 912 | int ret = 0; |
23a173a7 | 913 | char *eol; |
cec097be | 914 | struct strbuf sb = STRBUF_INIT; |
c54f5ca9 | 915 | const char *p; |
cec097be | 916 | |
103fb6d4 | 917 | ret = verify_headers(buffer, size, oid, OBJ_TAG, options); |
8a272f29 | 918 | if (ret) |
cec097be JS |
919 | goto done; |
920 | ||
921 | if (!skip_prefix(buffer, "object ", &buffer)) { | |
103fb6d4 | 922 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); |
cec097be JS |
923 | goto done; |
924 | } | |
acf9de4c | 925 | if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') { |
103fb6d4 | 926 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); |
7d7d5b05 JS |
927 | if (ret) |
928 | goto done; | |
cec097be | 929 | } |
c54f5ca9 | 930 | buffer = p + 1; |
cec097be JS |
931 | |
932 | if (!skip_prefix(buffer, "type ", &buffer)) { | |
103fb6d4 | 933 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); |
cec097be JS |
934 | goto done; |
935 | } | |
936 | eol = strchr(buffer, '\n'); | |
937 | if (!eol) { | |
103fb6d4 | 938 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
939 | goto done; |
940 | } | |
acf9de4c ÆAB |
941 | *tagged_type = type_from_string_gently(buffer, eol - buffer, 1); |
942 | if (*tagged_type < 0) | |
103fb6d4 | 943 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); |
cec097be JS |
944 | if (ret) |
945 | goto done; | |
946 | buffer = eol + 1; | |
947 | ||
948 | if (!skip_prefix(buffer, "tag ", &buffer)) { | |
103fb6d4 | 949 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); |
cec097be JS |
950 | goto done; |
951 | } | |
952 | eol = strchr(buffer, '\n'); | |
953 | if (!eol) { | |
103fb6d4 | 954 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
955 | goto done; |
956 | } | |
957 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); | |
f27d05b1 | 958 | if (check_refname_format(sb.buf, 0)) { |
103fb6d4 | 959 | ret = report(options, oid, OBJ_TAG, |
38370253 JK |
960 | FSCK_MSG_BAD_TAG_NAME, |
961 | "invalid 'tag' name: %.*s", | |
962 | (int)(eol - buffer), buffer); | |
f27d05b1 JS |
963 | if (ret) |
964 | goto done; | |
965 | } | |
cec097be JS |
966 | buffer = eol + 1; |
967 | ||
f27d05b1 | 968 | if (!skip_prefix(buffer, "tagger ", &buffer)) { |
cec097be | 969 | /* early tags do not contain 'tagger' lines; warn only */ |
103fb6d4 | 970 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); |
f27d05b1 JS |
971 | if (ret) |
972 | goto done; | |
973 | } | |
cec097be | 974 | else |
103fb6d4 | 975 | ret = fsck_ident(&buffer, oid, OBJ_TAG, options); |
9a1a3a4d ÆAB |
976 | if (!*buffer) |
977 | goto done; | |
cec097be | 978 | |
acf9de4c ÆAB |
979 | if (!starts_with(buffer, "\n")) { |
980 | /* | |
981 | * The verify_headers() check will allow | |
982 | * e.g. "[...]tagger <tagger>\nsome | |
983 | * garbage\n\nmessage" to pass, thinking "some | |
984 | * garbage" could be a custom header. E.g. "mktag" | |
985 | * doesn't want any unknown headers. | |
986 | */ | |
987 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'"); | |
988 | if (ret) | |
989 | goto done; | |
990 | } | |
991 | ||
cec097be JS |
992 | done: |
993 | strbuf_release(&sb); | |
cec097be JS |
994 | return ret; |
995 | } | |
996 | ||
a2b26ffb JN |
997 | /* |
998 | * Like builtin/submodule--helper.c's starts_with_dot_slash, but without | |
999 | * relying on the platform-dependent is_dir_sep helper. | |
1000 | * | |
1001 | * This is for use in checking whether a submodule URL is interpreted as | |
1002 | * relative to the current directory on any platform, since \ is a | |
1003 | * directory separator on Windows but not on other platforms. | |
1004 | */ | |
1005 | static int starts_with_dot_slash(const char *str) | |
1006 | { | |
1007 | return str[0] == '.' && (str[1] == '/' || str[1] == '\\'); | |
1008 | } | |
1009 | ||
1010 | /* | |
1011 | * Like starts_with_dot_slash, this is a variant of submodule--helper's | |
1012 | * helper of the same name with the twist that it accepts backslash as a | |
1013 | * directory separator even on non-Windows platforms. | |
1014 | */ | |
1015 | static int starts_with_dot_dot_slash(const char *str) | |
1016 | { | |
1017 | return str[0] == '.' && starts_with_dot_slash(str + 1); | |
1018 | } | |
1019 | ||
1020 | static int submodule_url_is_relative(const char *url) | |
1021 | { | |
1022 | return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url); | |
1023 | } | |
1024 | ||
c44088ec JN |
1025 | /* |
1026 | * Count directory components that a relative submodule URL should chop | |
1027 | * from the remote_url it is to be resolved against. | |
1028 | * | |
1029 | * In other words, this counts "../" components at the start of a | |
1030 | * submodule URL. | |
1031 | * | |
1032 | * Returns the number of directory components to chop and writes a | |
1033 | * pointer to the next character of url after all leading "./" and | |
1034 | * "../" components to out. | |
1035 | */ | |
1036 | static int count_leading_dotdots(const char *url, const char **out) | |
1037 | { | |
1038 | int result = 0; | |
1039 | while (1) { | |
1040 | if (starts_with_dot_dot_slash(url)) { | |
1041 | result++; | |
1042 | url += strlen("../"); | |
1043 | continue; | |
1044 | } | |
1045 | if (starts_with_dot_slash(url)) { | |
1046 | url += strlen("./"); | |
1047 | continue; | |
1048 | } | |
1049 | *out = url; | |
1050 | return result; | |
1051 | } | |
1052 | } | |
a2b26ffb JN |
1053 | /* |
1054 | * Check whether a transport is implemented by git-remote-curl. | |
1055 | * | |
1056 | * If it is, returns 1 and writes the URL that would be passed to | |
1057 | * git-remote-curl to the "out" parameter. | |
1058 | * | |
1059 | * Otherwise, returns 0 and leaves "out" untouched. | |
1060 | * | |
1061 | * Examples: | |
1062 | * http::https://example.com/repo.git -> 1, https://example.com/repo.git | |
1063 | * https://example.com/repo.git -> 1, https://example.com/repo.git | |
1064 | * git://example.com/repo.git -> 0 | |
1065 | * | |
1066 | * This is for use in checking for previously exploitable bugs that | |
1067 | * required a submodule URL to be passed to git-remote-curl. | |
1068 | */ | |
1069 | static int url_to_curl_url(const char *url, const char **out) | |
1070 | { | |
1071 | /* | |
1072 | * We don't need to check for case-aliases, "http.exe", and so | |
1073 | * on because in the default configuration, is_transport_allowed | |
1074 | * prevents URLs with those schemes from being cloned | |
1075 | * automatically. | |
1076 | */ | |
1077 | if (skip_prefix(url, "http::", out) || | |
1078 | skip_prefix(url, "https::", out) || | |
1079 | skip_prefix(url, "ftp::", out) || | |
1080 | skip_prefix(url, "ftps::", out)) | |
1081 | return 1; | |
1082 | if (starts_with(url, "http://") || | |
1083 | starts_with(url, "https://") || | |
1084 | starts_with(url, "ftp://") || | |
1085 | starts_with(url, "ftps://")) { | |
1086 | *out = url; | |
1087 | return 1; | |
1088 | } | |
1089 | return 0; | |
1090 | } | |
1091 | ||
07259e74 JK |
1092 | static int check_submodule_url(const char *url) |
1093 | { | |
a2b26ffb | 1094 | const char *curl_url; |
07259e74 JK |
1095 | |
1096 | if (looks_like_command_line_option(url)) | |
1097 | return -1; | |
1098 | ||
6aed5673 | 1099 | if (submodule_url_is_relative(url) || starts_with(url, "git://")) { |
c44088ec JN |
1100 | char *decoded; |
1101 | const char *next; | |
1102 | int has_nl; | |
1103 | ||
a2b26ffb JN |
1104 | /* |
1105 | * This could be appended to an http URL and url-decoded; | |
1106 | * check for malicious characters. | |
1107 | */ | |
c44088ec JN |
1108 | decoded = url_decode(url); |
1109 | has_nl = !!strchr(decoded, '\n'); | |
1110 | ||
a2b26ffb JN |
1111 | free(decoded); |
1112 | if (has_nl) | |
1113 | return -1; | |
c44088ec JN |
1114 | |
1115 | /* | |
1116 | * URLs which escape their root via "../" can overwrite | |
1117 | * the host field and previous components, resolving to | |
1a3609e4 JN |
1118 | * URLs like https::example.com/submodule.git and |
1119 | * https:///example.com/submodule.git that were | |
c44088ec JN |
1120 | * susceptible to CVE-2020-11008. |
1121 | */ | |
1122 | if (count_leading_dotdots(url, &next) > 0 && | |
1a3609e4 | 1123 | (*next == ':' || *next == '/')) |
c44088ec | 1124 | return -1; |
a2b26ffb JN |
1125 | } |
1126 | ||
1127 | else if (url_to_curl_url(url, &curl_url)) { | |
1128 | struct credential c = CREDENTIAL_INIT; | |
1a3609e4 JN |
1129 | int ret = 0; |
1130 | if (credential_from_url_gently(&c, curl_url, 1) || | |
1131 | !*c.host) | |
1132 | ret = -1; | |
a2b26ffb JN |
1133 | credential_clear(&c); |
1134 | return ret; | |
1135 | } | |
1136 | ||
1137 | return 0; | |
07259e74 JK |
1138 | } |
1139 | ||
ed8b10f6 | 1140 | struct fsck_gitmodules_data { |
6da40b22 | 1141 | const struct object_id *oid; |
ed8b10f6 JK |
1142 | struct fsck_options *options; |
1143 | int ret; | |
1144 | }; | |
1145 | ||
1146 | static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata) | |
1147 | { | |
1148 | struct fsck_gitmodules_data *data = vdata; | |
1149 | const char *subsection, *key; | |
f5914f4b | 1150 | size_t subsection_len; |
ed8b10f6 JK |
1151 | char *name; |
1152 | ||
1153 | if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 || | |
1154 | !subsection) | |
1155 | return 0; | |
1156 | ||
1157 | name = xmemdupz(subsection, subsection_len); | |
1158 | if (check_submodule_name(name) < 0) | |
38370253 | 1159 | data->ret |= report(data->options, |
6da40b22 | 1160 | data->oid, OBJ_BLOB, |
ed8b10f6 JK |
1161 | FSCK_MSG_GITMODULES_NAME, |
1162 | "disallowed submodule name: %s", | |
1163 | name); | |
a124133e | 1164 | if (!strcmp(key, "url") && value && |
07259e74 | 1165 | check_submodule_url(value) < 0) |
38370253 | 1166 | data->ret |= report(data->options, |
6da40b22 | 1167 | data->oid, OBJ_BLOB, |
a124133e JK |
1168 | FSCK_MSG_GITMODULES_URL, |
1169 | "disallowed submodule url: %s", | |
1170 | value); | |
1a7fd1fb JK |
1171 | if (!strcmp(key, "path") && value && |
1172 | looks_like_command_line_option(value)) | |
38370253 | 1173 | data->ret |= report(data->options, |
6da40b22 | 1174 | data->oid, OBJ_BLOB, |
1a7fd1fb JK |
1175 | FSCK_MSG_GITMODULES_PATH, |
1176 | "disallowed submodule path: %s", | |
1177 | value); | |
bb92255e JN |
1178 | if (!strcmp(key, "update") && value && |
1179 | parse_submodule_update_type(value) == SM_UPDATE_COMMAND) | |
7034cd09 | 1180 | data->ret |= report(data->options, data->oid, OBJ_BLOB, |
bb92255e JN |
1181 | FSCK_MSG_GITMODULES_UPDATE, |
1182 | "disallowed submodule update setting: %s", | |
1183 | value); | |
ed8b10f6 JK |
1184 | free(name); |
1185 | ||
1186 | return 0; | |
1187 | } | |
1188 | ||
6da40b22 | 1189 | static int fsck_blob(const struct object_id *oid, const char *buf, |
7ac4f3a0 JK |
1190 | unsigned long size, struct fsck_options *options) |
1191 | { | |
ed8b10f6 | 1192 | struct fsck_gitmodules_data data; |
de6bd9e3 | 1193 | struct config_options config_opts = { 0 }; |
ed8b10f6 | 1194 | |
6da40b22 | 1195 | if (!oidset_contains(&gitmodules_found, oid)) |
ed8b10f6 | 1196 | return 0; |
6da40b22 | 1197 | oidset_insert(&gitmodules_done, oid); |
ed8b10f6 | 1198 | |
6da40b22 | 1199 | if (object_on_skiplist(options, oid)) |
fb162877 RJ |
1200 | return 0; |
1201 | ||
ed8b10f6 JK |
1202 | if (!buf) { |
1203 | /* | |
1204 | * A missing buffer here is a sign that the caller found the | |
1205 | * blob too gigantic to load into memory. Let's just consider | |
1206 | * that an error. | |
1207 | */ | |
6da40b22 | 1208 | return report(options, oid, OBJ_BLOB, |
0d68764d | 1209 | FSCK_MSG_GITMODULES_LARGE, |
ed8b10f6 JK |
1210 | ".gitmodules too large to parse"); |
1211 | } | |
1212 | ||
6da40b22 | 1213 | data.oid = oid; |
ed8b10f6 JK |
1214 | data.options = options; |
1215 | data.ret = 0; | |
de6bd9e3 | 1216 | config_opts.error_action = CONFIG_ERROR_SILENT; |
ed8b10f6 | 1217 | if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB, |
de6bd9e3 | 1218 | ".gitmodules", buf, size, &data, &config_opts)) |
6da40b22 | 1219 | data.ret |= report(options, oid, OBJ_BLOB, |
ed8b10f6 JK |
1220 | FSCK_MSG_GITMODULES_PARSE, |
1221 | "could not parse gitmodules blob"); | |
1222 | ||
1223 | return data.ret; | |
7ac4f3a0 JK |
1224 | } |
1225 | ||
90a398bb | 1226 | int fsck_object(struct object *obj, void *data, unsigned long size, |
22410549 | 1227 | struct fsck_options *options) |
ba002f3b MK |
1228 | { |
1229 | if (!obj) | |
38370253 | 1230 | return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); |
ba002f3b MK |
1231 | |
1232 | if (obj->type == OBJ_BLOB) | |
6da40b22 | 1233 | return fsck_blob(&obj->oid, data, size, options); |
ba002f3b | 1234 | if (obj->type == OBJ_TREE) |
b2f2039c | 1235 | return fsck_tree(&obj->oid, data, size, options); |
ba002f3b | 1236 | if (obj->type == OBJ_COMMIT) |
c5b4269b | 1237 | return fsck_commit(&obj->oid, data, size, options); |
ba002f3b | 1238 | if (obj->type == OBJ_TAG) |
103fb6d4 | 1239 | return fsck_tag(&obj->oid, data, size, options); |
ba002f3b | 1240 | |
38370253 JK |
1241 | return report(options, &obj->oid, obj->type, |
1242 | FSCK_MSG_UNKNOWN_TYPE, | |
1243 | "unknown type '%d' (internal fsck error)", | |
1244 | obj->type); | |
ba002f3b | 1245 | } |
d6ffc8d7 | 1246 | |
1cd772cc | 1247 | int fsck_error_function(struct fsck_options *o, |
5afc4b1d JK |
1248 | const struct object_id *oid, |
1249 | enum object_type object_type, | |
1250 | int msg_type, const char *message) | |
d6ffc8d7 | 1251 | { |
0282f4dc | 1252 | if (msg_type == FSCK_WARN) { |
5afc4b1d | 1253 | warning("object %s: %s", fsck_describe_object(o, oid), message); |
0282f4dc JS |
1254 | return 0; |
1255 | } | |
5afc4b1d | 1256 | error("object %s: %s", fsck_describe_object(o, oid), message); |
d6ffc8d7 MK |
1257 | return 1; |
1258 | } | |
159e7b08 | 1259 | |
5476e1ef JT |
1260 | void register_found_gitmodules(const struct object_id *oid) |
1261 | { | |
1262 | oidset_insert(&gitmodules_found, oid); | |
1263 | } | |
1264 | ||
159e7b08 JK |
1265 | int fsck_finish(struct fsck_options *options) |
1266 | { | |
1267 | int ret = 0; | |
1268 | struct oidset_iter iter; | |
1269 | const struct object_id *oid; | |
1270 | ||
1271 | oidset_iter_init(&gitmodules_found, &iter); | |
1272 | while ((oid = oidset_iter_next(&iter))) { | |
159e7b08 JK |
1273 | enum object_type type; |
1274 | unsigned long size; | |
1275 | char *buf; | |
1276 | ||
1277 | if (oidset_contains(&gitmodules_done, oid)) | |
1278 | continue; | |
1279 | ||
7913f53b | 1280 | buf = read_object_file(oid, &type, &size); |
159e7b08 | 1281 | if (!buf) { |
b8b00f16 | 1282 | if (is_promisor_object(oid)) |
27387444 | 1283 | continue; |
38370253 | 1284 | ret |= report(options, |
b8b00f16 | 1285 | oid, OBJ_BLOB, |
159e7b08 JK |
1286 | FSCK_MSG_GITMODULES_MISSING, |
1287 | "unable to read .gitmodules blob"); | |
1288 | continue; | |
1289 | } | |
1290 | ||
1291 | if (type == OBJ_BLOB) | |
b8b00f16 | 1292 | ret |= fsck_blob(oid, buf, size, options); |
159e7b08 | 1293 | else |
38370253 | 1294 | ret |= report(options, |
b8b00f16 | 1295 | oid, type, |
159e7b08 JK |
1296 | FSCK_MSG_GITMODULES_BLOB, |
1297 | "non-blob found at .gitmodules"); | |
1298 | free(buf); | |
1299 | } | |
1300 | ||
1301 | ||
1302 | oidset_clear(&gitmodules_found); | |
1303 | oidset_clear(&gitmodules_done); | |
1304 | return ret; | |
1305 | } | |
1f3299fd | 1306 | |
fb79f5bf | 1307 | int git_fsck_config(const char *var, const char *value, void *cb) |
1f3299fd | 1308 | { |
fb79f5bf | 1309 | struct fsck_options *options = cb; |
1f3299fd ÆAB |
1310 | if (strcmp(var, "fsck.skiplist") == 0) { |
1311 | const char *path; | |
1312 | struct strbuf sb = STRBUF_INIT; | |
1313 | ||
1314 | if (git_config_pathname(&path, var, value)) | |
1315 | return 1; | |
1316 | strbuf_addf(&sb, "skiplist=%s", path); | |
1317 | free((char *)path); | |
1318 | fsck_set_msg_types(options, sb.buf); | |
1319 | strbuf_release(&sb); | |
1320 | return 0; | |
1321 | } | |
1322 | ||
1323 | if (skip_prefix(var, "fsck.", &var)) { | |
1324 | fsck_set_msg_type(options, var, value); | |
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | return git_default_config(var, value, cb); | |
1329 | } |