Skip to content

Commit 5a8c71d

Browse files
committed
Merge branch 'jk/submodule-name-verify-fsck' into maint
* jk/submodule-name-verify-fsck: fsck: complain when .gitignore and .gitattributes are symlinks fsck: complain when .gitmodules is a symlink index-pack: check .gitmodules files with --strict unpack-objects: call fsck_finish() after fscking objects fsck: call fsck_finish() after fscking objects fsck: check .gitmodules content fsck: handle promisor objects in .gitmodules check fsck: detect gitmodules files fsck: actually fsck blob data fsck: simplify ".git" check index-pack: make fsck error message more specific
2 parents 47aa802 + 2baa638 commit 5a8c71d

File tree

8 files changed

+286
-30
lines changed

8 files changed

+286
-30
lines changed

builtin/fsck.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void check_connectivity(void)
337337
}
338338
}
339339

340-
static int fsck_obj(struct object *obj)
340+
static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
341341
{
342342
int err;
343343

@@ -351,7 +351,7 @@ static int fsck_obj(struct object *obj)
351351

352352
if (fsck_walk(obj, NULL, &fsck_obj_options))
353353
objerror(obj, "broken links");
354-
err = fsck_object(obj, NULL, 0, &fsck_obj_options);
354+
err = fsck_object(obj, buffer, size, &fsck_obj_options);
355355
if (err)
356356
goto out;
357357

@@ -396,7 +396,7 @@ static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
396396
}
397397
obj->flags &= ~(REACHABLE | SEEN);
398398
obj->flags |= HAS_OBJ;
399-
return fsck_obj(obj);
399+
return fsck_obj(obj, buffer, size);
400400
}
401401

402402
static int default_refs;
@@ -504,44 +504,42 @@ static void get_default_heads(void)
504504
}
505505
}
506506

507-
static struct object *parse_loose_object(const struct object_id *oid,
508-
const char *path)
507+
static int fsck_loose(const struct object_id *oid, const char *path, void *data)
509508
{
510509
struct object *obj;
511-
void *contents;
512510
enum object_type type;
513511
unsigned long size;
512+
void *contents;
514513
int eaten;
515514

516-
if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
517-
return NULL;
515+
if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0) {
516+
errors_found |= ERROR_OBJECT;
517+
error("%s: object corrupt or missing: %s",
518+
oid_to_hex(oid), path);
519+
return 0; /* keep checking other objects */
520+
}
518521

519522
if (!contents && type != OBJ_BLOB)
520-
die("BUG: read_loose_object streamed a non-blob");
523+
BUG("read_loose_object streamed a non-blob");
521524

522525
obj = parse_object_buffer(oid, type, size, contents, &eaten);
523-
524-
if (!eaten)
525-
free(contents);
526-
return obj;
527-
}
528-
529-
static int fsck_loose(const struct object_id *oid, const char *path, void *data)
530-
{
531-
struct object *obj = parse_loose_object(oid, path);
532-
533526
if (!obj) {
534527
errors_found |= ERROR_OBJECT;
535-
error("%s: object corrupt or missing: %s",
528+
error("%s: object could not be parsed: %s",
536529
oid_to_hex(oid), path);
530+
if (!eaten)
531+
free(contents);
537532
return 0; /* keep checking other objects */
538533
}
539534

540535
obj->flags &= ~(REACHABLE | SEEN);
541536
obj->flags |= HAS_OBJ;
542-
if (fsck_obj(obj))
537+
if (fsck_obj(obj, contents, size))
543538
errors_found |= ERROR_OBJECT;
544-
return 0;
539+
540+
if (!eaten)
541+
free(contents);
542+
return 0; /* keep checking other objects, even if we saw an error */
545543
}
546544

547545
static int fsck_cruft(const char *basename, const char *path, void *data)
@@ -750,6 +748,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
750748
}
751749
stop_progress(&progress);
752750
}
751+
752+
if (fsck_finish(&fsck_obj_options))
753+
errors_found |= ERROR_OBJECT;
753754
}
754755

755756
for (i = 0; i < argc; i++) {

builtin/index-pack.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
836836
blob->object.flags |= FLAG_CHECKED;
837837
else
838838
die(_("invalid blob object %s"), oid_to_hex(oid));
839+
if (do_fsck_object &&
840+
fsck_object(&blob->object, (void *)data, size, &fsck_options))
841+
die(_("fsck error in packed object"));
839842
} else {
840843
struct object *obj;
841844
int eaten;
@@ -853,7 +856,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
853856
die(_("invalid %s"), type_name(type));
854857
if (do_fsck_object &&
855858
fsck_object(obj, buf, size, &fsck_options))
856-
die(_("Error in object"));
859+
die(_("fsck error in packed object"));
857860
if (strict && fsck_walk(obj, NULL, &fsck_options))
858861
die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
859862

@@ -1477,6 +1480,9 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14771480
} else
14781481
chmod(final_index_name, 0444);
14791482

