Skip to content

Commit 2ad4f1a

Browse files
szedergitster
authored andcommitted
commit-graph: simplify parse_commit_graph() #1
While we iterate over all entries of the Chunk Lookup table we make sure that we don't attempt to read past the end of the mmap-ed commit-graph file, and check in each iteration that the chunk ID and offset we are about to read is still within the mmap-ed memory region. However, these checks in each iteration are not really necessary, because the number of chunks in the commit-graph file is already known before this loop from the just parsed commit-graph header. So let's check that the commit-graph file is large enough for all entries in the Chunk Lookup table before we start iterating over those entries, and drop those per-iteration checks. While at it, take into account the size of everything that is necessary to have a valid commit-graph file, i.e. the size of the header, the size of the mandatory OID Fanout chunk, and the size of the signature in the trailer as well. Note that this necessitates the change of the error message as well, and, consequently, have to update the 'detect incorrect chunk count' test in 't5318-commit-graph.sh' as well. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa79653 commit 2ad4f1a

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

commit-graph.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
272272
graph->data = graph_map;
273273
graph->data_len = graph_size;
274274

275+
if (graph_size < GRAPH_HEADER_SIZE +
276+
(graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
277+
GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
278+
error(_("commit-graph file is too small to hold %u chunks"),
279+
graph->num_chunks);
280+
free(graph);
281+
return NULL;
282+
}
283+
275284
last_chunk_id = 0;
276285
last_chunk_offset = 8;
277286
chunk_lookup = data + 8;
@@ -280,13 +289,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
280289
uint64_t chunk_offset;
281290
int chunk_repeated = 0;
282291

283-
if (data + graph_size - chunk_lookup <
284-
GRAPH_CHUNKLOOKUP_WIDTH) {
285-
error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
286-
free(graph);
287-
return NULL;
288-
}
289-
290292
chunk_id = get_be32(chunk_lookup + 0);
291293
chunk_offset = get_be64(chunk_lookup + 4);
292294

t/t5318-commit-graph.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ test_expect_success 'detect invalid checksum hash' '
574574

575575
test_expect_success 'detect incorrect chunk count' '
576576
corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
577-
"chunk lookup table entry missing" $GRAPH_CHUNK_LOOKUP_OFFSET
577+
"commit-graph file is too small to hold [0-9]* chunks" \
578+
$GRAPH_CHUNK_LOOKUP_OFFSET
578579
'
579580

580581
test_expect_success 'git fsck (checks commit-graph)' '

0 commit comments

Comments
 (0)