receive-pack, fetch-pack: reject bogus pack that records objects twice

When receive-pack & fetch-pack are run and store the pack obtained over the wire to a local repository, they internally run the index-pack command with the --strict option. Make sure that we reject incoming packfile that records objects twice to avoid spreading such a damage. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 16, 2011 at 22:04 UTC 68be2fea50e7a34c0e5f9fdf6adb9040c8df197f
4 files changed +11 -2
builtin/index-pack.c
+3 -1
@@ -1122,8 +1122,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1122 if (!index_name)
1123 die("--verify with no packfile name given");
1124 read_idx_option(&opts, index_name);
1125 - opts.flags |= WRITE_IDX_VERIFY;
1125 + opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
1126 }
1127 + if (strict)
1128 + opts.flags |= WRITE_IDX_STRICT;
1129
1130 curr_pack = open_pack_file(pack_name);
1131 parse_pack_header();
object.c
+2
@@ -149,6 +149,8 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
149 struct tree *tree = lookup_tree(sha1);
150 if (tree) {
151 obj = &tree->object;
152 + if (!tree->buffer)
153 + tree->object.parsed = 0;
154 if (!tree->object.parsed) {
155 if (parse_tree_buffer(tree, buffer, size))
156 return NULL;
pack-write.c
+4
@@ -129,6 +129,10 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
129 }
130 sha1write(f, obj->sha1, 20);
131 git_SHA1_Update(&ctx, obj->sha1, 20);
132 + if ((opts->flags & WRITE_IDX_STRICT) &&
133 + (i && !hashcmp(list[-2]->sha1, obj->sha1)))
134 + die("The same object %s appears twice in the pack",
135 + sha1_to_hex(obj->sha1));
136 }
137
138 if (index_version >= 2) {
pack.h
+2 -1
@@ -37,7 +37,8 @@ struct pack_header {
37 struct pack_idx_option {
38 unsigned flags;
39 /* flag bits */
40 -#define WRITE_IDX_VERIFY 01
40 +#define WRITE_IDX_VERIFY 01 /* verify only, do not write the idx file */
41 +#define WRITE_IDX_STRICT 02
42
43 uint32_t version;
44 uint32_t off32_limit;