1483+
if (do_fsck_object)
1484+
add_packed_git(final_index_name, strlen(final_index_name), 0);
1485+
14801486
if (!from_stdin) {
14811487
printf("%s\n", sha1_to_hex(hash));
14821488
} else {
@@ -1818,6 +1824,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
18181824
pack_hash);
18191825
else
18201826
close(input_fd);
1827+
1828+
if (do_fsck_object && fsck_finish(&fsck_options))
1829+
die(_("fsck error in pack objects"));
1830+
18211831
free(objects);
18221832
strbuf_release(&index_name_buf);
18231833
if (pack_name == NULL)

builtin/unpack-objects.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int check_object(struct object *obj, int type, void *data, struct fsck_op
210210
if (!obj_buf)
211211
die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
212212
if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
213-
die("Error in object");
213+
die("fsck error in packed object");
214214
fsck_options.walk = check_object;
215215
if (fsck_walk(obj, NULL, &fsck_options))
216216
die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
@@ -572,8 +572,11 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
572572
unpack_all();
573573
the_hash_algo->update_fn(&ctx, buffer, offset);
574574
the_hash_algo->final_fn(oid.hash, &ctx);
575-
if (strict)
575+
if (strict) {
576576
write_rest();
577+
if (fsck_finish(&fsck_options))
578+
die(_("fsck error in pack objects"));
579+
}
577580
if (hashcmp(fill(the_hash_algo->rawsz), oid.hash))
578581
die("final sha1 did not match");
579582
use(the_hash_algo->rawsz);

fsck.c

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
#include "utf8.h"
1111
#include "sha1-array.h"
1212
#include "decorate.h"
13+
#include "oidset.h"
14+
#include "packfile.h"
15+
#include "submodule-config.h"
16+
#include "config.h"
17+
18+
static struct oidset gitmodules_found = OIDSET_INIT;
19+
static struct oidset gitmodules_done = OIDSET_INIT;
1320

1421
#define FSCK_FATAL -1
1522
#define FSCK_INFO -2
@@ -44,13 +51,21 @@
4451
FUNC(MISSING_TAG_ENTRY, ERROR) \
4552
FUNC(MISSING_TAG_OBJECT, ERROR) \
4653
FUNC(MISSING_TREE, ERROR) \
54+
FUNC(MISSING_TREE_OBJECT, ERROR) \
4755
FUNC(MISSING_TYPE, ERROR) \
4856
FUNC(MISSING_TYPE_ENTRY, ERROR) \
4957
FUNC(MULTIPLE_AUTHORS, ERROR) \
5058
FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
5159
FUNC(TREE_NOT_SORTED, ERROR) \
5260
FUNC(UNKNOWN_TYPE, ERROR) \
5361
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(GITIGNORE_SYMLINK, ERROR) \
68+
FUNC(GITATTRIBUTES_SYMLINK, ERROR) \
5469
/* warnings */ \
5570
FUNC(BAD_FILEMODE, WARN) \
5671
FUNC(EMPTY_NAME, WARN) \
@@ -561,10 +576,31 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
561576
has_empty_name |= !*name;
562577
has_dot |= !strcmp(name, ".");
563578
has_dotdot |= !strcmp(name, "..");
564-
has_dotgit |= (!strcmp(name, ".git") ||
565-
is_hfs_dotgit(name) ||
566-
is_ntfs_dotgit(name));
579+
has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
567580
has_zero_pad |= *(char *)desc.buffer == '0';
581+
582+
if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
583+
if (!S_ISLNK(mode))
584+
oidset_insert(&gitmodules_found, oid);
585+
else
586+
retval += report(options, &item->object,
587+
FSCK_MSG_GITMODULES_SYMLINK,
588+
".gitmodules is a symbolic link");
589+
}
590+
591+
if (S_ISLNK(mode)) {
592+
if (is_hfs_dotgitignore(name) ||
593+
is_ntfs_dotgitignore(name))
594+
retval += report(options, &item->object,
595+
FSCK_MSG_GITIGNORE_SYMLINK,
596+
".gitignore is a symlink");
597+
if (is_hfs_dotgitattributes(name) ||
598+
is_ntfs_dotgitattributes(name))
599+
retval += report(options, &item->object,
600+
FSCK_MSG_GITATTRIBUTES_SYMLINK,
601+
".gitattributes is a symlink");
602+
}
603+
568604
if (update_tree_entry_gently(&desc)) {
569605
retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
570606
break;
@@ -901,14 +937,74 @@ static int fsck_tag(struct tag *tag, const char *data,
901937
return fsck_tag_buffer(tag, data, size, options);
902938
}
903939

940+
struct fsck_gitmodules_data {
941+
struct object *obj;
942+
struct fsck_options *options;
943+
int ret;
944+
};
945+
946+
static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
947+
{
948+
struct fsck_gitmodules_data *data = vdata;
949+
const char *subsection, *key;
950+
int subsection_len;
951+
char *name;
952+
953+
if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
954+
!subsection)
955+
return 0;
956+
957+
name = xmemdupz(subsection, subsection_len);
958+
if (check_submodule_name(name) < 0)
959+
data->ret |= report(data->options, data->obj,
960+
FSCK_MSG_GITMODULES_NAME,
961+
"disallowed submodule name: %s",
962+
name);
963+
free(name);
964+
965+
return 0;
966+
}
967+
968+
static int fsck_blob(struct blob *blob, const char *buf,
969+
unsigned long size, struct fsck_options *options)
970+
{
971+
struct fsck_gitmodules_data data;
972+
973+
if (!oidset_contains(&gitmodules_found, &blob->object.oid))
974+
return 0;
975+
oidset_insert(&gitmodules_done, &blob->object.oid);
976+
977+
if (!buf) {
978+
/*
979+
* A missing buffer here is a sign that the caller found the
980+
* blob too gigantic to load into memory. Let's just consider
981+
* that an error.
982+
*/
983+
return report(options, &blob->object,
984+
FSCK_MSG_GITMODULES_PARSE,
985+
".gitmodules too large to parse");
986+
}
987+
988+
data.obj = &blob->object;
989+
data.options = options;
990+
data.ret = 0;
991+
if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
992+
".gitmodules", buf, size, &data))
993+
data.ret |= report(options, &blob->object,
994+
FSCK_MSG_GITMODULES_PARSE,
995+
"could not parse gitmodules blob");
996+
997+
return data.ret;
998+
}
999+
9041000
int fsck_object(struct object *obj, void *data, unsigned long size,
9051001
struct fsck_options *options)
9061002
{
9071003
if (!obj)
9081004
return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
9091005

9101006
if (obj->type == OBJ_BLOB)
911-
return 0;
1007+
return fsck_blob((struct blob *)obj, data, size, options);
9121008
if (obj->type == OBJ_TREE)
9131009
return fsck_tree((struct tree *) obj, options);
9141010
if (obj->type == OBJ_COMMIT)
@@ -932,3 +1028,52 @@ int fsck_error_function(struct fsck_options *o,
9321028
error("object %s: %s", describe_object(o, obj), message);
9331029
return 1;
9341030
}
1031+
1032+
int fsck_finish(struct fsck_options *options)
1033+
{
1034+
int ret = 0;
1035+
struct oidset_iter iter;
1036+
const struct object_id *oid;
1037+
1038+
oidset_iter_init(&gitmodules_found, &iter);
1039+
while ((oid = oidset_iter_next(&iter))) {
1040+
struct blob *blob;
1041+
enum object_type type;
1042+
unsigned long size;
1043+
char *buf;
1044+
1045+
if (oidset_contains(&gitmodules_done, oid))
1046+
continue;
1047+
1048+
blob = lookup_blob(oid);
1049+
if (!blob) {
1050+
ret |= report(options, &blob->object,
1051+
FSCK_MSG_GITMODULES_BLOB,
1052+
"non-blob found at .gitmodules");
1053+
continue;
1054+
}
1055+
1056+
buf = read_sha1_file(oid->hash, &type, &size);
1057+
if (!buf) {
1058+
if (is_promisor_object(&blob->object.oid))
1059+
continue;
1060+
ret |= report(options, &blob->object,
1061+
FSCK_MSG_GITMODULES_MISSING,
1062+
"unable to read .gitmodules blob");
1063+
continue;
1064+
}
1065+
1066+
if (type == OBJ_BLOB)
1067+
ret |= fsck_blob(blob, buf, size, options);
1068+
else
1069+
ret |= report(options, &blob->object,
1070+
FSCK_MSG_GITMODULES_BLOB,
1071+
"non-blob found at .gitmodules");
1072+
free(buf);
1073+
}
1074+
1075+
1076+
oidset_clear(&gitmodules_found);
1077+
oidset_clear(&gitmodules_done);
1078+
return ret;
1079+
}

fsck.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ int fsck_walk(struct object *obj, void *data, struct fsck_options *options);
5353
int fsck_object(struct object *obj, void *data, unsigned long size,
5454
struct fsck_options *options);
5555

56+
/*
57+
* Some fsck checks are context-dependent, and may end up queued; run this
58+
* after completing all fsck_object() calls in order to resolve any remaining
59+
* checks.
60+
*/
61+
int fsck_finish(struct fsck_options *options);
62+
5663
#endif

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ int read_loose_object(const char *path,
22092209
goto out;
22102210
}
22112211

2212-
if (*type == OBJ_BLOB) {
2212+
if (*type == OBJ_BLOB && *size > big_file_threshold) {
22132213
if (check_stream_sha1(&stream, hdr, *size, path, expected_sha1) < 0)
22142214
goto out;
22152215
} else {

0 commit comments

Comments
 (0)