Skip to content

Commit 32e357b

Browse files
committed
Merge branch 'ps/attr-limits-with-fsck' into maint-2.32
2 parents 8a755ed + 27ab478 commit 32e357b

File tree

3 files changed

+130
-43
lines changed

3 files changed

+130
-43
lines changed

fsck.c

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "object-store.h"
33
#include "repository.h"
44
#include "object.h"
5+
#include "attr.h"
56
#include "blob.h"
67
#include "tree.h"
78
#include "tree-walk.h"
@@ -614,17 +615,22 @@ static int fsck_tree(const struct object_id *tree_oid,
614615
".gitmodules is a symbolic link");
615616
}
616617

618+
if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
619+
if (!S_ISLNK(mode))
620+
oidset_insert(&options->gitattributes_found,
621+
entry_oid);
622+
else
623+
retval += report(options, tree_oid, OBJ_TREE,
624+
FSCK_MSG_GITATTRIBUTES_SYMLINK,
625+
".gitattributes is a symlink");
626+
}
627+
617628
if (S_ISLNK(mode)) {
618629
if (is_hfs_dotgitignore(name) ||
619630
is_ntfs_dotgitignore(name))
620631
retval += report(options, tree_oid, OBJ_TREE,
621632
FSCK_MSG_GITIGNORE_SYMLINK,
622633
".gitignore is a symlink");
623-
if (is_hfs_dotgitattributes(name) ||
624-
is_ntfs_dotgitattributes(name))
625-
retval += report(options, tree_oid, OBJ_TREE,
626-
FSCK_MSG_GITATTRIBUTES_SYMLINK,
627-
".gitattributes is a symlink");
628634
if (is_hfs_dotmailmap(name) ||
629635
is_ntfs_dotmailmap(name))
630636
retval += report(options, tree_oid, OBJ_TREE,
@@ -1170,38 +1176,70 @@ static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
11701176
static int fsck_blob(const struct object_id *oid, const char *buf,
11711177
unsigned long size, struct fsck_options *options)
11721178
{
1173-
struct fsck_gitmodules_data data;
1174-
struct config_options config_opts = { 0 };
1175-
1176-
if (!oidset_contains(&options->gitmodules_found, oid))
1177-
return 0;
1178-
oidset_insert(&options->gitmodules_done, oid);
1179+
int ret = 0;
11791180

11801181
if (object_on_skiplist(options, oid))
11811182
return 0;
11821183

1183-
if (!buf) {
1184-
/*
1185-
* A missing buffer here is a sign that the caller found the
1186-
* blob too gigantic to load into memory. Let's just consider
1187-
* that an error.
1188-
*/
1189-
return report(options, oid, OBJ_BLOB,
1190-
FSCK_MSG_GITMODULES_LARGE,
1191-
".gitmodules too large to parse");
1184+
if (oidset_contains(&options->gitmodules_found, oid)) {
1185+
struct config_options config_opts = { 0 };
1186+
struct fsck_gitmodules_data data;
1187+
1188+
oidset_insert(&options->gitmodules_done, oid);
1189+
1190+
if (!buf) {
1191+
/*
1192+
* A missing buffer here is a sign that the caller found the
1193+
* blob too gigantic to load into memory. Let's just consider
1194+
* that an error.
1195+
*/
1196+
return report(options, oid, OBJ_BLOB,
1197+
FSCK_MSG_GITMODULES_LARGE,
1198+
".gitmodules too large to parse");
1199+
}
1200+
1201+
data.oid = oid;
1202+
data.options = options;
1203+
data.ret = 0;
1204+
config_opts.error_action = CONFIG_ERROR_SILENT;
1205+
if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1206+
".gitmodules", buf, size, &data, &config_opts))
1207+
data.ret |= report(options, oid, OBJ_BLOB,
1208+
FSCK_MSG_GITMODULES_PARSE,
1209+
"could not parse gitmodules blob");
1210+
ret |= data.ret;
11921211
}
11931212

1194-
data.oid = oid;
1195-
data.options = options;
1196-
data.ret = 0;
1197-
config_opts.error_action = CONFIG_ERROR_SILENT;
1198-
if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1199-
".gitmodules", buf, size, &data, &config_opts))
1200-
data.ret |= report(options, oid, OBJ_BLOB,
1201-
FSCK_MSG_GITMODULES_PARSE,
1202-
"could not parse gitmodules blob");
1203-
1204-
return data.ret;
1213+
if (oidset_contains(&options->gitattributes_found, oid)) {
1214+
const char *ptr;
1215+
1216+
oidset_insert(&options->gitattributes_done, oid);
1217+
1218+
if (!buf || size > ATTR_MAX_FILE_SIZE) {
1219+
/*
1220+
* A missing buffer here is a sign that the caller found the
1221+
* blob too gigantic to load into memory. Let's just consider
1222+
* that an error.
1223+
*/
1224+
return report(options, oid, OBJ_BLOB,
1225+
FSCK_MSG_GITATTRIBUTES_LARGE,
1226+
".gitattributes too large to parse");
1227+
}
1228+
1229+
for (ptr = buf; *ptr; ) {
1230+
const char *eol = strchrnul(ptr, '\n');
1231+
if (eol - ptr >= ATTR_MAX_LINE_LENGTH) {
1232+
ret |= report(options, oid, OBJ_BLOB,
1233+
FSCK_MSG_GITATTRIBUTES_LINE_LENGTH,
1234+
".gitattributes has too long lines to parse");
1235+
break;
1236+
}
1237+
1238+
ptr = *eol ? eol + 1 : eol;
1239+
}
1240+
}
1241+
1242+
return ret;
12051243
}
12061244

