fsck.con commit fsck: detect submodule urls starting with dash (a124133)
   1#include "cache.h"
   2#include "object.h"
   3#include "blob.h"
   4#include "tree.h"
   5#include "tree-walk.h"
   6#include "commit.h"
   7#include "tag.h"
   8#include "fsck.h"
   9#include "refs.h"
  10#include "utf8.h"
  11#include "sha1-array.h"
  12#include "decorate.h"
  13#include "oidset.h"
  14#include "packfile.h"
  15#include "submodule-config.h"
  16#include "config.h"
  17
  18static struct oidset gitmodules_found = OIDSET_INIT;
  19static struct oidset gitmodules_done = OIDSET_INIT;
  20
  21#define FSCK_FATAL -1
  22#define FSCK_INFO -2
  23
  24#define FOREACH_MSG_ID(FUNC) \
  25        /* fatal errors */ \
  26        FUNC(NUL_IN_HEADER, FATAL) \
  27        FUNC(UNTERMINATED_HEADER, FATAL) \
  28        /* errors */ \
  29        FUNC(BAD_DATE, ERROR) \
  30        FUNC(BAD_DATE_OVERFLOW, ERROR) \
  31        FUNC(BAD_EMAIL, ERROR) \
  32        FUNC(BAD_NAME, ERROR) \
  33        FUNC(BAD_OBJECT_SHA1, ERROR) \
  34        FUNC(BAD_PARENT_SHA1, ERROR) \
  35        FUNC(BAD_TAG_OBJECT, ERROR) \
  36        FUNC(BAD_TIMEZONE, ERROR) \
  37        FUNC(BAD_TREE, ERROR) \
  38        FUNC(BAD_TREE_SHA1, ERROR) \
  39        FUNC(BAD_TYPE, ERROR) \
  40        FUNC(DUPLICATE_ENTRIES, ERROR) \
  41        FUNC(MISSING_AUTHOR, ERROR) \
  42        FUNC(MISSING_COMMITTER, ERROR) \
  43        FUNC(MISSING_EMAIL, ERROR) \
  44        FUNC(MISSING_GRAFT, ERROR) \
  45        FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
  46        FUNC(MISSING_OBJECT, ERROR) \
  47        FUNC(MISSING_PARENT, ERROR) \
  48        FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
  49        FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
  50        FUNC(MISSING_TAG, ERROR) \
  51        FUNC(MISSING_TAG_ENTRY, ERROR) \
  52        FUNC(MISSING_TAG_OBJECT, ERROR) \
  53        FUNC(MISSING_TREE, ERROR) \
  54        FUNC(MISSING_TREE_OBJECT, ERROR) \
  55        FUNC(MISSING_TYPE, ERROR) \
  56        FUNC(MISSING_TYPE_ENTRY, ERROR) \
  57        FUNC(MULTIPLE_AUTHORS, ERROR) \
  58        FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
  59        FUNC(TREE_NOT_SORTED, ERROR) \
  60        FUNC(UNKNOWN_TYPE, ERROR) \
  61        FUNC(ZERO_PADDED_DATE, ERROR) \
  62        FUNC(GITMODULES_MISSING, ERROR) \
  63        FUNC(GITMODULES_BLOB, ERROR) \
  64        FUNC(GITMODULES_PARSE, ERROR) \
  65        FUNC(GITMODULES_NAME, ERROR) \
  66        FUNC(GITMODULES_SYMLINK, ERROR) \
  67        FUNC(GITMODULES_URL, ERROR) \
  68        /* warnings */ \
  69        FUNC(BAD_FILEMODE, WARN) \
  70        FUNC(EMPTY_NAME, WARN) \
  71        FUNC(FULL_PATHNAME, WARN) \
  72        FUNC(HAS_DOT, WARN) \
  73        FUNC(HAS_DOTDOT, WARN) \
  74        FUNC(HAS_DOTGIT, WARN) \
  75        FUNC(NULL_SHA1, WARN) \
  76        FUNC(ZERO_PADDED_FILEMODE, WARN) \
  77        FUNC(NUL_IN_COMMIT, WARN) \
  78        /* infos (reported as warnings, but ignored by default) */ \
  79        FUNC(BAD_TAG_NAME, INFO) \
  80        FUNC(MISSING_TAGGER_ENTRY, INFO)
  81
  82#define MSG_ID(id, msg_type) FSCK_MSG_##id,
  83enum fsck_msg_id {
  84        FOREACH_MSG_ID(MSG_ID)
  85        FSCK_MSG_MAX
  86};
  87#undef MSG_ID
  88
  89#define STR(x) #x
  90#define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
  91static struct {
  92        const char *id_string;
  93        const char *downcased;
  94        int msg_type;
  95} msg_id_info[FSCK_MSG_MAX + 1] = {
  96        FOREACH_MSG_ID(MSG_ID)
  97        { NULL, NULL, -1 }
  98};
  99#undef MSG_ID
 100
 101static int parse_msg_id(const char *text)
 102{
 103        int i;
 104
 105        if (!msg_id_info[0].downcased) {
 106                /* convert id_string to lower case, without underscores. */
 107                for (i = 0; i < FSCK_MSG_MAX; i++) {
 108                        const char *p = msg_id_info[i].id_string;
 109                        int len = strlen(p);
 110                        char *q = xmalloc(len);
 111
 112                        msg_id_info[i].downcased = q;
 113                        while (*p)
 114                                if (*p == '_')
 115                                        p++;
 116                                else
 117                                        *(q)++ = tolower(*(p)++);
 118                        *q = '\0';
 119                }
 120        }
 121
 122        for (i = 0; i < FSCK_MSG_MAX; i++)
 123                if (!strcmp(text, msg_id_info[i].downcased))
 124                        return i;
 125
 126        return -1;
 127}
 128
 129static int fsck_msg_type(enum fsck_msg_id msg_id,
 130        struct fsck_options *options)
 131{
 132        int msg_type;
 133
 134        assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
 135
 136        if (options->msg_type)
 137                msg_type = options->msg_type[msg_id];
 138        else {
 139                msg_type = msg_id_info[msg_id].msg_type;
 140                if (options->strict && msg_type == FSCK_WARN)
 141                        msg_type = FSCK_ERROR;
 142        }
 143
 144        return msg_type;
 145}
 146
 147static void init_skiplist(struct fsck_options *options, const char *path)
 148{
 149        static struct oid_array skiplist = OID_ARRAY_INIT;
 150        int sorted, fd;
 151        char buffer[GIT_MAX_HEXSZ + 1];
 152        struct object_id oid;
 153
 154        if (options->skiplist)
 155                sorted = options->skiplist->sorted;
 156        else {
 157                sorted = 1;
 158                options->skiplist = &skiplist;
 159        }
 160
 161        fd = open(path, O_RDONLY);
 162        if (fd < 0)
 163                die("Could not open skip list: %s", path);
 164        for (;;) {
 165                const char *p;
 166                int result = read_in_full(fd, buffer, sizeof(buffer));
 167                if (result < 0)
 168                        die_errno("Could not read '%s'", path);
 169                if (!result)
 170                        break;
 171                if (parse_oid_hex(buffer, &oid, &p) || *p != '\n')
 172                        die("Invalid SHA-1: %s", buffer);
 173                oid_array_append(&skiplist, &oid);
 174                if (sorted && skiplist.nr > 1 &&
 175                                oidcmp(&skiplist.oid[skiplist.nr - 2],
 176                                       &oid) > 0)
 177                        sorted = 0;
 178        }
 179        close(fd);
 180
 181        if (sorted)
 182                skiplist.sorted = 1;
 183}
 184
 185static int parse_msg_type(const char *str)
 186{
 187        if (!strcmp(str, "error"))
 188                return FSCK_ERROR;
 189        else if (!strcmp(str, "warn"))
 190                return FSCK_WARN;
 191        else if (!strcmp(str, "ignore"))
 192                return FSCK_IGNORE;
 193        else
 194                die("Unknown fsck message type: '%s'", str);
 195}
 196
 197int is_valid_msg_type(const char *msg_id, const char *msg_type)
 198{
 199        if (parse_msg_id(msg_id) < 0)
 200                return 0;
 201        parse_msg_type(msg_type);
 202        return 1;
 203}
 204
 205void fsck_set_msg_type(struct fsck_options *options,
 206                const char *msg_id, const char *msg_type)
 207{
 208        int id = parse_msg_id(msg_id), type;
 209
 210        if (id < 0)
 211                die("Unhandled message id: %s", msg_id);
 212        type = parse_msg_type(msg_type);
 213
 214        if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
 215                die("Cannot demote %s to %s", msg_id, msg_type);
 216
 217        if (!options->msg_type) {
 218                int i;
 219                int *msg_type;
 220                ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
 221                for (i = 0; i < FSCK_MSG_MAX; i++)
 222                        msg_type[i] = fsck_msg_type(i, options);
 223                options->msg_type = msg_type;
 224        }
 225
 226        options->msg_type[id] = type;
 227}
 228
 229void fsck_set_msg_types(struct fsck_options *options, const char *values)
 230{
 231        char *buf = xstrdup(values), *to_free = buf;
 232        int done = 0;
 233
 234        while (!done) {
 235                int len = strcspn(buf, " ,|"), equal;
 236
 237                done = !buf[len];
 238                if (!len) {
 239                        buf++;
 240                        continue;
 241                }
 242                buf[len] = '\0';
 243
 244                for (equal = 0;
 245                     equal < len && buf[equal] != '=' && buf[equal] != ':';
 246                     equal++)
 247                        buf[equal] = tolower(buf[equal]);
 248                buf[equal] = '\0';
 249
 250                if (!strcmp(buf, "skiplist")) {
 251                        if (equal == len)
 252                                die("skiplist requires a path");
 253                        init_skiplist(options, buf + equal + 1);
 254                        buf += len + 1;
 255                        continue;
 256                }
 257
 258                if (equal == len)
 259                        die("Missing '=': '%s'", buf);
 260
 261                fsck_set_msg_type(options, buf, buf + equal + 1);
 262                buf += len + 1;
 263        }
 264        free(to_free);
 265}
 266
 267static void append_msg_id(struct strbuf *sb, const char *msg_id)
 268{
 269        for (;;) {
 270                char c = *(msg_id)++;
 271
 272                if (!c)
 273                        break;
 274                if (c != '_')
 275                        strbuf_addch(sb, tolower(c));
 276                else {
 277                        assert(*msg_id);
 278                        strbuf_addch(sb, *(msg_id)++);
 279                }
 280        }
 281
 282        strbuf_addstr(sb, ": ");
 283}
 284
 285__attribute__((format (printf, 4, 5)))
 286static int report(struct fsck_options *options, struct object *object,
 287        enum fsck_msg_id id, const char *fmt, ...)
 288{
 289        va_list ap;
 290        struct strbuf sb = STRBUF_INIT;
 291        int msg_type = fsck_msg_type(id, options), result;
 292
 293        if (msg_type == FSCK_IGNORE)
 294                return 0;
 295
 296        if (options->skiplist && object &&
 297                        oid_array_lookup(options->skiplist, &object->oid) >= 0)
 298                return 0;
 299
 300        if (msg_type == FSCK_FATAL)
 301                msg_type = FSCK_ERROR;
 302        else if (msg_type == FSCK_INFO)
 303                msg_type = FSCK_WARN;
 304
 305        append_msg_id(&sb, msg_id_info[id].id_string);
 306
 307        va_start(ap, fmt);
 308        strbuf_vaddf(&sb, fmt, ap);
 309        result = options->error_func(options, object, msg_type, sb.buf);
 310        strbuf_release(&sb);
 311        va_end(ap);
 312
 313        return result;
 314}
 315
 316static char *get_object_name(struct fsck_options *options, struct object *obj)
 317{
 318        if (!options->object_names)
 319                return NULL;
 320        return lookup_decoration(options->object_names, obj);
 321}
 322
 323static void put_object_name(struct fsck_options *options, struct object *obj,
 324        const char *fmt, ...)
 325{
 326        va_list ap;
 327        struct strbuf buf = STRBUF_INIT;
 328        char *existing;
 329
 330        if (!options->object_names)
 331                return;
 332        existing = lookup_decoration(options->object_names, obj);
 333        if (existing)
 334                return;
 335        va_start(ap, fmt);
 336        strbuf_vaddf(&buf, fmt, ap);
 337        add_decoration(options->object_names, obj, strbuf_detach(&buf, NULL));
 338        va_end(ap);
 339}
 340
 341static const char *describe_object(struct fsck_options *o, struct object *obj)
 342{
 343        static struct strbuf buf = STRBUF_INIT;
 344        char *name;
 345
 346        strbuf_reset(&buf);
 347        strbuf_addstr(&buf, oid_to_hex(&obj->oid));
 348        if (o->object_names && (name = lookup_decoration(o->object_names, obj)))
 349                strbuf_addf(&buf, " (%s)", name);
 350
 351        return buf.buf;
 352}
 353
 354static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
 355{
 356        struct tree_desc desc;
 357        struct name_entry entry;
 358        int res = 0;
 359        const char *name;
 360
 361        if (parse_tree(tree))
 362                return -1;
 363
 364        name = get_object_name(options, &tree->object);
 365        if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
 366                return -1;
 367        while (tree_entry_gently(&desc, &entry)) {
 368                struct object *obj;
 369                int result;
 370
 371                if (S_ISGITLINK(entry.mode))
 372                        continue;
 373
 374                if (S_ISDIR(entry.mode)) {
 375                        obj = (struct object *)lookup_tree(entry.oid);
 376                        if (name && obj)
 377                                put_object_name(options, obj, "%s%s/", name,
 378                                        entry.path);
 379                        result = options->walk(obj, OBJ_TREE, data, options);
 380                }
 381                else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
 382                        obj = (struct object *)lookup_blob(entry.oid);
 383                        if (name && obj)
 384                                put_object_name(options, obj, "%s%s", name,
 385                                        entry.path);
 386                        result = options->walk(obj, OBJ_BLOB, data, options);
 387                }
 388                else {
 389                        result = error("in tree %s: entry %s has bad mode %.6o",
 390                                        describe_object(options, &tree->object), entry.path, entry.mode);
 391                }
 392                if (result < 0)
 393                        return result;
 394                if (!res)
 395                        res = result;
 396        }
 397        return res;
 398}
 399
 400static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
 401{
 402        int counter = 0, generation = 0, name_prefix_len = 0;
 403        struct commit_list *parents;
 404        int res;
 405        int result;
 406        const char *name;
 407
 408        if (parse_commit(commit))
 409                return -1;
 410
 411        name = get_object_name(options, &commit->object);
 412        if (name)
 413                put_object_name(options, &commit->tree->object, "%s:", name);
 414
 415        result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
 416        if (result < 0)
 417                return result;
 418        res = result;
 419
 420        parents = commit->parents;
 421        if (name && parents) {
 422                int len = strlen(name), power;
 423
 424                if (len && name[len - 1] == '^') {
 425                        generation = 1;
 426                        name_prefix_len = len - 1;
 427                }
 428                else { /* parse ~<generation> suffix */
 429                        for (generation = 0, power = 1;
 430                             len && isdigit(name[len - 1]);
 431                             power *= 10)
 432                                generation += power * (name[--len] - '0');
 433                        if (power > 1 && len && name[len - 1] == '~')
 434                                name_prefix_len = len - 1;
 435                }
 436        }
 437
 438        while (parents) {
 439                if (name) {
 440                        struct object *obj = &parents->item->object;
 441
 442                        if (++counter > 1)
 443                                put_object_name(options, obj, "%s^%d",
 444                                        name, counter);
 445                        else if (generation > 0)
 446                                put_object_name(options, obj, "%.*s~%d",
 447                                        name_prefix_len, name, generation + 1);
 448                        else
 449                                put_object_name(options, obj, "%s^", name);
 450                }
 451                result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
 452                if (result < 0)
 453                        return result;
 454                if (!res)
 455                        res = result;
 456                parents = parents->next;
 457        }
 458        return res;
 459}
 460
 461static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
 462{
 463        char *name = get_object_name(options, &tag->object);
 464
 465        if (parse_tag(tag))
 466                return -1;
 467        if (name)
 468                put_object_name(options, tag->tagged, "%s", name);
 469        return options->walk(tag->tagged, OBJ_ANY, data, options);
 470}
 471
 472int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
 473{
 474        if (!obj)
 475                return -1;
 476
 477        if (obj->type == OBJ_NONE)
 478                parse_object(&obj->oid);
 479
 480        switch (obj->type) {
 481        case OBJ_BLOB:
 482                return 0;
 483        case OBJ_TREE:
 484                return fsck_walk_tree((struct tree *)obj, data, options);
 485        case OBJ_COMMIT:
 486                return fsck_walk_commit((struct commit *)obj, data, options);
 487        case OBJ_TAG:
 488                return fsck_walk_tag((struct tag *)obj, data, options);
 489        default:
 490                error("Unknown object type for %s", describe_object(options, obj));
 491                return -1;
 492        }
 493}
 494
 495/*
 496 * The entries in a tree are ordered in the _path_ order,
 497 * which means that a directory entry is ordered by adding
 498 * a slash to the end of it.
 499 *
 500 * So a directory called "a" is ordered _after_ a file
 501 * called "a.c", because "a/" sorts after "a.c".
 502 */
 503#define TREE_UNORDERED (-1)
 504#define TREE_HAS_DUPS  (-2)
 505
 506static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 507{
 508        int len1 = strlen(name1);
 509        int len2 = strlen(name2);
 510        int len = len1 < len2 ? len1 : len2;
 511        unsigned char c1, c2;
 512        int cmp;
 513
 514        cmp = memcmp(name1, name2, len);
 515        if (cmp < 0)
 516                return 0;
 517        if (cmp > 0)
 518                return TREE_UNORDERED;
 519
 520        /*
 521         * Ok, the first <len> characters are the same.
 522         * Now we need to order the next one, but turn
 523         * a '\0' into a '/' for a directory entry.
 524         */
 525        c1 = name1[len];
 526        c2 = name2[len];
 527        if (!c1 && !c2)
 528                /*
 529                 * git-write-tree used to write out a nonsense tree that has
 530                 * entries with the same name, one blob and one tree.  Make
 531                 * sure we do not have duplicate entries.
 532                 */
 533                return TREE_HAS_DUPS;
 534        if (!c1 && S_ISDIR(mode1))
 535                c1 = '/';
 536        if (!c2 && S_ISDIR(mode2))
 537                c2 = '/';
 538        return c1 < c2 ? 0 : TREE_UNORDERED;
 539}
 540
 541static int fsck_tree(struct tree *item, struct fsck_options *options)
 542{
 543        int retval = 0;
 544        int has_null_sha1 = 0;
 545        int has_full_path = 0;
 546        int has_empty_name = 0;
 547        int has_dot = 0;
 548        int has_dotdot = 0;
 549        int has_dotgit = 0;
 550        int has_zero_pad = 0;
 551        int has_bad_modes = 0;
 552        int has_dup_entries = 0;
 553        int not_properly_sorted = 0;
 554        struct tree_desc desc;
 555        unsigned o_mode;
 556        const char *o_name;
 557
 558        if (init_tree_desc_gently(&desc, item->buffer, item->size)) {
 559                retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
 560                return retval;
 561        }
 562
 563        o_mode = 0;
 564        o_name = NULL;
 565
 566        while (desc.size) {
 567                unsigned mode;
 568                const char *name;
 569                const struct object_id *oid;
 570
 571                oid = tree_entry_extract(&desc, &name, &mode);
 572
 573                has_null_sha1 |= is_null_oid(oid);
 574                has_full_path |= !!strchr(name, '/');
 575                has_empty_name |= !*name;
 576                has_dot |= !strcmp(name, ".");
 577                has_dotdot |= !strcmp(name, "..");
 578                has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
 579                has_zero_pad |= *(char *)desc.buffer == '0';
 580
 581                if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
 582                        if (!S_ISLNK(mode))
 583                                oidset_insert(&gitmodules_found, oid);
 584                        else
 585                                retval += report(options, &item->object,
 586                                                 FSCK_MSG_GITMODULES_SYMLINK,
 587                                                 ".gitmodules is a symbolic link");
 588                }
 589
 590                if (update_tree_entry_gently(&desc)) {
 591                        retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
 592                        break;
 593                }
 594
 595                switch (mode) {
 596                /*
 597                 * Standard modes..
 598                 */
 599                case S_IFREG | 0755:
 600                case S_IFREG | 0644:
 601                case S_IFLNK:
 602                case S_IFDIR:
 603                case S_IFGITLINK:
 604                        break;
 605                /*
 606                 * This is nonstandard, but we had a few of these
 607                 * early on when we honored the full set of mode
 608                 * bits..
 609                 */
 610                case S_IFREG | 0664:
 611                        if (!options->strict)
 612                                break;
 613                        /* fallthrough */
 614                default:
 615                        has_bad_modes = 1;
 616                }
 617
 618                if (o_name) {
 619                        switch (verify_ordered(o_mode, o_name, mode, name)) {
 620                        case TREE_UNORDERED:
 621                                not_properly_sorted = 1;
 622                                break;
 623                        case TREE_HAS_DUPS:
 624                                has_dup_entries = 1;
 625                                break;
 626                        default:
 627                                break;
 628                        }
 629                }
 630
 631                o_mode = mode;
 632                o_name = name;
 633        }
 634
 635        if (has_null_sha1)
 636                retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
 637        if (has_full_path)
 638                retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
 639        if (has_empty_name)
 640                retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
 641        if (has_dot)
 642                retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
 643        if (has_dotdot)
 644                retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
 645        if (has_dotgit)
 646                retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
 647        if (has_zero_pad)
 648                retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
 649        if (has_bad_modes)
 650                retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
 651        if (has_dup_entries)
 652                retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
 653        if (not_properly_sorted)
 654                retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
 655        return retval;
 656}
 657
 658static int verify_headers(const void *data, unsigned long size,
 659                          struct object *obj, struct fsck_options *options)
 660{
 661        const char *buffer = (const char *)data;
 662        unsigned long i;
 663
 664        for (i = 0; i < size; i++) {
 665                switch (buffer[i]) {
 666                case '\0':
 667                        return report(options, obj,
 668                                FSCK_MSG_NUL_IN_HEADER,
 669                                "unterminated header: NUL at offset %ld", i);
 670                case '\n':
 671                        if (i + 1 < size && buffer[i + 1] == '\n')
 672                                return 0;
 673                }
 674        }
 675
 676        /*
 677         * We did not find double-LF that separates the header
 678         * and the body.  Not having a body is not a crime but
 679         * we do want to see the terminating LF for the last header
 680         * line.
 681         */
 682        if (size && buffer[size - 1] == '\n')
 683                return 0;
 684
 685        return report(options, obj,
 686                FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
 687}
 688
 689static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
 690{
 691        const char *p = *ident;
 692        char *end;
 693
 694        *ident = strchrnul(*ident, '\n');
 695        if (**ident == '\n')
 696                (*ident)++;
 697
 698        if (*p == '<')
 699                return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 700        p += strcspn(p, "<>\n");
 701        if (*p == '>')
 702                return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
 703        if (*p != '<')
 704                return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
 705        if (p[-1] != ' ')
 706                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 707        p++;
 708        p += strcspn(p, "<>\n");
 709        if (*p != '>')
 710                return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
 711        p++;
 712        if (*p != ' ')
 713                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
 714        p++;
 715        if (*p == '0' && p[1] != ' ')
 716                return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
 717        if (date_overflows(parse_timestamp(p, &end, 10)))
 718                return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
 719        if ((end == p || *end != ' '))
 720                return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
 721        p = end + 1;
 722        if ((*p != '+' && *p != '-') ||
 723            !isdigit(p[1]) ||
 724            !isdigit(p[2]) ||
 725            !isdigit(p[3]) ||
 726            !isdigit(p[4]) ||
 727            (p[5] != '\n'))
 728                return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
 729        p += 6;
 730        return 0;
 731}
 732
 733static int fsck_commit_buffer(struct commit *commit, const char *buffer,
 734        unsigned long size, struct fsck_options *options)
 735{
 736        unsigned char tree_sha1[20], sha1[20];
 737        struct commit_graft *graft;
 738        unsigned parent_count, parent_line_count = 0, author_count;
 739        int err;
 740        const char *buffer_begin = buffer;
 741
 742        if (verify_headers(buffer, size, &commit->object, options))
 743                return -1;
 744
 745        if (!skip_prefix(buffer, "tree ", &buffer))
 746                return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
 747        if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') {
 748                err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
 749                if (err)
 750                        return err;
 751        }
 752        buffer += 41;
 753        while (skip_prefix(buffer, "parent ", &buffer)) {
 754                if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
 755                        err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
 756                        if (err)
 757                                return err;
 758                }
 759                buffer += 41;
 760                parent_line_count++;
 761        }
 762        graft = lookup_commit_graft(&commit->object.oid);
 763        parent_count = commit_list_count(commit->parents);
 764        if (graft) {
 765                if (graft->nr_parent == -1 && !parent_count)
 766                        ; /* shallow commit */
 767                else if (graft->nr_parent != parent_count) {
 768                        err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
 769                        if (err)
 770                                return err;
 771                }
 772        } else {
 773                if (parent_count != parent_line_count) {
 774                        err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
 775                        if (err)
 776                                return err;
 777                }
 778        }
 779        author_count = 0;
 780        while (skip_prefix(buffer, "author ", &buffer)) {
 781                author_count++;
 782                err = fsck_ident(&buffer, &commit->object, options);
 783                if (err)
 784                        return err;
 785        }
 786        if (author_count < 1)
 787                err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
 788        else if (author_count > 1)
 789                err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
 790        if (err)
 791                return err;
 792        if (!skip_prefix(buffer, "committer ", &buffer))
 793                return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
 794        err = fsck_ident(&buffer, &commit->object, options);
 795        if (err)
 796                return err;
 797        if (!commit->tree) {
 798                err = report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
 799                if (err)
 800                        return err;
 801        }
 802        if (memchr(buffer_begin, '\0', size)) {
 803                err = report(options, &commit->object, FSCK_MSG_NUL_IN_COMMIT,
 804                             "NUL byte in the commit object body");
 805                if (err)
 806                        return err;
 807        }
 808        return 0;
 809}
 810
 811static int fsck_commit(struct commit *commit, const char *data,
 812        unsigned long size, struct fsck_options *options)
 813{
 814        const char *buffer = data ?  data : get_commit_buffer(commit, &size);
 815        int ret = fsck_commit_buffer(commit, buffer, size, options);
 816        if (!data)
 817                unuse_commit_buffer(commit, buffer);
 818        return ret;
 819}
 820
 821static int fsck_tag_buffer(struct tag *tag, const char *data,
 822        unsigned long size, struct fsck_options *options)
 823{
 824        unsigned char sha1[20];
 825        int ret = 0;
 826        const char *buffer;
 827        char *to_free = NULL, *eol;
 828        struct strbuf sb = STRBUF_INIT;
 829
 830        if (data)
 831                buffer = data;
 832        else {
 833                enum object_type type;
 834
 835                buffer = to_free =
 836                        read_sha1_file(tag->object.oid.hash, &type, &size);
 837                if (!buffer)
 838                        return report(options, &tag->object,
 839                                FSCK_MSG_MISSING_TAG_OBJECT,
 840                                "cannot read tag object");
 841
 842                if (type != OBJ_TAG) {
 843                        ret = report(options, &tag->object,
 844                                FSCK_MSG_TAG_OBJECT_NOT_TAG,
 845                                "expected tag got %s",
 846                            type_name(type));
 847                        goto done;
 848                }
 849        }
 850
 851        ret = verify_headers(buffer, size, &tag->object, options);
 852        if (ret)
 853                goto done;
 854
 855        if (!skip_prefix(buffer, "object ", &buffer)) {
 856                ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
 857                goto done;
 858        }
 859        if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
 860                ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
 861                if (ret)
 862                        goto done;
 863        }
 864        buffer += 41;
 865
 866        if (!skip_prefix(buffer, "type ", &buffer)) {
 867                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
 868                goto done;
 869        }
 870        eol = strchr(buffer, '\n');
 871        if (!eol) {
 872                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
 873                goto done;
 874        }
 875        if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
 876                ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
 877        if (ret)
 878                goto done;
 879        buffer = eol + 1;
 880
 881        if (!skip_prefix(buffer, "tag ", &buffer)) {
 882                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
 883                goto done;
 884        }
 885        eol = strchr(buffer, '\n');
 886        if (!eol) {
 887                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
 888                goto done;
 889        }
 890        strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
 891        if (check_refname_format(sb.buf, 0)) {
 892                ret = report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
 893                           "invalid 'tag' name: %.*s",
 894                           (int)(eol - buffer), buffer);
 895                if (ret)
 896                        goto done;
 897        }
 898        buffer = eol + 1;
 899
 900        if (!skip_prefix(buffer, "tagger ", &buffer)) {
 901                /* early tags do not contain 'tagger' lines; warn only */
 902                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
 903                if (ret)
 904                        goto done;
 905        }
 906        else
 907                ret = fsck_ident(&buffer, &tag->object, options);
 908
 909done:
 910        strbuf_release(&sb);
 911        free(to_free);
 912        return ret;
 913}
 914
 915static int fsck_tag(struct tag *tag, const char *data,
 916        unsigned long size, struct fsck_options *options)
 917{
 918        struct object *tagged = tag->tagged;
 919
 920        if (!tagged)
 921                return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
 922
 923        return fsck_tag_buffer(tag, data, size, options);
 924}
 925
 926struct fsck_gitmodules_data {
 927        struct object *obj;
 928        struct fsck_options *options;
 929        int ret;
 930};
 931
 932static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
 933{
 934        struct fsck_gitmodules_data *data = vdata;
 935        const char *subsection, *key;
 936        int subsection_len;
 937        char *name;
 938
 939        if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
 940            !subsection)
 941                return 0;
 942
 943        name = xmemdupz(subsection, subsection_len);
 944        if (check_submodule_name(name) < 0)
 945                data->ret |= report(data->options, data->obj,
 946                                    FSCK_MSG_GITMODULES_NAME,
 947                                    "disallowed submodule name: %s",
 948                                    name);
 949        if (!strcmp(key, "url") && value &&
 950            looks_like_command_line_option(value))
 951                data->ret |= report(data->options, data->obj,
 952                                    FSCK_MSG_GITMODULES_URL,
 953                                    "disallowed submodule url: %s",
 954                                    value);
 955        free(name);
 956
 957        return 0;
 958}
 959
 960static int fsck_blob(struct blob *blob, const char *buf,
 961                     unsigned long size, struct fsck_options *options)
 962{
 963        struct fsck_gitmodules_data data;
 964
 965        if (!oidset_contains(&gitmodules_found, &blob->object.oid))
 966                return 0;
 967        oidset_insert(&gitmodules_done, &blob->object.oid);
 968
 969        if (!buf) {
 970                /*
 971                 * A missing buffer here is a sign that the caller found the
 972                 * blob too gigantic to load into memory. Let's just consider
 973                 * that an error.
 974                 */
 975                return report(options, &blob->object,
 976                              FSCK_MSG_GITMODULES_PARSE,
 977                              ".gitmodules too large to parse");
 978        }
 979
 980        data.obj = &blob->object;
 981        data.options = options;
 982        data.ret = 0;
 983        if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
 984                                ".gitmodules", buf, size, &data))
 985                data.ret |= report(options, &blob->object,
 986                                   FSCK_MSG_GITMODULES_PARSE,
 987                                   "could not parse gitmodules blob");
 988
 989        return data.ret;
 990}
 991
 992int fsck_object(struct object *obj, void *data, unsigned long size,
 993        struct fsck_options *options)
 994{
 995        if (!obj)
 996                return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
 997
 998        if (obj->type == OBJ_BLOB)
 999                return fsck_blob((struct blob *)obj, data, size, options);
1000        if (obj->type == OBJ_TREE)
1001                return fsck_tree((struct tree *) obj, options);
1002        if (obj->type == OBJ_COMMIT)
1003                return fsck_commit((struct commit *) obj, (const char *) data,
1004                        size, options);
1005        if (obj->type == OBJ_TAG)
1006                return fsck_tag((struct tag *) obj, (const char *) data,
1007                        size, options);
1008
1009        return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
1010                          obj->type);
1011}
1012
1013int fsck_error_function(struct fsck_options *o,
1014        struct object *obj, int msg_type, const char *message)
1015{
1016        if (msg_type == FSCK_WARN) {
1017                warning("object %s: %s", describe_object(o, obj), message);
1018                return 0;
1019        }
1020        error("object %s: %s", describe_object(o, obj), message);
1021        return 1;
1022}
1023
1024int fsck_finish(struct fsck_options *options)
1025{
1026        int ret = 0;
1027        struct oidset_iter iter;
1028        const struct object_id *oid;
1029
1030        oidset_iter_init(&gitmodules_found, &iter);
1031        while ((oid = oidset_iter_next(&iter))) {
1032                struct blob *blob;
1033                enum object_type type;
1034                unsigned long size;
1035                char *buf;
1036
1037                if (oidset_contains(&gitmodules_done, oid))
1038                        continue;
1039
1040                blob = lookup_blob(oid);
1041                if (!blob) {
1042                        ret |= report(options, &blob->object,
1043                                      FSCK_MSG_GITMODULES_BLOB,
1044                                      "non-blob found at .gitmodules");
1045                        continue;
1046                }
1047
1048                buf = read_sha1_file(oid->hash, &type, &size);
1049                if (!buf) {
1050                        if (is_promisor_object(&blob->object.oid))
1051                                continue;
1052                        ret |= report(options, &blob->object,
1053                                      FSCK_MSG_GITMODULES_MISSING,
1054                                      "unable to read .gitmodules blob");
1055                        continue;
1056                }
1057
1058                if (type == OBJ_BLOB)
1059                        ret |= fsck_blob(blob, buf, size, options);
1060                else
1061                        ret |= report(options, &blob->object,
1062                                      FSCK_MSG_GITMODULES_BLOB,
1063                                      "non-blob found at .gitmodules");
1064                free(buf);
1065        }
1066
1067
1068        oidset_clear(&gitmodules_found);
1069        oidset_clear(&gitmodules_done);
1070        return ret;
1071}