Skip to content

Commit 9bda846

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2bd0365 commit 9bda846

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

commit-graph.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ static void graph_report(const char *fmt, ...)
843843

844844
int verify_commit_graph(struct repository *r, struct commit_graph *g)
845845
{
846+
uint32_t i, cur_fanout_pos = 0;
847+
struct object_id prev_oid, cur_oid;
848+
846849
if (!g) {
847850
graph_report("no commit-graph file loaded");
848851
return 1;
@@ -857,5 +860,38 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
857860
if (!g->chunk_commit_data)
858861
graph_report("commit-graph is missing the Commit Data chunk");
859862

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+
860896
return verify_commit_graph_error;
861897
}

t/t5318-commit-graph.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ test_expect_success 'git commit-graph verify' '
247247
git commit-graph verify >output
248248
'
249249

250+
HASH_LEN=20
250251
GRAPH_BYTE_VERSION=4
251252
GRAPH_BYTE_HASH=5
252253
GRAPH_BYTE_CHUNK_COUNT=6
@@ -258,6 +259,12 @@ GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
258259
1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
259260
GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
260261
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))
261268

262269
# usage: corrupt_graph_and_verify <position> <data> <string>
263270
# Manipulates the commit-graph file at the position
@@ -312,4 +319,19 @@ test_expect_success 'detect missing commit data chunk' '
312319
"missing the Commit Data chunk"
313320
'
314321

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+
315337
test_done

0 commit comments

Comments
 (0)