@matheusbsbs / matheus-bagliano / commits / 436e7a74c6
Fork 0

Don't allow fast-import tree delta chains to exceed maximum depth

Brian Downing noticed fast-import can produce tree depths of up to 6,035 objects and even deeper. Long delta chains can create very small packfiles but cause problems during repacking as git needs to unpack each tree to count the reachable blobs. What's happening here is the active branch cache isn't big enough. We're swapping out the branch and thus recycling the tree information (struct tree_content) back into the free pool. When we later reload the tree we set the delta_depth to 0 but we kept the tree we just reloaded as a delta base. So if the tree we reloaded was already at the maximum depth we wouldn't know it and make the new tree a delta. Multiply the number of times the branch cache has to swap out the tree times max_depth (10) and you get the maximum delta depth of a tree created by fast-import. In Brian's case above the active branch cache had to swap the branch out 603/604 times during this import to produce a tree with a delta depth of 6035. Acked-by: Brian Downing <bdowning@lavos.net> Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Nov 13, 2007 at 23:48 UTC 436e7a74c638bc9de2e585ce3eb0fd4bd6e06115
1 file changed +13 -7
fast-import.c
+13 -7
@@ -154,13 +154,16 @@ Format of STDIN stream:
154
155 #define PACK_ID_BITS 16
156 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
157 +#define DEPTH_BITS 13
158 +#define MAX_DEPTH ((1<<DEPTH_BITS)-1)
159
160 struct object_entry
161 {
162 struct object_entry *next;
163 uint32_t offset;
162 - unsigned type : TYPE_BITS;
163 - unsigned pack_id : PACK_ID_BITS;
164 + uint32_t type : TYPE_BITS,
165 + pack_id : PACK_ID_BITS,
166 + depth : DEPTH_BITS;
167 unsigned char sha1[20];
168 };
169
@@ -1105,7 +1108,7 @@ static int store_object(
1108 unsigned pos = sizeof(hdr) - 1;
1109
1110 delta_count_by_type[type]++;
1108 - last->depth++;
1111 + e->depth = last->depth + 1;
1112
1113 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
1114 write_or_die(pack_data->pack_fd, hdr, hdrlen);
@@ -1117,8 +1120,7 @@ static int store_object(
1120 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
1121 pack_size += sizeof(hdr) - pos;
1122 } else {
1120 - if (last)
1121 - last->depth = 0;
1123 + e->depth = 0;
1124 hdrlen = encode_header(type, datlen, hdr);
1125 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1126 pack_size += hdrlen;
@@ -1134,6 +1136,7 @@ static int store_object(
1136 free(last->data);
1137 last->data = dat;
1138 last->offset = e->offset;
1139 + last->depth = e->depth;
1140 last->len = datlen;
1141 }
1142 return 0;
@@ -1181,7 +1184,7 @@ static void load_tree(struct tree_entry *root)
1184 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1185 if (myoe->type != OBJ_TREE)
1186 die("Not a tree: %s", sha1_to_hex(sha1));
1184 - t->delta_depth = 0;
1187 + t->delta_depth = myoe->depth;
1188 buf = gfi_unpack_entry(myoe, &size);
1189 } else {
1190 enum object_type type;
@@ -2347,8 +2350,11 @@ int main(int argc, const char **argv)
2350 }
2351 else if (!prefixcmp(a, "--max-pack-size="))
2352 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2350 - else if (!prefixcmp(a, "--depth="))
2353 + else if (!prefixcmp(a, "--depth=")) {
2354 max_depth = strtoul(a + 8, NULL, 0);
2355 + if (max_depth > MAX_DEPTH)
2356 + die("--depth cannot exceed %u", MAX_DEPTH);
2357 + }
2358 else if (!prefixcmp(a, "--active-branches="))
2359 max_active_branches = strtoul(a + 18, NULL, 0);
2360 else if (!prefixcmp(a, "--import-marks="))