12071245
int fsck_object(struct object *obj, void *data, unsigned long size,
@@ -1240,45 +1278,58 @@ int fsck_error_function(struct fsck_options *o,
12401278
return 1;
12411279
}
12421280

1243-
int fsck_finish(struct fsck_options *options)
1281+
static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
1282+
enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
1283+
struct fsck_options *options, const char *blob_type)
12441284
{
12451285
int ret = 0;
12461286
struct oidset_iter iter;
12471287
const struct object_id *oid;
12481288

1249-
oidset_iter_init(&options->gitmodules_found, &iter);
1289+
oidset_iter_init(blobs_found, &iter);
12501290
while ((oid = oidset_iter_next(&iter))) {
12511291
enum object_type type;
12521292
unsigned long size;
12531293
char *buf;
12541294

1255-
if (oidset_contains(&options->gitmodules_done, oid))
1295+
if (oidset_contains(blobs_done, oid))
12561296
continue;
12571297

12581298
buf = read_object_file(oid, &type, &size);
12591299
if (!buf) {
12601300
if (is_promisor_object(oid))
12611301
continue;
12621302
ret |= report(options,
1263-
oid, OBJ_BLOB,
1264-
FSCK_MSG_GITMODULES_MISSING,
1265-
"unable to read .gitmodules blob");
1303+
oid, OBJ_BLOB, msg_missing,
1304+
"unable to read %s blob", blob_type);
12661305
continue;
12671306
}
12681307

12691308
if (type == OBJ_BLOB)
12701309
ret |= fsck_blob(oid, buf, size, options);
12711310
else
1272-
ret |= report(options,
1273-
oid, type,
1274-
FSCK_MSG_GITMODULES_BLOB,
1275-
"non-blob found at .gitmodules");
1311+
ret |= report(options, oid, type, msg_type,
1312+
"non-blob found at %s", blob_type);
12761313
free(buf);
12771314
}
12781315

