--- builtin/index-pack.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 473514a..dcb6409 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -334,6 +334,14 @@ static const unsigned char *read_sha1ref(void) return sha1_table + index * 20; } +static const unsigned char *read_sha1table_ref(void) +{ + const unsigned char *sha1 = read_sha1ref(); + if (sha1 < sha1_table || sha1 >= sha1_table + nr_objects * 20) + check_against_sha1table(sha1); + return sha1; +} + static const unsigned char *read_dictref(struct packv4_dict *dict) { unsigned int index = read_varint(); @@ -561,21 +569,105 @@ static void *unpack_commit_v4(unsigned int offset, unsigned long size, } /* + * v4 trees are actually kind of deltas and we don't do delta in the + * first pass. This function only walks through a tree object to find + * the end offset, register object dependencies and performs limited + * validation. For v4 trees that have no dependencies, we do + * uncompress and calculate their SHA-1. + */ +static void *unpack_tree_v4(struct object_entry *obj, + unsigned int offset, unsigned long size, + unsigned char *sha1) +{ + unsigned int nr = read_varint(); + const unsigned char *last_base = NULL; + struct strbuf sb = STRBUF_INIT; + while (nr) { + unsigned int copy_start_or_path = read_varint(); + if (copy_start_or_path & 1) { /* copy_start */ + unsigned int copy_count = read_varint(); + if (copy_count & 1) { /* first delta */ + last_base = read_sha1table_ref(); + } else if (!last_base) + bad_object(offset, + _("missing delta base unpack_tree_v4")); + copy_count >>= 1; + if (!copy_count || copy_count > nr) + bad_object(offset, + _("bad copy count index in unpack_tree_v4")); + nr -= copy_count; + } else { /* path */ + unsigned int path_idx = copy_start_or_path >> 1; + const unsigned char *entry_sha1; + + if (path_idx >= path_dict->nb_entries) + bad_object(offset, + _("bad path index in unpack_tree_v4")); + entry_sha1 = read_sha1ref(); + nr--; + + /* + * Attempt to rebuild a canonical (base) tree. + * If last_base is set, this tree depends on + * another tree, which we have no access at this + * stage, so reconstruction must be delayed until + * the second pass. + */ + if (!last_base) { + const unsigned char *path; + unsigned mode; + + path = path_dict->data + path_dict->offsets[path_idx]; + mode = (path[0] << 8) | path[1]; + strbuf_addf(&sb, "%o %s%c", mode, path+2, '\0'); + strbuf_add(&sb, entry_sha1, 20); + if (sb.len > size) + bad_object(offset, + _("tree larger than expected")); + } + } + } + + if (last_base) { + strbuf_release(&sb); + return NULL; + } else { + git_SHA_CTX ctx; + char hdr[32]; + int hdrlen; + + if (sb.len != size) + bad_object(offset, _("tree size mismatch")); + + hdrlen = sprintf(hdr, "tree %lu", size) + 1; + git_SHA1_Init(&ctx); + git_SHA1_Update(&ctx, hdr, hdrlen); + git_SHA1_Update(&ctx, sb.buf, size); + git_SHA1_Final(sha1, &ctx); + return strbuf_detach(&sb, NULL); + } +} + +/* * Unpack an entry data in the streamed pack, calculate the object * SHA-1 if it's not a large blob. Otherwise just try to inflate the * object to /dev/null to determine the end of the entry in the pack. */ -static void *unpack_entry_data(unsigned long offset, unsigned long size, - enum object_type type, unsigned char *sha1) +static void *unpack_entry_data(struct object_entry *obj, unsigned char *sha1) { static char fixed_buf[8192]; void *buf; git_SHA_CTX c; char hdr[32]; int hdrlen; + unsigned long offset = obj->idx.offset; + unsigned long size = obj->size; + enum object_type type = obj->type; if (type == OBJ_PV4_COMMIT) return unpack_commit_v4(offset, size, sha1); + if (type == OBJ_PV4_TREE) + return unpack_tree_v4(obj, offset, size, sha1); if (!is_delta_type(type)) { hdrlen = sprintf(hdr, "%s %lu", typename(type), size) + 1; @@ -644,16 +736,19 @@ static void *unpack_raw_entry(struct object_entry *obj, case OBJ_BLOB: case OBJ_TAG: break; - case OBJ_PV4_COMMIT: obj->real_type = OBJ_COMMIT; break; + case OBJ_PV4_TREE: + obj->real_type = OBJ_TREE; + break; + default: bad_object(obj->idx.offset, _("unknown object type %d"), obj->type); } obj->hdr_size = consumed_bytes - obj->idx.offset; - data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, sha1); + data = unpack_entry_data(obj, sha1); obj->idx.crc32 = input_crc32; return data; } @@ -1201,6 +1296,8 @@ static void parse_pack_objects(unsigned char *sha1) nr_deltas++; delta->obj_no = i; delta++; + } else if (!data && obj->type == OBJ_PV4_TREE) { + /* delay sha1_object() until second pass */ } else if (!data) { /* large blobs, check later */ obj->real_type = OBJ_BAD; -- 1.8.2.83.gc99314b -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html