Coverage Report

Created: 2026-11-12 06:03

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