Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / fsck.c
blob871c0a9a2522cdf1abd16ceef4d224d12fcb7d2e
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "hex.h"
4 #include "object-store.h"
5 #include "repository.h"
6 #include "object.h"
7 #include "attr.h"
8 #include "blob.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "commit.h"
12 #include "tag.h"
13 #include "fsck.h"
14 #include "refs.h"
15 #include "url.h"
16 #include "utf8.h"
17 #include "decorate.h"
18 #include "oidset.h"
19 #include "packfile.h"
20 #include "submodule-config.h"
21 #include "config.h"
22 #include "credential.h"
23 #include "help.h"
25 #define STR(x) #x
26 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
27 static struct {
28 const char *id_string;
29 const char *downcased;
30 const char *camelcased;
31 enum fsck_msg_type msg_type;
32 } msg_id_info[FSCK_MSG_MAX + 1] = {
33 FOREACH_FSCK_MSG_ID(MSG_ID)
34 { NULL, NULL, NULL, -1 }
36 #undef MSG_ID
37 #undef STR
39 static void prepare_msg_ids(void)
41 int i;
43 if (msg_id_info[0].downcased)
44 return;
46 /* convert id_string to lower case, without underscores. */
47 for (i = 0; i < FSCK_MSG_MAX; i++) {
48 const char *p = msg_id_info[i].id_string;
49 int len = strlen(p);
50 char *q = xmalloc(len);
52 msg_id_info[i].downcased = q;
53 while (*p)
54 if (*p == '_')
55 p++;
56 else
57 *(q)++ = tolower(*(p)++);
58 *q = '\0';
60 p = msg_id_info[i].id_string;
61 q = xmalloc(len);
62 msg_id_info[i].camelcased = q;
63 while (*p) {
64 if (*p == '_') {
65 p++;
66 if (*p)
67 *q++ = *p++;
68 } else {
69 *q++ = tolower(*p++);
72 *q = '\0';
76 static int parse_msg_id(const char *text)
78 int i;
80 prepare_msg_ids();
82 for (i = 0; i < FSCK_MSG_MAX; i++)
83 if (!strcmp(text, msg_id_info[i].downcased))
84 return i;
86 return -1;
89 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
91 int i;
93 prepare_msg_ids();
95 for (i = 0; i < FSCK_MSG_MAX; i++)
96 list_config_item(list, prefix, msg_id_info[i].camelcased);
99 static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
100 struct fsck_options *options)
102 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
104 if (!options->msg_type) {
105 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
107 if (options->strict && msg_type == FSCK_WARN)
108 msg_type = FSCK_ERROR;
109 return msg_type;
112 return options->msg_type[msg_id];
115 static enum fsck_msg_type parse_msg_type(const char *str)
117 if (!strcmp(str, "error"))
118 return FSCK_ERROR;
119 else if (!strcmp(str, "warn"))
120 return FSCK_WARN;
121 else if (!strcmp(str, "ignore"))
122 return FSCK_IGNORE;
123 else
124 die("Unknown fsck message type: '%s'", str);
127 int is_valid_msg_type(const char *msg_id, const char *msg_type)
129 if (parse_msg_id(msg_id) < 0)
130 return 0;
131 parse_msg_type(msg_type);
132 return 1;
135 void fsck_set_msg_type_from_ids(struct fsck_options *options,
136 enum fsck_msg_id msg_id,
137 enum fsck_msg_type msg_type)
139 if (!options->msg_type) {
140 int i;
141 enum fsck_msg_type *severity;
142 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
143 for (i = 0; i < FSCK_MSG_MAX; i++)
144 severity[i] = fsck_msg_type(i, options);
145 options->msg_type = severity;
148 options->msg_type[msg_id] = msg_type;
151 void fsck_set_msg_type(struct fsck_options *options,
152 const char *msg_id_str, const char *msg_type_str)
154 int msg_id = parse_msg_id(msg_id_str);
155 enum fsck_msg_type msg_type = parse_msg_type(msg_type_str);
157 if (msg_id < 0)
158 die("Unhandled message id: %s", msg_id_str);
160 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
161 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
163 fsck_set_msg_type_from_ids(options, msg_id, msg_type);
166 void fsck_set_msg_types(struct fsck_options *options, const char *values)
168 char *buf = xstrdup(values), *to_free = buf;
169 int done = 0;
171 while (!done) {
172 int len = strcspn(buf, " ,|"), equal;
174 done = !buf[len];
175 if (!len) {
176 buf++;
177 continue;
179 buf[len] = '\0';
181 for (equal = 0;
182 equal < len && buf[equal] != '=' && buf[equal] != ':';
183 equal++)
184 buf[equal] = tolower(buf[equal]);
185 buf[equal] = '\0';
187 if (!strcmp(buf, "skiplist")) {
188 if (equal == len)
189 die("skiplist requires a path");
190 oidset_parse_file(&options->skiplist, buf + equal + 1);
191 buf += len + 1;
192 continue;
195 if (equal == len)
196 die("Missing '=': '%s'", buf);
198 fsck_set_msg_type(options, buf, buf + equal + 1);
199 buf += len + 1;
201 free(to_free);
204 static int object_on_skiplist(struct fsck_options *opts,
205 const struct object_id *oid)
207 return opts && oid && oidset_contains(&opts->skiplist, oid);
210 __attribute__((format (printf, 5, 6)))
211 static int report(struct fsck_options *options,
212 const struct object_id *oid, enum object_type object_type,
213 enum fsck_msg_id msg_id, const char *fmt, ...)
215 va_list ap;
216 struct strbuf sb = STRBUF_INIT;
217 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
218 int result;
220 if (msg_type == FSCK_IGNORE)
221 return 0;
223 if (object_on_skiplist(options, oid))
224 return 0;
226 if (msg_type == FSCK_FATAL)
227 msg_type = FSCK_ERROR;
228 else if (msg_type == FSCK_INFO)
229 msg_type = FSCK_WARN;
231 prepare_msg_ids();
232 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
234 va_start(ap, fmt);
235 strbuf_vaddf(&sb, fmt, ap);
236 result = options->error_func(options, oid, object_type,
237 msg_type, msg_id, sb.buf);
238 strbuf_release(&sb);
239 va_end(ap);
241 return result;
244 void fsck_enable_object_names(struct fsck_options *options)
246 if (!options->object_names)
247 options->object_names = kh_init_oid_map();
250 const char *fsck_get_object_name(struct fsck_options *options,
251 const struct object_id *oid)
253 khiter_t pos;
254 if (!options->object_names)
255 return NULL;
256 pos = kh_get_oid_map(options->object_names, *oid);
257 if (pos >= kh_end(options->object_names))
258 return NULL;
259 return kh_value(options->object_names, pos);
262 void fsck_put_object_name(struct fsck_options *options,
263 const struct object_id *oid,
264 const char *fmt, ...)
266 va_list ap;
267 struct strbuf buf = STRBUF_INIT;
268 khiter_t pos;
269 int hashret;
271 if (!options->object_names)
272 return;
274 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
275 if (!hashret)
276 return;
277 va_start(ap, fmt);
278 strbuf_vaddf(&buf, fmt, ap);
279 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
280 va_end(ap);
283 const char *fsck_describe_object(struct fsck_options *options,
284 const struct object_id *oid)
286 static struct strbuf bufs[] = {
287 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
289 static int b = 0;
290 struct strbuf *buf;
291 const char *name = fsck_get_object_name(options, oid);
293 buf = bufs + b;
294 b = (b + 1) % ARRAY_SIZE(bufs);
295 strbuf_reset(buf);
296 strbuf_addstr(buf, oid_to_hex(oid));
297 if (name)
298 strbuf_addf(buf, " (%s)", name);
300 return buf->buf;
303 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
305 struct tree_desc desc;
306 struct name_entry entry;
307 int res = 0;
308 const char *name;
310 if (parse_tree(tree))
311 return -1;
313 name = fsck_get_object_name(options, &tree->object.oid);
314 if (init_tree_desc_gently(&desc, tree->buffer, tree->size, 0))
315 return -1;
316 while (tree_entry_gently(&desc, &entry)) {
317 struct object *obj;
318 int result;
320 if (S_ISGITLINK(entry.mode))
321 continue;
323 if (S_ISDIR(entry.mode)) {
324 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
325 if (name && obj)
326 fsck_put_object_name(options, &entry.oid, "%s%s/",
327 name, entry.path);
328 result = options->walk(obj, OBJ_TREE, data, options);
330 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
331 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
332 if (name && obj)
333 fsck_put_object_name(options, &entry.oid, "%s%s",
334 name, entry.path);
335 result = options->walk(obj, OBJ_BLOB, data, options);
337 else {
338 result = error("in tree %s: entry %s has bad mode %.6o",
339 fsck_describe_object(options, &tree->object.oid),
340 entry.path, entry.mode);
342 if (result < 0)
343 return result;
344 if (!res)
345 res = result;
347 return res;
350 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
352 int counter = 0, generation = 0, name_prefix_len = 0;
353 struct commit_list *parents;
354 int res;
355 int result;
356 const char *name;
358 if (parse_commit(commit))
359 return -1;
361 name = fsck_get_object_name(options, &commit->object.oid);
362 if (name)
363 fsck_put_object_name(options, get_commit_tree_oid(commit),
364 "%s:", name);
366 result = options->walk((struct object *)get_commit_tree(commit),
367 OBJ_TREE, data, options);
368 if (result < 0)
369 return result;
370 res = result;
372 parents = commit->parents;
373 if (name && parents) {
374 int len = strlen(name), power;
376 if (len && name[len - 1] == '^') {
377 generation = 1;
378 name_prefix_len = len - 1;
380 else { /* parse ~<generation> suffix */
381 for (generation = 0, power = 1;
382 len && isdigit(name[len - 1]);
383 power *= 10)
384 generation += power * (name[--len] - '0');
385 if (power > 1 && len && name[len - 1] == '~')
386 name_prefix_len = len - 1;
387 else {
388 /* Maybe a non-first parent, e.g. HEAD^2 */
389 generation = 0;
390 name_prefix_len = len;
395 while (parents) {
396 if (name) {
397 struct object_id *oid = &parents->item->object.oid;
399 if (counter++)
400 fsck_put_object_name(options, oid, "%s^%d",
401 name, counter);
402 else if (generation > 0)
403 fsck_put_object_name(options, oid, "%.*s~%d",
404 name_prefix_len, name,
405 generation + 1);
406 else
407 fsck_put_object_name(options, oid, "%s^", name);
409 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
410 if (result < 0)
411 return result;
412 if (!res)
413 res = result;
414 parents = parents->next;
416 return res;
419 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
421 const char *name = fsck_get_object_name(options, &tag->object.oid);
423 if (parse_tag(tag))
424 return -1;
425 if (name)
426 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
427 return options->walk(tag->tagged, OBJ_ANY, data, options);
430 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
432 if (!obj)
433 return -1;
435 if (obj->type == OBJ_NONE)
436 parse_object(the_repository, &obj->oid);
438 switch (obj->type) {
439 case OBJ_BLOB:
440 return 0;
441 case OBJ_TREE:
442 return fsck_walk_tree((struct tree *)obj, data, options);
443 case OBJ_COMMIT:
444 return fsck_walk_commit((struct commit *)obj, data, options);
445 case OBJ_TAG:
446 return fsck_walk_tag((struct tag *)obj, data, options);
447 default:
448 error("Unknown object type for %s",
449 fsck_describe_object(options, &obj->oid));
450 return -1;
454 struct name_stack {
455 const char **names;
456 size_t nr, alloc;
459 static void name_stack_push(struct name_stack *stack, const char *name)
461 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
462 stack->names[stack->nr++] = name;
465 static const char *name_stack_pop(struct name_stack *stack)
467 return stack->nr ? stack->names[--stack->nr] : NULL;
470 static void name_stack_clear(struct name_stack *stack)
472 FREE_AND_NULL(stack->names);
473 stack->nr = stack->alloc = 0;
477 * The entries in a tree are ordered in the _path_ order,
478 * which means that a directory entry is ordered by adding
479 * a slash to the end of it.
481 * So a directory called "a" is ordered _after_ a file
482 * called "a.c", because "a/" sorts after "a.c".
484 #define TREE_UNORDERED (-1)
485 #define TREE_HAS_DUPS (-2)
487 static int is_less_than_slash(unsigned char c)
489 return '\0' < c && c < '/';
492 static int verify_ordered(unsigned mode1, const char *name1,
493 unsigned mode2, const char *name2,
494 struct name_stack *candidates)
496 int len1 = strlen(name1);
497 int len2 = strlen(name2);
498 int len = len1 < len2 ? len1 : len2;
499 unsigned char c1, c2;
500 int cmp;
502 cmp = memcmp(name1, name2, len);
503 if (cmp < 0)
504 return 0;
505 if (cmp > 0)
506 return TREE_UNORDERED;
509 * Ok, the first <len> characters are the same.
510 * Now we need to order the next one, but turn
511 * a '\0' into a '/' for a directory entry.
513 c1 = name1[len];
514 c2 = name2[len];
515 if (!c1 && !c2)
517 * git-write-tree used to write out a nonsense tree that has
518 * entries with the same name, one blob and one tree. Make
519 * sure we do not have duplicate entries.
521 return TREE_HAS_DUPS;
522 if (!c1 && S_ISDIR(mode1))
523 c1 = '/';
524 if (!c2 && S_ISDIR(mode2))
525 c2 = '/';
528 * There can be non-consecutive duplicates due to the implicitly
529 * added slash, e.g.:
531 * foo
532 * foo.bar
533 * foo.bar.baz
534 * foo.bar/
535 * foo/
537 * Record non-directory candidates (like "foo" and "foo.bar" in
538 * the example) on a stack and check directory candidates (like
539 * foo/" and "foo.bar/") against that stack.
541 if (!c1 && is_less_than_slash(c2)) {
542 name_stack_push(candidates, name1);
543 } else if (c2 == '/' && is_less_than_slash(c1)) {
544 for (;;) {
545 const char *p;
546 const char *f_name = name_stack_pop(candidates);
548 if (!f_name)
549 break;
550 if (!skip_prefix(name2, f_name, &p))
551 continue;
552 if (!*p)
553 return TREE_HAS_DUPS;
554 if (is_less_than_slash(*p)) {
555 name_stack_push(candidates, f_name);
556 break;
561 return c1 < c2 ? 0 : TREE_UNORDERED;
564 static int fsck_tree(const struct object_id *tree_oid,
565 const char *buffer, unsigned long size,
566 struct fsck_options *options)
568 int retval = 0;
569 int has_null_sha1 = 0;
570 int has_full_path = 0;
571 int has_empty_name = 0;
572 int has_dot = 0;
573 int has_dotdot = 0;
574 int has_dotgit = 0;
575 int has_zero_pad = 0;
576 int has_bad_modes = 0;
577 int has_dup_entries = 0;
578 int not_properly_sorted = 0;
579 struct tree_desc desc;
580 unsigned o_mode;
581 const char *o_name;
582 struct name_stack df_dup_candidates = { NULL };
584 if (init_tree_desc_gently(&desc, buffer, size, TREE_DESC_RAW_MODES)) {
585 retval += report(options, tree_oid, OBJ_TREE,
586 FSCK_MSG_BAD_TREE,
587 "cannot be parsed as a tree");
588 return retval;
591 o_mode = 0;
592 o_name = NULL;
594 while (desc.size) {
595 unsigned short mode;
596 const char *name, *backslash;
597 const struct object_id *entry_oid;
599 entry_oid = tree_entry_extract(&desc, &name, &mode);
601 has_null_sha1 |= is_null_oid(entry_oid);
602 has_full_path |= !!strchr(name, '/');
603 has_empty_name |= !*name;
604 has_dot |= !strcmp(name, ".");
605 has_dotdot |= !strcmp(name, "..");
606 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
607 has_zero_pad |= *(char *)desc.buffer == '0';
609 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
610 if (!S_ISLNK(mode))
611 oidset_insert(&options->gitmodules_found,
612 entry_oid);
613 else
614 retval += report(options,
615 tree_oid, OBJ_TREE,
616 FSCK_MSG_GITMODULES_SYMLINK,
617 ".gitmodules is a symbolic link");
620 if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
621 if (!S_ISLNK(mode))
622 oidset_insert(&options->gitattributes_found,
623 entry_oid);
624 else
625 retval += report(options, tree_oid, OBJ_TREE,
626 FSCK_MSG_GITATTRIBUTES_SYMLINK,
627 ".gitattributes is a symlink");
630 if (S_ISLNK(mode)) {
631 if (is_hfs_dotgitignore(name) ||
632 is_ntfs_dotgitignore(name))
633 retval += report(options, tree_oid, OBJ_TREE,
634 FSCK_MSG_GITIGNORE_SYMLINK,
635 ".gitignore is a symlink");
636 if (is_hfs_dotmailmap(name) ||
637 is_ntfs_dotmailmap(name))
638 retval += report(options, tree_oid, OBJ_TREE,
639 FSCK_MSG_MAILMAP_SYMLINK,
640 ".mailmap is a symlink");
643 if ((backslash = strchr(name, '\\'))) {
644 while (backslash) {
645 backslash++;
646 has_dotgit |= is_ntfs_dotgit(backslash);
647 if (is_ntfs_dotgitmodules(backslash)) {
648 if (!S_ISLNK(mode))
649 oidset_insert(&options->gitmodules_found,
650 entry_oid);
651 else
652 retval += report(options, tree_oid, OBJ_TREE,
653 FSCK_MSG_GITMODULES_SYMLINK,
654 ".gitmodules is a symbolic link");
656 backslash = strchr(backslash, '\\');
660 if (update_tree_entry_gently(&desc)) {
661 retval += report(options, tree_oid, OBJ_TREE,
662 FSCK_MSG_BAD_TREE,
663 "cannot be parsed as a tree");
664 break;
667 switch (mode) {
669 * Standard modes..
671 case S_IFREG | 0755:
672 case S_IFREG | 0644:
673 case S_IFLNK:
674 case S_IFDIR:
675 case S_IFGITLINK:
676 break;
678 * This is nonstandard, but we had a few of these
679 * early on when we honored the full set of mode
680 * bits..
682 case S_IFREG | 0664:
683 if (!options->strict)
684 break;
685 /* fallthrough */
686 default:
687 has_bad_modes = 1;
690 if (o_name) {
691 switch (verify_ordered(o_mode, o_name, mode, name,
692 &df_dup_candidates)) {
693 case TREE_UNORDERED:
694 not_properly_sorted = 1;
695 break;
696 case TREE_HAS_DUPS:
697 has_dup_entries = 1;
698 break;
699 default:
700 break;
704 o_mode = mode;
705 o_name = name;
708 name_stack_clear(&df_dup_candidates);
710 if (has_null_sha1)
711 retval += report(options, tree_oid, OBJ_TREE,
712 FSCK_MSG_NULL_SHA1,
713 "contains entries pointing to null sha1");
714 if (has_full_path)
715 retval += report(options, tree_oid, OBJ_TREE,
716 FSCK_MSG_FULL_PATHNAME,
717 "contains full pathnames");
718 if (has_empty_name)
719 retval += report(options, tree_oid, OBJ_TREE,
720 FSCK_MSG_EMPTY_NAME,
721 "contains empty pathname");
722 if (has_dot)
723 retval += report(options, tree_oid, OBJ_TREE,
724 FSCK_MSG_HAS_DOT,
725 "contains '.'");
726 if (has_dotdot)
727 retval += report(options, tree_oid, OBJ_TREE,
728 FSCK_MSG_HAS_DOTDOT,
729 "contains '..'");
730 if (has_dotgit)
731 retval += report(options, tree_oid, OBJ_TREE,
732 FSCK_MSG_HAS_DOTGIT,
733 "contains '.git'");
734 if (has_zero_pad)
735 retval += report(options, tree_oid, OBJ_TREE,
736 FSCK_MSG_ZERO_PADDED_FILEMODE,
737 "contains zero-padded file modes");
738 if (has_bad_modes)
739 retval += report(options, tree_oid, OBJ_TREE,
740 FSCK_MSG_BAD_FILEMODE,
741 "contains bad file modes");
742 if (has_dup_entries)
743 retval += report(options, tree_oid, OBJ_TREE,
744 FSCK_MSG_DUPLICATE_ENTRIES,
745 "contains duplicate file entries");
746 if (not_properly_sorted)
747 retval += report(options, tree_oid, OBJ_TREE,
748 FSCK_MSG_TREE_NOT_SORTED,
749 "not properly sorted");
750 return retval;
754 * Confirm that the headers of a commit or tag object end in a reasonable way,
755 * either with the usual "\n\n" separator, or at least with a trailing newline
756 * on the final header line.
758 * This property is important for the memory safety of our callers. It allows
759 * them to scan the buffer linewise without constantly checking the remaining
760 * size as long as:
762 * - they check that there are bytes left in the buffer at the start of any
763 * line (i.e., that the last newline they saw was not the final one we
764 * found here)
766 * - any intra-line scanning they do will stop at a newline, which will worst
767 * case hit the newline we found here as the end-of-header. This makes it
768 * OK for them to use helpers like parse_oid_hex(), or even skip_prefix().
770 static int verify_headers(const void *data, unsigned long size,
771 const struct object_id *oid, enum object_type type,
772 struct fsck_options *options)
774 const char *buffer = (const char *)data;
775 unsigned long i;
777 for (i = 0; i < size; i++) {
778 switch (buffer[i]) {
779 case '\0':
780 return report(options, oid, type,
781 FSCK_MSG_NUL_IN_HEADER,
782 "unterminated header: NUL at offset %ld", i);
783 case '\n':
784 if (i + 1 < size && buffer[i + 1] == '\n')
785 return 0;
790 * We did not find double-LF that separates the header
791 * and the body. Not having a body is not a crime but
792 * we do want to see the terminating LF for the last header
793 * line.
795 if (size && buffer[size - 1] == '\n')
796 return 0;
798 return report(options, oid, type,
799 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
802 static int fsck_ident(const char **ident,
803 const struct object_id *oid, enum object_type type,
804 struct fsck_options *options)
806 const char *p = *ident;
807 char *end;
809 *ident = strchrnul(*ident, '\n');
810 if (**ident == '\n')
811 (*ident)++;
813 if (*p == '<')
814 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
815 p += strcspn(p, "<>\n");
816 if (*p == '>')
817 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
818 if (*p != '<')
819 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
820 if (p[-1] != ' ')
821 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
822 p++;
823 p += strcspn(p, "<>\n");
824 if (*p != '>')
825 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
826 p++;
827 if (*p != ' ')
828 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
829 p++;
831 * Our timestamp parser is based on the C strto*() functions, which
832 * will happily eat whitespace, including the newline that is supposed
833 * to prevent us walking past the end of the buffer. So do our own
834 * scan, skipping linear whitespace but not newlines, and then
835 * confirming we found a digit. We _could_ be even more strict here,
836 * as we really expect only a single space, but since we have
837 * traditionally allowed extra whitespace, we'll continue to do so.
839 while (*p == ' ' || *p == '\t')
840 p++;
841 if (!isdigit(*p))
842 return report(options, oid, type, FSCK_MSG_BAD_DATE,
843 "invalid author/committer line - bad date");
844 if (*p == '0' && p[1] != ' ')
845 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
846 if (date_overflows(parse_timestamp(p, &end, 10)))
847 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
848 if ((end == p || *end != ' '))
849 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
850 p = end + 1;
851 if ((*p != '+' && *p != '-') ||
852 !isdigit(p[1]) ||
853 !isdigit(p[2]) ||
854 !isdigit(p[3]) ||
855 !isdigit(p[4]) ||
856 (p[5] != '\n'))
857 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
858 p += 6;
859 return 0;
862 static int fsck_commit(const struct object_id *oid,
863 const char *buffer, unsigned long size,
864 struct fsck_options *options)
866 struct object_id tree_oid, parent_oid;
867 unsigned author_count;
868 int err;
869 const char *buffer_begin = buffer;
870 const char *buffer_end = buffer + size;
871 const char *p;
874 * We _must_ stop parsing immediately if this reports failure, as the
875 * memory safety of the rest of the function depends on it. See the
876 * comment above the definition of verify_headers() for more details.
878 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
879 return -1;
881 if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer))
882 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
883 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
884 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
885 if (err)
886 return err;
888 buffer = p + 1;
889 while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) {
890 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
891 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
892 if (err)
893 return err;
895 buffer = p + 1;
897 author_count = 0;
898 while (buffer < buffer_end && skip_prefix(buffer, "author ", &buffer)) {
899 author_count++;
900 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
901 if (err)
902 return err;
904 if (author_count < 1)
905 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
906 else if (author_count > 1)
907 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
908 if (err)
909 return err;
910 if (buffer >= buffer_end || !skip_prefix(buffer, "committer ", &buffer))
911 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
912 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
913 if (err)
914 return err;
915 if (memchr(buffer_begin, '\0', size)) {
916 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
917 "NUL byte in the commit object body");
918 if (err)
919 return err;
921 return 0;
924 static int fsck_tag(const struct object_id *oid, const char *buffer,
925 unsigned long size, struct fsck_options *options)
927 struct object_id tagged_oid;
928 int tagged_type;
929 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
930 &tagged_type);
933 int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
934 unsigned long size, struct fsck_options *options,
935 struct object_id *tagged_oid,
936 int *tagged_type)
938 int ret = 0;
939 char *eol;
940 struct strbuf sb = STRBUF_INIT;
941 const char *buffer_end = buffer + size;
942 const char *p;
945 * We _must_ stop parsing immediately if this reports failure, as the
946 * memory safety of the rest of the function depends on it. See the
947 * comment above the definition of verify_headers() for more details.
949 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
950 if (ret)
951 goto done;
953 if (buffer >= buffer_end || !skip_prefix(buffer, "object ", &buffer)) {
954 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
955 goto done;
957 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
958 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
959 if (ret)
960 goto done;
962 buffer = p + 1;
964 if (buffer >= buffer_end || !skip_prefix(buffer, "type ", &buffer)) {
965 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
966 goto done;
968 eol = memchr(buffer, '\n', buffer_end - buffer);
969 if (!eol) {
970 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
971 goto done;
973 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
974 if (*tagged_type < 0)
975 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
976 if (ret)
977 goto done;
978 buffer = eol + 1;
980 if (buffer >= buffer_end || !skip_prefix(buffer, "tag ", &buffer)) {
981 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
982 goto done;
984 eol = memchr(buffer, '\n', buffer_end - buffer);
985 if (!eol) {
986 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
987 goto done;
989 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
990 if (check_refname_format(sb.buf, 0)) {
991 ret = report(options, oid, OBJ_TAG,
992 FSCK_MSG_BAD_TAG_NAME,
993 "invalid 'tag' name: %.*s",
994 (int)(eol - buffer), buffer);
995 if (ret)
996 goto done;
998 buffer = eol + 1;
1000 if (buffer >= buffer_end || !skip_prefix(buffer, "tagger ", &buffer)) {
1001 /* early tags do not contain 'tagger' lines; warn only */
1002 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
1003 if (ret)
1004 goto done;
1006 else
1007 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
1009 if (buffer < buffer_end && !starts_with(buffer, "\n")) {
1011 * The verify_headers() check will allow
1012 * e.g. "[...]tagger <tagger>\nsome
1013 * garbage\n\nmessage" to pass, thinking "some
1014 * garbage" could be a custom header. E.g. "mktag"
1015 * doesn't want any unknown headers.
1017 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
1018 if (ret)
1019 goto done;
1022 done:
1023 strbuf_release(&sb);
1024 return ret;
1027 static int starts_with_dot_slash(const char *const path)
1029 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
1030 PATH_MATCH_XPLATFORM);
1033 static int starts_with_dot_dot_slash(const char *const path)
1035 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
1036 PATH_MATCH_XPLATFORM);
1039 static int submodule_url_is_relative(const char *url)
1041 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
1045 * Count directory components that a relative submodule URL should chop
1046 * from the remote_url it is to be resolved against.
1048 * In other words, this counts "../" components at the start of a
1049 * submodule URL.
1051 * Returns the number of directory components to chop and writes a
1052 * pointer to the next character of url after all leading "./" and
1053 * "../" components to out.
1055 static int count_leading_dotdots(const char *url, const char **out)
1057 int result = 0;
1058 while (1) {
1059 if (starts_with_dot_dot_slash(url)) {
1060 result++;
1061 url += strlen("../");
1062 continue;
1064 if (starts_with_dot_slash(url)) {
1065 url += strlen("./");
1066 continue;
1068 *out = url;
1069 return result;
1073 * Check whether a transport is implemented by git-remote-curl.
1075 * If it is, returns 1 and writes the URL that would be passed to
1076 * git-remote-curl to the "out" parameter.
1078 * Otherwise, returns 0 and leaves "out" untouched.
1080 * Examples:
1081 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1082 * https://example.com/repo.git -> 1, https://example.com/repo.git
1083 * git://example.com/repo.git -> 0
1085 * This is for use in checking for previously exploitable bugs that
1086 * required a submodule URL to be passed to git-remote-curl.
1088 static int url_to_curl_url(const char *url, const char **out)
1091 * We don't need to check for case-aliases, "http.exe", and so
1092 * on because in the default configuration, is_transport_allowed
1093 * prevents URLs with those schemes from being cloned
1094 * automatically.
1096 if (skip_prefix(url, "http::", out) ||
1097 skip_prefix(url, "https::", out) ||
1098 skip_prefix(url, "ftp::", out) ||
1099 skip_prefix(url, "ftps::", out))
1100 return 1;
1101 if (starts_with(url, "http://") ||
1102 starts_with(url, "https://") ||
1103 starts_with(url, "ftp://") ||
1104 starts_with(url, "ftps://")) {
1105 *out = url;
1106 return 1;
1108 return 0;
1111 static int check_submodule_url(const char *url)
1113 const char *curl_url;
1115 if (looks_like_command_line_option(url))
1116 return -1;
1118 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
1119 char *decoded;
1120 const char *next;
1121 int has_nl;
1124 * This could be appended to an http URL and url-decoded;
1125 * check for malicious characters.
1127 decoded = url_decode(url);
1128 has_nl = !!strchr(decoded, '\n');
1130 free(decoded);
1131 if (has_nl)
1132 return -1;
1135 * URLs which escape their root via "../" can overwrite
1136 * the host field and previous components, resolving to
1137 * URLs like https::example.com/submodule.git and
1138 * https:///example.com/submodule.git that were
1139 * susceptible to CVE-2020-11008.
1141 if (count_leading_dotdots(url, &next) > 0 &&
1142 (*next == ':' || *next == '/'))
1143 return -1;
1146 else if (url_to_curl_url(url, &curl_url)) {
1147 struct credential c = CREDENTIAL_INIT;
1148 int ret = 0;
1149 if (credential_from_url_gently(&c, curl_url, 1) ||
1150 !*c.host)
1151 ret = -1;
1152 credential_clear(&c);
1153 return ret;
1156 return 0;
1159 struct fsck_gitmodules_data {
1160 const struct object_id *oid;
1161 struct fsck_options *options;
1162 int ret;
1165 static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
1167 struct fsck_gitmodules_data *data = vdata;
1168 const char *subsection, *key;
1169 size_t subsection_len;
1170 char *name;
1172 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1173 !subsection)
1174 return 0;
1176 name = xmemdupz(subsection, subsection_len);
1177 if (check_submodule_name(name) < 0)
1178 data->ret |= report(data->options,
1179 data->oid, OBJ_BLOB,
1180 FSCK_MSG_GITMODULES_NAME,
1181 "disallowed submodule name: %s",
1182 name);
1183 if (!strcmp(key, "url") && value &&
1184 check_submodule_url(value) < 0)
1185 data->ret |= report(data->options,
1186 data->oid, OBJ_BLOB,
1187 FSCK_MSG_GITMODULES_URL,
1188 "disallowed submodule url: %s",
1189 value);
1190 if (!strcmp(key, "path") && value &&
1191 looks_like_command_line_option(value))
1192 data->ret |= report(data->options,
1193 data->oid, OBJ_BLOB,
1194 FSCK_MSG_GITMODULES_PATH,
1195 "disallowed submodule path: %s",
1196 value);
1197 if (!strcmp(key, "update") && value &&
1198 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
1199 data->ret |= report(data->options, data->oid, OBJ_BLOB,
1200 FSCK_MSG_GITMODULES_UPDATE,
1201 "disallowed submodule update setting: %s",
1202 value);
1203 free(name);
1205 return 0;
1208 static int fsck_blob(const struct object_id *oid, const char *buf,
1209 unsigned long size, struct fsck_options *options)
1211 int ret = 0;
1213 if (object_on_skiplist(options, oid))
1214 return 0;
1216 if (oidset_contains(&options->gitmodules_found, oid)) {
1217 struct config_options config_opts = { 0 };
1218 struct fsck_gitmodules_data data;
1220 oidset_insert(&options->gitmodules_done, oid);
1222 if (!buf) {
1224 * A missing buffer here is a sign that the caller found the
1225 * blob too gigantic to load into memory. Let's just consider
1226 * that an error.
1228 return report(options, oid, OBJ_BLOB,
1229 FSCK_MSG_GITMODULES_LARGE,
1230 ".gitmodules too large to parse");
1233 data.oid = oid;
1234 data.options = options;
1235 data.ret = 0;
1236 config_opts.error_action = CONFIG_ERROR_SILENT;
1237 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1238 ".gitmodules", buf, size, &data, &config_opts))
1239 data.ret |= report(options, oid, OBJ_BLOB,
1240 FSCK_MSG_GITMODULES_PARSE,
1241 "could not parse gitmodules blob");
1242 ret |= data.ret;
1245 if (oidset_contains(&options->gitattributes_found, oid)) {
1246 const char *ptr;
1248 oidset_insert(&options->gitattributes_done, oid);
1250 if (!buf || size > ATTR_MAX_FILE_SIZE) {
1252 * A missing buffer here is a sign that the caller found the
1253 * blob too gigantic to load into memory. Let's just consider
1254 * that an error.
1256 return report(options, oid, OBJ_BLOB,
1257 FSCK_MSG_GITATTRIBUTES_LARGE,
1258 ".gitattributes too large to parse");
1261 for (ptr = buf; *ptr; ) {
1262 const char *eol = strchrnul(ptr, '\n');
1263 if (eol - ptr >= ATTR_MAX_LINE_LENGTH) {
1264 ret |= report(options, oid, OBJ_BLOB,
1265 FSCK_MSG_GITATTRIBUTES_LINE_LENGTH,
1266 ".gitattributes has too long lines to parse");
1267 break;
1270 ptr = *eol ? eol + 1 : eol;
1274 return ret;
1277 int fsck_object(struct object *obj, void *data, unsigned long size,
1278 struct fsck_options *options)
1280 if (!obj)
1281 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1283 return fsck_buffer(&obj->oid, obj->type, data, size, options);
1286 int fsck_buffer(const struct object_id *oid, enum object_type type,
1287 void *data, unsigned long size,
1288 struct fsck_options *options)
1290 if (type == OBJ_BLOB)
1291 return fsck_blob(oid, data, size, options);
1292 if (type == OBJ_TREE)
1293 return fsck_tree(oid, data, size, options);
1294 if (type == OBJ_COMMIT)
1295 return fsck_commit(oid, data, size, options);
1296 if (type == OBJ_TAG)
1297 return fsck_tag(oid, data, size, options);
1299 return report(options, oid, type,
1300 FSCK_MSG_UNKNOWN_TYPE,
1301 "unknown type '%d' (internal fsck error)",
1302 type);
1305 int fsck_error_function(struct fsck_options *o,
1306 const struct object_id *oid,
1307 enum object_type object_type,
1308 enum fsck_msg_type msg_type,
1309 enum fsck_msg_id msg_id,
1310 const char *message)
1312 if (msg_type == FSCK_WARN) {
1313 warning("object %s: %s", fsck_describe_object(o, oid), message);
1314 return 0;
1316 error("object %s: %s", fsck_describe_object(o, oid), message);
1317 return 1;
1320 static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
1321 enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
1322 struct fsck_options *options, const char *blob_type)
1324 int ret = 0;
1325 struct oidset_iter iter;
1326 const struct object_id *oid;
1328 oidset_iter_init(blobs_found, &iter);
1329 while ((oid = oidset_iter_next(&iter))) {
1330 enum object_type type;
1331 unsigned long size;
1332 char *buf;
1334 if (oidset_contains(blobs_done, oid))
1335 continue;
1337 buf = read_object_file(oid, &type, &size);
1338 if (!buf) {
1339 if (is_promisor_object(oid))
1340 continue;
1341 ret |= report(options,
1342 oid, OBJ_BLOB, msg_missing,
1343 "unable to read %s blob", blob_type);
1344 continue;
1347 if (type == OBJ_BLOB)
1348 ret |= fsck_blob(oid, buf, size, options);
1349 else
1350 ret |= report(options, oid, type, msg_type,
1351 "non-blob found at %s", blob_type);
1352 free(buf);
1355 oidset_clear(blobs_found);
1356 oidset_clear(blobs_done);
1358 return ret;
1361 int fsck_finish(struct fsck_options *options)
1363 int ret = 0;
1365 ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
1366 FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
1367 options, ".gitmodules");
1368 ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
1369 FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
1370 options, ".gitattributes");
1372 return ret;
1375 int git_fsck_config(const char *var, const char *value, void *cb)
1377 struct fsck_options *options = cb;
1378 if (strcmp(var, "fsck.skiplist") == 0) {
1379 const char *path;
1380 struct strbuf sb = STRBUF_INIT;
1382 if (git_config_pathname(&path, var, value))
1383 return 1;
1384 strbuf_addf(&sb, "skiplist=%s", path);
1385 free((char *)path);
1386 fsck_set_msg_types(options, sb.buf);
1387 strbuf_release(&sb);
1388 return 0;
1391 if (skip_prefix(var, "fsck.", &var)) {
1392 fsck_set_msg_type(options, var, value);
1393 return 0;
1396 return git_default_config(var, value, cb);
1400 * Custom error callbacks that are used in more than one place.
1403 int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
1404 const struct object_id *oid,
1405 enum object_type object_type,
1406 enum fsck_msg_type msg_type,
1407 enum fsck_msg_id msg_id,
1408 const char *message)
1410 if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1411 puts(oid_to_hex(oid));
1412 return 0;
1414 return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);