config.txt: describe handling of whitespace further
[alt-git.git] / fsck.c
blob2b1e348005b7bcc7f27bc08e126c93260cda81e4
1 #include "git-compat-util.h"
2 #include "date.h"
3 #include "dir.h"
4 #include "hex.h"
5 #include "object-store-ll.h"
6 #include "path.h"
7 #include "repository.h"
8 #include "object.h"
9 #include "attr.h"
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"
16 #include "refs.h"
17 #include "url.h"
18 #include "utf8.h"
19 #include "decorate.h"
20 #include "oidset.h"
21 #include "packfile.h"
22 #include "submodule-config.h"
23 #include "config.h"
24 #include "credential.h"
25 #include "help.h"
27 #define STR(x) #x
28 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
29 static struct {
30 const char *id_string;
31 const char *downcased;
32 const char *camelcased;
33 enum fsck_msg_type msg_type;
34 } msg_id_info[FSCK_MSG_MAX + 1] = {
35 FOREACH_FSCK_MSG_ID(MSG_ID)
36 { NULL, NULL, NULL, -1 }
38 #undef MSG_ID
39 #undef STR
41 static void prepare_msg_ids(void)
43 int i;
45 if (msg_id_info[0].downcased)
46 return;
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);
54 msg_id_info[i].downcased = q;
55 while (*p)
56 if (*p == '_')
57 p++;
58 else
59 *(q)++ = tolower(*(p)++);
60 *q = '\0';
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++);
74 *q = '\0';
78 static int parse_msg_id(const char *text)
80 int i;
82 prepare_msg_ids();
84 for (i = 0; i < FSCK_MSG_MAX; i++)
85 if (!strcmp(text, msg_id_info[i].downcased))
86 return i;
88 return -1;
91 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
93 int i;
95 prepare_msg_ids();
97 for (i = 0; i < FSCK_MSG_MAX; i++)
98 list_config_item(list, prefix, msg_id_info[i].camelcased);
101 static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
102 struct fsck_options *options)
104 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
106 if (!options->msg_type) {
107 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
109 if (options->strict && msg_type == FSCK_WARN)
110 msg_type = FSCK_ERROR;
111 return msg_type;
114 return options->msg_type[msg_id];
117 static enum fsck_msg_type parse_msg_type(const char *str)
119 if (!strcmp(str, "error"))
120 return FSCK_ERROR;
121 else if (!strcmp(str, "warn"))
122 return FSCK_WARN;
123 else if (!strcmp(str, "ignore"))
124 return FSCK_IGNORE;
125 else
126 die("Unknown fsck message type: '%s'", str);
129 int is_valid_msg_type(const char *msg_id, const char *msg_type)
131 if (parse_msg_id(msg_id) < 0)
132 return 0;
133 parse_msg_type(msg_type);
134 return 1;
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)
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;
150 options->msg_type[msg_id] = msg_type;
153 void fsck_set_msg_type(struct fsck_options *options,
154 const char *msg_id_str, const char *msg_type_str)
156 int msg_id = parse_msg_id(msg_id_str);
157 enum fsck_msg_type msg_type = parse_msg_type(msg_type_str);
159 if (msg_id < 0)
160 die("Unhandled message id: %s", msg_id_str);
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);
165 fsck_set_msg_type_from_ids(options, msg_id, msg_type);
168 void fsck_set_msg_types(struct fsck_options *options, const char *values)
170 char *buf = xstrdup(values), *to_free = buf;
171 int done = 0;
173 while (!done) {
174 int len = strcspn(buf, " ,|"), equal;
176 done = !buf[len];
177 if (!len) {
178 buf++;
179 continue;
181 buf[len] = '\0';
183 for (equal = 0;
184 equal < len && buf[equal] != '=' && buf[equal] != ':';
185 equal++)
186 buf[equal] = tolower(buf[equal]);
187 buf[equal] = '\0';
189 if (!strcmp(buf, "skiplist")) {
190 if (equal == len)
191 die("skiplist requires a path");
192 oidset_parse_file(&options->skiplist, buf + equal + 1);
193 buf += len + 1;
194 continue;
197 if (equal == len)
198 die("Missing '=': '%s'", buf);
200 fsck_set_msg_type(options, buf, buf + equal + 1);
201 buf += len + 1;
203 free(to_free);
206 static int object_on_skiplist(struct fsck_options *opts,
207 const struct object_id *oid)
209 return opts && oid && oidset_contains(&opts->skiplist, oid);
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,
215 enum fsck_msg_id msg_id, const char *fmt, ...)
217 va_list ap;
218 struct strbuf sb = STRBUF_INIT;
219 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
220 int result;
222 if (msg_type == FSCK_IGNORE)
223 return 0;
225 if (object_on_skiplist(options, oid))
226 return 0;
228 if (msg_type == FSCK_FATAL)
229 msg_type = FSCK_ERROR;
230 else if (msg_type == FSCK_INFO)
231 msg_type = FSCK_WARN;
233 prepare_msg_ids();
234 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
236 va_start(ap, fmt);
237 strbuf_vaddf(&sb, fmt, ap);
238 result = options->error_func(options, oid, object_type,
239 msg_type, msg_id, sb.buf);
240 strbuf_release(&sb);
241 va_end(ap);
243 return result;
246 void fsck_enable_object_names(struct fsck_options *options)
248 if (!options->object_names)
249 options->object_names = kh_init_oid_map();
252 const char *fsck_get_object_name(struct fsck_options *options,
253 const struct object_id *oid)
255 khiter_t pos;
256 if (!options->object_names)
257 return NULL;
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);
264 void fsck_put_object_name(struct fsck_options *options,
265 const struct object_id *oid,
266 const char *fmt, ...)
268 va_list ap;
269 struct strbuf buf = STRBUF_INIT;
270 khiter_t pos;
271 int hashret;
273 if (!options->object_names)
274 return;
276 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
277 if (!hashret)
278 return;
279 va_start(ap, fmt);
280 strbuf_vaddf(&buf, fmt, ap);
281 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
282 va_end(ap);
285 const char *fsck_describe_object(struct fsck_options *options,
286 const struct object_id *oid)
288 static struct strbuf bufs[] = {
289 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
291 static int b = 0;
292 struct strbuf *buf;
293 const char *name = fsck_get_object_name(options, oid);
295 buf = bufs + b;
296 b = (b + 1) % ARRAY_SIZE(bufs);
297 strbuf_reset(buf);
298 strbuf_addstr(buf, oid_to_hex(oid));
299 if (name)
300 strbuf_addf(buf, " (%s)", name);
302 return buf->buf;
305 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
307 struct tree_desc desc;
308 struct name_entry entry;
309 int res = 0;
310 const char *name;
312 if (parse_tree(tree))
313 return -1;
315 name = fsck_get_object_name(options, &tree->object.oid);
316 if (init_tree_desc_gently(&desc, tree->buffer, tree->size, 0))
317 return -1;
318 while (tree_entry_gently(&desc, &entry)) {
319 struct object *obj;
320 int result;
322 if (S_ISGITLINK(entry.mode))
323 continue;
325 if (S_ISDIR(entry.mode)) {
326 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
327 if (name && obj)
328 fsck_put_object_name(options, &entry.oid, "%s%s/",
329 name, entry.path);
330 result = options->walk(obj, OBJ_TREE, data, options);
332 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
333 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
334 if (name && obj)
335 fsck_put_object_name(options, &entry.oid, "%s%s",
336 name, entry.path);
337 result = options->walk(obj, OBJ_BLOB, data, options);
339 else {
340 result = error("in tree %s: entry %s has bad mode %.6o",
341 fsck_describe_object(options, &tree->object.oid),
342 entry.path, entry.mode);
344 if (result < 0)
345 return result;
346 if (!res)
347 res = result;
349 return res;
352 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
354 int counter = 0, generation = 0, name_prefix_len = 0;
355 struct commit_list *parents;
356 int res;
357 int result;
358 const char *name;
360 if (repo_parse_commit(the_repository, commit))
361 return -1;
363 name = fsck_get_object_name(options, &commit->object.oid);
364 if (name)
365 fsck_put_object_name(options, get_commit_tree_oid(commit),
366 "%s:", name);
368 result = options->walk((struct object *) repo_get_commit_tree(the_repository, commit),
369 OBJ_TREE, data, options);
370 if (result < 0)
371 return result;
372 res = result;
374 parents = commit->parents;
375 if (name && parents) {
376 int len = strlen(name), power;
378 if (len && name[len - 1] == '^') {
379 generation = 1;
380 name_prefix_len = len - 1;
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;
389 else {
390 /* Maybe a non-first parent, e.g. HEAD^2 */
391 generation = 0;
392 name_prefix_len = len;
397 while (parents) {
398 if (name) {
399 struct object_id *oid = &parents->item->object.oid;
401 if (counter++)
402 fsck_put_object_name(options, oid, "%s^%d",
403 name, counter);
404 else if (generation > 0)
405 fsck_put_object_name(options, oid, "%.*s~%d",
406 name_prefix_len, name,
407 generation + 1);
408 else
409 fsck_put_object_name(options, oid, "%s^", name);
411 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
412 if (result < 0)
413 return result;
414 if (!res)
415 res = result;
416 parents = parents->next;
418 return res;
421 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
423 const char *name = fsck_get_object_name(options, &tag->object.oid);
425 if (parse_tag(tag))
426 return -1;
427 if (name)
428 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
429 return options->walk(tag->tagged, OBJ_ANY, data, options);
432 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
434 if (!obj)
435 return -1;
437 if (obj->type == OBJ_NONE)
438 parse_object(the_repository, &obj->oid);
440 switch (obj->type) {
441 case OBJ_BLOB:
442 return 0;
443 case OBJ_TREE:
444 return fsck_walk_tree((struct tree *)obj, data, options);
445 case OBJ_COMMIT:
446 return fsck_walk_commit((struct commit *)obj, data, options);
447 case OBJ_TAG:
448 return fsck_walk_tag((struct tag *)obj, data, options);
449 default:
450 error("Unknown object type for %s",
451 fsck_describe_object(options, &obj->oid));
452 return -1;
456 struct name_stack {
457 const char **names;
458 size_t nr, alloc;
461 static void name_stack_push(struct name_stack *stack, const char *name)
463 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
464 stack->names[stack->nr++] = name;
467 static const char *name_stack_pop(struct name_stack *stack)
469 return stack->nr ? stack->names[--stack->nr] : NULL;
472 static void name_stack_clear(struct name_stack *stack)
474 FREE_AND_NULL(stack->names);
475 stack->nr = stack->alloc = 0;
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.
483 * So a directory called "a" is ordered _after_ a file
484 * called "a.c", because "a/" sorts after "a.c".
486 #define TREE_UNORDERED (-1)
487 #define TREE_HAS_DUPS (-2)
489 static int is_less_than_slash(unsigned char c)
491 return '\0' < c && c < '/';
494 static int verify_ordered(unsigned mode1, const char *name1,
495 unsigned mode2, const char *name2,
496 struct name_stack *candidates)
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;
504 cmp = memcmp(name1, name2, len);
505 if (cmp < 0)
506 return 0;
507 if (cmp > 0)
508 return TREE_UNORDERED;
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.
515 c1 = name1[len];
516 c2 = name2[len];
517 if (!c1 && !c2)
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.
523 return TREE_HAS_DUPS;
524 if (!c1 && S_ISDIR(mode1))
525 c1 = '/';
526 if (!c2 && S_ISDIR(mode2))
527 c2 = '/';
530 * There can be non-consecutive duplicates due to the implicitly
531 * added slash, e.g.:
533 * foo
534 * foo.bar
535 * foo.bar.baz
536 * foo.bar/
537 * foo/
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.
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);
550 if (!f_name)
551 break;
552 if (!skip_prefix(name2, f_name, &p))
553 continue;
554 if (!*p)
555 return TREE_HAS_DUPS;
556 if (is_less_than_slash(*p)) {
557 name_stack_push(candidates, f_name);
558 break;
563 return c1 < c2 ? 0 : TREE_UNORDERED;
566 static int fsck_tree(const struct object_id *tree_oid,
567 const char *buffer, unsigned long size,
568 struct fsck_options *options)
570 int retval = 0;
571 int has_null_sha1 = 0;
572 int has_full_path = 0;
573 int has_empty_name = 0;
574 int has_dot = 0;
575 int has_dotdot = 0;
576 int has_dotgit = 0;
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;
584 struct name_stack df_dup_candidates = { NULL };
586 if (init_tree_desc_gently(&desc, buffer, size, TREE_DESC_RAW_MODES)) {
587 retval += report(options, tree_oid, OBJ_TREE,
588 FSCK_MSG_BAD_TREE,
589 "cannot be parsed as a tree");
590 return retval;
593 o_mode = 0;
594 o_name = NULL;
596 while (desc.size) {
597 unsigned short mode;
598 const char *name, *backslash;
599 const struct object_id *entry_oid;
601 entry_oid = tree_entry_extract(&desc, &name, &mode);
603 has_null_sha1 |= is_null_oid(entry_oid);
604 has_full_path |= !!strchr(name, '/');
605 has_empty_name |= !*name;
606 has_dot |= !strcmp(name, ".");
607 has_dotdot |= !strcmp(name, "..");
608 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
609 has_zero_pad |= *(char *)desc.buffer == '0';
611 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
612 if (!S_ISLNK(mode))
613 oidset_insert(&options->gitmodules_found,
614 entry_oid);
615 else
616 retval += report(options,
617 tree_oid, OBJ_TREE,
618 FSCK_MSG_GITMODULES_SYMLINK,
619 ".gitmodules is a symbolic link");
622 if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
623 if (!S_ISLNK(mode))
624 oidset_insert(&options->gitattributes_found,
625 entry_oid);
626 else
627 retval += report(options, tree_oid, OBJ_TREE,
628 FSCK_MSG_GITATTRIBUTES_SYMLINK,
629 ".gitattributes is a symlink");
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");
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");
645 if ((backslash = strchr(name, '\\'))) {
646 while (backslash) {
647 backslash++;
648 has_dotgit |= is_ntfs_dotgit(backslash);
649 if (is_ntfs_dotgitmodules(backslash)) {
650 if (!S_ISLNK(mode))
651 oidset_insert(&options->gitmodules_found,
652 entry_oid);
653 else
654 retval += report(options, tree_oid, OBJ_TREE,
655 FSCK_MSG_GITMODULES_SYMLINK,
656 ".gitmodules is a symbolic link");
658 backslash = strchr(backslash, '\\');
662 if (update_tree_entry_gently(&desc)) {
663 retval += report(options, tree_oid, OBJ_TREE,
664 FSCK_MSG_BAD_TREE,
665 "cannot be parsed as a tree");
666 break;
669 switch (mode) {
671 * Standard modes..
673 case S_IFREG | 0755:
674 case S_IFREG | 0644:
675 case S_IFLNK:
676 case S_IFDIR:
677 case S_IFGITLINK:
678 break;
680 * This is nonstandard, but we had a few of these
681 * early on when we honored the full set of mode
682 * bits..
684 case S_IFREG | 0664:
685 if (!options->strict)
686 break;
687 /* fallthrough */
688 default:
689 has_bad_modes = 1;
692 if (o_name) {
693 switch (verify_ordered(o_mode, o_name, mode, name,
694 &df_dup_candidates)) {
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;
706 o_mode = mode;
707 o_name = name;
710 name_stack_clear(&df_dup_candidates);
712 if (has_null_sha1)
713 retval += report(options, tree_oid, OBJ_TREE,
714 FSCK_MSG_NULL_SHA1,
715 "contains entries pointing to null sha1");
716 if (has_full_path)
717 retval += report(options, tree_oid, OBJ_TREE,
718 FSCK_MSG_FULL_PATHNAME,
719 "contains full pathnames");
720 if (has_empty_name)
721 retval += report(options, tree_oid, OBJ_TREE,
722 FSCK_MSG_EMPTY_NAME,
723 "contains empty pathname");
724 if (has_dot)
725 retval += report(options, tree_oid, OBJ_TREE,
726 FSCK_MSG_HAS_DOT,
727 "contains '.'");
728 if (has_dotdot)
729 retval += report(options, tree_oid, OBJ_TREE,
730 FSCK_MSG_HAS_DOTDOT,
731 "contains '..'");
732 if (has_dotgit)
733 retval += report(options, tree_oid, OBJ_TREE,
734 FSCK_MSG_HAS_DOTGIT,
735 "contains '.git'");
736 if (has_zero_pad)
737 retval += report(options, tree_oid, OBJ_TREE,
738 FSCK_MSG_ZERO_PADDED_FILEMODE,
739 "contains zero-padded file modes");
740 if (has_bad_modes)
741 retval += report(options, tree_oid, OBJ_TREE,
742 FSCK_MSG_BAD_FILEMODE,
743 "contains bad file modes");
744 if (has_dup_entries)
745 retval += report(options, tree_oid, OBJ_TREE,
746 FSCK_MSG_DUPLICATE_ENTRIES,
747 "contains duplicate file entries");
748 if (not_properly_sorted)
749 retval += report(options, tree_oid, OBJ_TREE,
750 FSCK_MSG_TREE_NOT_SORTED,
751 "not properly sorted");
752 return retval;
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.
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:
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)
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().
772 static int verify_headers(const void *data, unsigned long size,
773 const struct object_id *oid, enum object_type type,
774 struct fsck_options *options)
776 const char *buffer = (const char *)data;
777 unsigned long i;
779 for (i = 0; i < size; i++) {
780 switch (buffer[i]) {
781 case '\0':
782 return report(options, oid, type,
783 FSCK_MSG_NUL_IN_HEADER,
784 "unterminated header: NUL at offset %ld", i);
785 case '\n':
786 if (i + 1 < size && buffer[i + 1] == '\n')
787 return 0;
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.
797 if (size && buffer[size - 1] == '\n')
798 return 0;
800 return report(options, oid, type,
801 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
804 static int fsck_ident(const char **ident,
805 const struct object_id *oid, enum object_type type,
806 struct fsck_options *options)
808 const char *p = *ident;
809 char *end;
811 *ident = strchrnul(*ident, '\n');
812 if (**ident == '\n')
813 (*ident)++;
815 if (*p == '<')
816 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
817 p += strcspn(p, "<>\n");
818 if (*p == '>')
819 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
820 if (*p != '<')
821 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
822 if (p[-1] != ' ')
823 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
824 p++;
825 p += strcspn(p, "<>\n");
826 if (*p != '>')
827 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
828 p++;
829 if (*p != ' ')
830 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
831 p++;
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.
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");
846 if (*p == '0' && p[1] != ' ')
847 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
848 if (date_overflows(parse_timestamp(p, &end, 10)))
849 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
850 if ((end == p || *end != ' '))
851 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
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'))
859 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
860 p += 6;
861 return 0;
864 static int fsck_commit(const struct object_id *oid,
865 const char *buffer, unsigned long size,
866 struct fsck_options *options)
868 struct object_id tree_oid, parent_oid;
869 unsigned author_count;
870 int err;
871 const char *buffer_begin = buffer;
872 const char *buffer_end = buffer + size;
873 const char *p;
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.
880 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
881 return -1;
883 if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer))
884 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
885 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
886 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
887 if (err)
888 return err;
890 buffer = p + 1;
891 while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) {
892 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
893 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
894 if (err)
895 return err;
897 buffer = p + 1;
899 author_count = 0;
900 while (buffer < buffer_end && skip_prefix(buffer, "author ", &buffer)) {
901 author_count++;
902 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
903 if (err)
904 return err;
906 if (author_count < 1)
907 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
908 else if (author_count > 1)
909 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
910 if (err)
911 return err;
912 if (buffer >= buffer_end || !skip_prefix(buffer, "committer ", &buffer))
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);
915 if (err)
916 return err;
917 if (memchr(buffer_begin, '\0', size)) {
918 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
919 "NUL byte in the commit object body");
920 if (err)
921 return err;
923 return 0;
926 static int fsck_tag(const struct object_id *oid, const char *buffer,
927 unsigned long size, struct fsck_options *options)
929 struct object_id tagged_oid;
930 int tagged_type;
931 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
932 &tagged_type);
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)
940 int ret = 0;
941 char *eol;
942 struct strbuf sb = STRBUF_INIT;
943 const char *buffer_end = buffer + size;
944 const char *p;
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.
951 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
952 if (ret)
953 goto done;
955 if (buffer >= buffer_end || !skip_prefix(buffer, "object ", &buffer)) {
956 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
957 goto done;
959 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
960 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
961 if (ret)
962 goto done;
964 buffer = p + 1;
966 if (buffer >= buffer_end || !skip_prefix(buffer, "type ", &buffer)) {
967 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
968 goto done;
970 eol = memchr(buffer, '\n', buffer_end - buffer);
971 if (!eol) {
972 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
973 goto done;
975 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
976 if (*tagged_type < 0)
977 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
978 if (ret)
979 goto done;
980 buffer = eol + 1;
982 if (buffer >= buffer_end || !skip_prefix(buffer, "tag ", &buffer)) {
983 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
984 goto done;
986 eol = memchr(buffer, '\n', buffer_end - buffer);
987 if (!eol) {
988 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
989 goto done;
991 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
992 if (check_refname_format(sb.buf, 0)) {
993 ret = report(options, oid, OBJ_TAG,
994 FSCK_MSG_BAD_TAG_NAME,
995 "invalid 'tag' name: %.*s",
996 (int)(eol - buffer), buffer);
997 if (ret)
998 goto done;
1000 buffer = eol + 1;
1002 if (buffer >= buffer_end || !skip_prefix(buffer, "tagger ", &buffer)) {
1003 /* early tags do not contain 'tagger' lines; warn only */
1004 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
1005 if (ret)
1006 goto done;
1008 else
1009 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
1011 if (buffer < buffer_end && !starts_with(buffer, "\n")) {
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.
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;
1024 done:
1025 strbuf_release(&sb);
1026 return ret;
1029 static int starts_with_dot_slash(const char *const path)
1031 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
1032 PATH_MATCH_XPLATFORM);
1035 static int starts_with_dot_dot_slash(const char *const path)
1037 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
1038 PATH_MATCH_XPLATFORM);
1041 static int submodule_url_is_relative(const char *url)
1043 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
1047 * Count directory components that a relative submodule URL should chop
1048 * from the remote_url it is to be resolved against.
1050 * In other words, this counts "../" components at the start of a
1051 * submodule URL.
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.
1057 static int count_leading_dotdots(const char *url, const char **out)
1059 int result = 0;
1060 while (1) {
1061 if (starts_with_dot_dot_slash(url)) {
1062 result++;
1063 url += strlen("../");
1064 continue;
1066 if (starts_with_dot_slash(url)) {
1067 url += strlen("./");
1068 continue;
1070 *out = url;
1071 return result;
1075 * Check whether a transport is implemented by git-remote-curl.
1077 * If it is, returns 1 and writes the URL that would be passed to
1078 * git-remote-curl to the "out" parameter.
1080 * Otherwise, returns 0 and leaves "out" untouched.
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
1087 * This is for use in checking for previously exploitable bugs that
1088 * required a submodule URL to be passed to git-remote-curl.
1090 static int url_to_curl_url(const char *url, const char **out)
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.
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;
1110 return 0;
1113 static int check_submodule_url(const char *url)
1115 const char *curl_url;
1117 if (looks_like_command_line_option(url))
1118 return -1;
1120 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
1121 char *decoded;
1122 const char *next;
1123 int has_nl;
1126 * This could be appended to an http URL and url-decoded;
1127 * check for malicious characters.
1129 decoded = url_decode(url);
1130 has_nl = !!strchr(decoded, '\n');
1132 free(decoded);
1133 if (has_nl)
1134 return -1;
1137 * URLs which escape their root via "../" can overwrite
1138 * the host field and previous components, resolving to
1139 * URLs like https::example.com/submodule.git and
1140 * https:///example.com/submodule.git that were
1141 * susceptible to CVE-2020-11008.
1143 if (count_leading_dotdots(url, &next) > 0 &&
1144 (*next == ':' || *next == '/'))
1145 return -1;
1148 else if (url_to_curl_url(url, &curl_url)) {
1149 struct credential c = CREDENTIAL_INIT;
1150 int ret = 0;
1151 if (credential_from_url_gently(&c, curl_url, 1) ||
1152 !*c.host)
1153 ret = -1;
1154 credential_clear(&c);
1155 return ret;
1158 return 0;
1161 struct fsck_gitmodules_data {
1162 const struct object_id *oid;
1163 struct fsck_options *options;
1164 int ret;
1167 static int fsck_gitmodules_fn(const char *var, const char *value,
1168 const struct config_context *ctx UNUSED,
1169 void *vdata)
1171 struct fsck_gitmodules_data *data = vdata;
1172 const char *subsection, *key;
1173 size_t subsection_len;
1174 char *name;
1176 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1177 !subsection)
1178 return 0;
1180 name = xmemdupz(subsection, subsection_len);
1181 if (check_submodule_name(name) < 0)
1182 data->ret |= report(data->options,
1183 data->oid, OBJ_BLOB,
1184 FSCK_MSG_GITMODULES_NAME,
1185 "disallowed submodule name: %s",
1186 name);
1187 if (!strcmp(key, "url") && value &&
1188 check_submodule_url(value) < 0)
1189 data->ret |= report(data->options,
1190 data->oid, OBJ_BLOB,
1191 FSCK_MSG_GITMODULES_URL,
1192 "disallowed submodule url: %s",
1193 value);
1194 if (!strcmp(key, "path") && value &&
1195 looks_like_command_line_option(value))
1196 data->ret |= report(data->options,
1197 data->oid, OBJ_BLOB,
1198 FSCK_MSG_GITMODULES_PATH,
1199 "disallowed submodule path: %s",
1200 value);
1201 if (!strcmp(key, "update") && value &&
1202 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
1203 data->ret |= report(data->options, data->oid, OBJ_BLOB,
1204 FSCK_MSG_GITMODULES_UPDATE,
1205 "disallowed submodule update setting: %s",
1206 value);
1207 free(name);
1209 return 0;
1212 static int fsck_blob(const struct object_id *oid, const char *buf,
1213 unsigned long size, struct fsck_options *options)
1215 int ret = 0;
1217 if (object_on_skiplist(options, oid))
1218 return 0;
1220 if (oidset_contains(&options->gitmodules_found, oid)) {
1221 struct config_options config_opts = { 0 };
1222 struct fsck_gitmodules_data data;
1224 oidset_insert(&options->gitmodules_done, oid);
1226 if (!buf) {
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.
1232 return report(options, oid, OBJ_BLOB,
1233 FSCK_MSG_GITMODULES_LARGE,
1234 ".gitmodules too large to parse");
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,
1242 ".gitmodules", buf, size, &data,
1243 CONFIG_SCOPE_UNKNOWN, &config_opts))
1244 data.ret |= report(options, oid, OBJ_BLOB,
1245 FSCK_MSG_GITMODULES_PARSE,
1246 "could not parse gitmodules blob");
1247 ret |= data.ret;
1250 if (oidset_contains(&options->gitattributes_found, oid)) {
1251 const char *ptr;
1253 oidset_insert(&options->gitattributes_done, oid);
1255 if (!buf || size > ATTR_MAX_FILE_SIZE) {
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.
1261 return report(options, oid, OBJ_BLOB,
1262 FSCK_MSG_GITATTRIBUTES_LARGE,
1263 ".gitattributes too large to parse");
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;
1275 ptr = *eol ? eol + 1 : eol;
1279 return ret;
1282 int fsck_object(struct object *obj, void *data, unsigned long size,
1283 struct fsck_options *options)
1285 if (!obj)
1286 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1288 return fsck_buffer(&obj->oid, obj->type, data, size, options);
1291 int fsck_buffer(const struct object_id *oid, enum object_type type,
1292 void *data, unsigned long size,
1293 struct fsck_options *options)
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);
1304 return report(options, oid, type,
1305 FSCK_MSG_UNKNOWN_TYPE,
1306 "unknown type '%d' (internal fsck error)",
1307 type);
1310 int fsck_error_function(struct fsck_options *o,
1311 const struct object_id *oid,
1312 enum object_type object_type UNUSED,
1313 enum fsck_msg_type msg_type,
1314 enum fsck_msg_id msg_id UNUSED,
1315 const char *message)
1317 if (msg_type == FSCK_WARN) {
1318 warning("object %s: %s", fsck_describe_object(o, oid), message);
1319 return 0;
1321 error("object %s: %s", fsck_describe_object(o, oid), message);
1322 return 1;
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)
1329 int ret = 0;
1330 struct oidset_iter iter;
1331 const struct object_id *oid;
1333 oidset_iter_init(blobs_found, &iter);
1334 while ((oid = oidset_iter_next(&iter))) {
1335 enum object_type type;
1336 unsigned long size;
1337 char *buf;
1339 if (oidset_contains(blobs_done, oid))
1340 continue;
1342 buf = repo_read_object_file(the_repository, oid, &type, &size);
1343 if (!buf) {
1344 if (is_promisor_object(oid))
1345 continue;
1346 ret |= report(options,
1347 oid, OBJ_BLOB, msg_missing,
1348 "unable to read %s blob", blob_type);
1349 continue;
1352 if (type == OBJ_BLOB)
1353 ret |= fsck_blob(oid, buf, size, options);
1354 else
1355 ret |= report(options, oid, type, msg_type,
1356 "non-blob found at %s", blob_type);
1357 free(buf);
1360 oidset_clear(blobs_found);
1361 oidset_clear(blobs_done);
1363 return ret;
1366 int fsck_finish(struct fsck_options *options)
1368 int ret = 0;
1370 ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
1371 FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
1372 options, ".gitmodules");
1373 ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
1374 FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
1375 options, ".gitattributes");
1377 return ret;
1380 int git_fsck_config(const char *var, const char *value,
1381 const struct config_context *ctx, void *cb)
1383 struct fsck_options *options = cb;
1384 if (strcmp(var, "fsck.skiplist") == 0) {
1385 const char *path;
1386 struct strbuf sb = STRBUF_INIT;
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;
1397 if (skip_prefix(var, "fsck.", &var)) {
1398 fsck_set_msg_type(options, var, value);
1399 return 0;
1402 return git_default_config(var, value, ctx, cb);
1406 * Custom error callbacks that are used in more than one place.
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)
1416 if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1417 puts(oid_to_hex(oid));
1418 return 0;
1420 return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);