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