index-pack --verify: read anomalous offsets from v2 idx file
A pack v2 .idx file usually records offset using 64-bit representation only when the offset does not fit within 31-bit, but you can handcraft your .idx file to record smaller offset using 64-bit, storing all zero in the upper 4-byte. By inspecting the original idx file when running index-pack --verify, encode such low offsets that do not need to be in 64-bit but are encoded using 64-bit just like the original idx file so that we can still validate the pack/idx pair by comparing the idx file recomputed with the original. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Feb 25, 2011 at 16:55 UTC
3c9fc074c220d5d1d2173c84cc6ae57d750e2a2c
4 files changed
+74
-2
builtin/index-pack.c
+48
@@ -891,6 +891,51 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
891
return git_default_config(k, v, cb);
892
}
893
894
+static int cmp_uint32(const void *a_, const void *b_)
895
+{
896
+ uint32_t a = *((uint32_t *)a_);
897
+ uint32_t b = *((uint32_t *)b_);
898
+
899
+ return (a < b) ? -1 : (a != b);
900
+}
901
+
902
+static void read_v2_anomalous_offsets(struct packed_git *p,
903
+ struct pack_idx_option *opts)
904
+{
905
+ const uint32_t *idx1, *idx2;
906
+ uint32_t i;
907
+
908
+ /* The address of the 4-byte offset table */
909
+ idx1 = (((const uint32_t *)p->index_data)
910
+ + 2 /* 8-byte header */
911
+ + 256 /* fan out */
912
+ + 5 * p->num_objects /* 20-byte SHA-1 table */
913
+ + p->num_objects /* CRC32 table */
914
+ );
915
+
916
+ /* The address of the 8-byte offset table */
917
+ idx2 = idx1 + p->num_objects;
918
+
919
+ for (i = 0; i < p->num_objects; i++) {
920
+ uint32_t off = ntohl(idx1[i]);
921
+ if (!(off & 0x80000000))
922
+ continue;
923
+ off = off & 0x7fffffff;
924
+ if (idx2[off * 2])
925
+ continue;
926
+ /*
927
+ * The real offset is ntohl(idx2[off * 2]) in high 4
928
+ * octets, and ntohl(idx2[off * 2 + 1]) in low 4
929
+ * octets. But idx2[off * 2] is Zero!!!
930
+ */
931
+ ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
932
+ opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
933
+ }
934
+
935
+ if (1 < opts->anomaly_nr)
936
+ qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32);
937
+}
938
+
939
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
940
{
941
struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
@@ -903,6 +948,9 @@ static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
948
/* Read the attributes from the existing idx file */
949
opts->version = p->index_version;
950
951
+ if (opts->version == 2)
952
+ read_v2_anomalous_offsets(p, opts);
953
+
954
/*
955
* Get rid of the idx file as we do not need it anymore.
956
* NEEDSWORK: extract this bit from free_pack_by_name() in
pack-write.c
+17
-1
@@ -16,9 +16,25 @@ static int sha1_compare(const void *_a, const void *_b)
16
return hashcmp(a->sha1, b->sha1);
17
}
18
19
+static int cmp_uint32(const void *a_, const void *b_)
20
+{
21
+ uint32_t a = *((uint32_t *)a_);
22
+ uint32_t b = *((uint32_t *)b_);
23
+
24
+ return (a < b) ? -1 : (a != b);
25
+}
26
+
27
static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
28
{
21
- return (offset >> 31) || (opts->off32_limit < offset);
29
+ uint32_t ofsval;
30
+
31
+ if ((offset >> 31) || (opts->off32_limit < offset))
32
+ return 1;
33
+ if (!opts->anomaly_nr)
34
+ return 0;
35
+ ofsval = offset;
36
+ return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr,
37
+ sizeof(ofsval), cmp_uint32);
38
}
39
40
/*
pack.h
+8
@@ -41,6 +41,14 @@ struct pack_idx_option {
41
42
uint32_t version;
43
uint32_t off32_limit;
44
+
45
+ /*
46
+ * List of offsets that would fit within off32_limit but
47
+ * need to be written out as 64-bit entity for byte-for-byte
48
+ * verification.
49
+ */
50
+ int anomaly_alloc, anomaly_nr;
51
+ uint32_t *anomaly;
52
};
53
54
extern void reset_pack_idx_option(struct pack_idx_option *);
t/t5302-pack-index.sh
+1
-1
@@ -107,7 +107,7 @@ test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
107
git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
108
'
109
110
-test_expect_failure OFF64_T 'index-pack --verify on 64-bit offset v2' '
110
+test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
111
git index-pack --verify "test-3-${pack3}.pack"
112
'
113