commit-graph: verify corrupt OID fanout and lookup

In the commit-graph file, the OID fanout chunk provides an index into the OID lookup. The 'verify' subcommand should find incorrect values in the fanout. Similarly, the 'verify' subcommand should find out-of-order values in the OID lookup. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 27, 2018 at 09:24 UTC 9bda84678950c95e4d81cad60e5210c001fbe119
2 files changed +58
commit-graph.c
+36
@@ -843,6 +843,9 @@ static void graph_report(const char *fmt, ...)
843
844 int verify_commit_graph(struct repository *r, struct commit_graph *g)
845 {
846 + uint32_t i, cur_fanout_pos = 0;
847 + struct object_id prev_oid, cur_oid;
848 +
849 if (!g) {
850 graph_report("no commit-graph file loaded");
851 return 1;
@@ -857,5 +860,38 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
860 if (!g->chunk_commit_data)
861 graph_report("commit-graph is missing the Commit Data chunk");
862
863 + if (verify_commit_graph_error)
864 + return verify_commit_graph_error;
865 +
866 + for (i = 0; i < g->num_commits; i++) {
867 + hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
868 +
869 + if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
870 + graph_report("commit-graph has incorrect OID order: %s then %s",
871 + oid_to_hex(&prev_oid),
872 + oid_to_hex(&cur_oid));
873 +
874 + oidcpy(&prev_oid, &cur_oid);
875 +
876 + while (cur_oid.hash[0] > cur_fanout_pos) {
877 + uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
878 +
879 + if (i != fanout_value)
880 + graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
881 + cur_fanout_pos, fanout_value, i);
882 + cur_fanout_pos++;
883 + }
884 + }
885 +
886 + while (cur_fanout_pos < 256) {
887 + uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
888 +
889 + if (g->num_commits != fanout_value)
890 + graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
891 + cur_fanout_pos, fanout_value, i);
892 +
893 + cur_fanout_pos++;
894 + }
895 +
896 return verify_commit_graph_error;
897 }
t/t5318-commit-graph.sh
+22
@@ -247,6 +247,7 @@ test_expect_success 'git commit-graph verify' '
247 git commit-graph verify >output
248 '
249
250 +HASH_LEN=20
251 GRAPH_BYTE_VERSION=4
252 GRAPH_BYTE_HASH=5
253 GRAPH_BYTE_CHUNK_COUNT=6
@@ -258,6 +259,12 @@ GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
259 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
260 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
261 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
262 +GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
263 + $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
264 +GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
265 +GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
266 +GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
267 +GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
268
269 # usage: corrupt_graph_and_verify <position> <data> <string>
270 # Manipulates the commit-graph file at the position
@@ -312,4 +319,19 @@ test_expect_success 'detect missing commit data chunk' '
319 "missing the Commit Data chunk"
320 '
321
322 +test_expect_success 'detect incorrect fanout' '
323 + corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
324 + "fanout value"
325 +'
326 +
327 +test_expect_success 'detect incorrect fanout final value' '
328 + corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
329 + "fanout value"
330 +'
331 +
332 +test_expect_success 'detect incorrect OID order' '
333 + corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
334 + "incorrect OID order"
335 +'
336 +
337 test_done