Skip to content

Commit 8bd40ed

Browse files
peffgitster
authored andcommitted
commit-graph: abort as soon as we see a bogus chunk
The code to read commit-graph files tries to read all of the required chunks, but doesn't abort if we can't find one (or if it's corrupted). It's only at the end of reading the file that we then do some sanity checks for NULL entries. But it's preferable to detect the errors and bail immediately, for a few reasons: 1. It's less error-prone. It's easy in the reader functions to flag an error but still end up setting some struct fields (an error I in fact made while working on this patch series). 2. It's safer. Since verifying some chunks depends on the values of other chunks, we may be depending on not-yet-verified data. I don't know offhand of any case where this can cause problems, but it's one less subtle thing to worry about in the reader code. 3. It prevents the user from seeing nonsense errors. If we're missing an OIDL chunk, then g->num_commits will be zero. And so we may complain that the size of our CDAT chunk (which should have a fixed-size record for each commit) is wrong unless it's also zero. But that's misleading; the problem is the missing OIDL chunk; the CDAT one might be fine! So let's just check the return value from read_chunk(). This is exactly how the midx chunk-reading code does it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 93d2924 commit 8bd40ed

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

commit-graph.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,6 @@ static int verify_commit_graph_lite(struct commit_graph *g)
291291
* over g->num_commits, or runs a checksum on the commit-graph
292292
* itself.
293293
*/
294-
if (!g->chunk_oid_fanout) {
295-
error(_("commit-graph required OID fanout chunk missing or corrupted"));
296-
return 1;
297-
}
298-
if (!g->chunk_oid_lookup) {
299-
error(_("commit-graph required OID lookup chunk missing or corrupted"));
300-
return 1;
301-
}
302-
if (!g->chunk_commit_data) {
303-
error(_("commit-graph required commit data chunk missing or corrupted"));
304-
return 1;
305-
}
306-
307294
for (i = 0; i < 255; i++) {
308295
uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
309296
uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
@@ -462,9 +449,19 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
462449
GRAPH_HEADER_SIZE, graph->num_chunks, 1))
463450
goto free_and_return;
464451

465-
read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph);
466-
read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
467-
read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph);
452+
if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
453+
error(_("commit-graph required OID fanout chunk missing or corrupted"));
454+
goto free_and_return;
455+
}
456+
if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
457+
error(_("commit-graph required OID lookup chunk missing or corrupted"));
458+
goto free_and_return;
459+
}
460+
if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
461+
error(_("commit-graph required commit data chunk missing or corrupted"));
462+
goto free_and_return;
463+
}
464+
468465
pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
469466
&graph->chunk_extra_edges_size);
470467
pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,

0 commit comments

Comments
 (0)