1316+
oidset_clear(blobs_found);
1317+
oidset_clear(blobs_done);
1318+
1319+
return ret;
1320+
}
1321+
1322+
int fsck_finish(struct fsck_options *options)
1323+
{
1324+
int ret = 0;
1325+
1326+
ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
1327+
FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
1328+
options, ".gitmodules");
1329+
ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
1330+
FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
1331+
options, ".gitattributes");
12791332

1280-
oidset_clear(&options->gitmodules_found);
1281-
oidset_clear(&options->gitmodules_done);
12821333
return ret;
12831334
}
12841335

fsck.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ enum fsck_msg_type {
5555
FUNC(GITMODULES_URL, ERROR) \
5656
FUNC(GITMODULES_PATH, ERROR) \
5757
FUNC(GITMODULES_UPDATE, ERROR) \
58+
FUNC(GITATTRIBUTES_MISSING, ERROR) \
59+
FUNC(GITATTRIBUTES_LARGE, ERROR) \
60+
FUNC(GITATTRIBUTES_LINE_LENGTH, ERROR) \
61+
FUNC(GITATTRIBUTES_BLOB, ERROR) \
5862
/* warnings */ \
5963
FUNC(BAD_FILEMODE, WARN) \
6064
FUNC(EMPTY_NAME, WARN) \
@@ -129,25 +133,33 @@ struct fsck_options {
129133
struct oidset skiplist;
130134
struct oidset gitmodules_found;
131135
struct oidset gitmodules_done;
136+
struct oidset gitattributes_found;
137+
struct oidset gitattributes_done;
132138
kh_oid_map_t *object_names;
133139
};
134140

135141
#define FSCK_OPTIONS_DEFAULT { \
136142
.skiplist = OIDSET_INIT, \
137143
.gitmodules_found = OIDSET_INIT, \
138144
.gitmodules_done = OIDSET_INIT, \
145+
.gitattributes_found = OIDSET_INIT, \
146+
.gitattributes_done = OIDSET_INIT, \
139147
.error_func = fsck_error_function \
140148
}
141149
#define FSCK_OPTIONS_STRICT { \
142150
.strict = 1, \
143151
.gitmodules_found = OIDSET_INIT, \
144152
.gitmodules_done = OIDSET_INIT, \
153+
.gitattributes_found = OIDSET_INIT, \
154+
.gitattributes_done = OIDSET_INIT, \
145155
.error_func = fsck_error_function, \
146156
}
147157
#define FSCK_OPTIONS_MISSING_GITMODULES { \
148158
.strict = 1, \
149159
.gitmodules_found = OIDSET_INIT, \
150160
.gitmodules_done = OIDSET_INIT, \
161+
.gitattributes_found = OIDSET_INIT, \
162+
.gitattributes_done = OIDSET_INIT, \
151163
.error_func = fsck_error_cb_print_missing_gitmodules, \
152164
}
153165

t/t1450-fsck.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,28 @@ test_expect_success 'detect corrupt index file in fsck' '
865865
test_i18ngrep "bad index file" errors
866866
'
867867

868+
test_expect_success 'fsck error on gitattributes with excessive line lengths' '
869+
blob=$(printf "pattern %02048d" 1 | git hash-object -w --stdin) &&
870+
test_when_finished "remove_object $blob" &&
871+
tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
872+
test_when_finished "remove_object $tree" &&
873+
cat >expected <<-EOF &&
874+
error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
875+
EOF
876+
test_must_fail git fsck --no-dangling >actual 2>&1 &&
877+
test_cmp expected actual
878+
'
879+
880+
test_expect_success 'fsck error on gitattributes with excessive size' '
881+
blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
882+
test_when_finished "remove_object $blob" &&
883+
tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
884+
test_when_finished "remove_object $tree" &&
885+
cat >expected <<-EOF &&
886+
error in blob $blob: gitattributesLarge: .gitattributes too large to parse
887+
EOF
888+
test_must_fail git fsck --no-dangling >actual 2>&1 &&
889+
test_cmp expected actual
890+
'
891+
868892
test_done

0 commit comments

Comments
 (0)