Skip to content

Commit bdca834

Browse files
szederderrickstolee
authored andcommitted
commit-graph: check chunk sizes after writing
In my experience while experimenting with new commit-graph chunks, early versions of the corresponding new write_commit_graph_my_chunk() functions are, sadly but not surprisingly, often buggy, and write more or less data than they are supposed to, especially if the chunk size is not directly proportional to the number of commits. This then causes all kinds of issues when reading such a bogus commit-graph file, raising the question of whether the writing or the reading part happens to be buggy this time. Let's catch such issues early, already when writing the commit-graph file, and check that each write_graph_chunk_*() function wrote the amount of data that it was expected to, and what has been encoded in the Chunk Lookup table. Now that all commit-graph chunks are written in a loop we can do this check in a single place for all chunks, and any chunks added in the future will get checked as well. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 3d24b98 commit bdca834

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

commit-graph.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,12 +1659,21 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
16591659
num_chunks * ctx->commits.nr);
16601660
}
16611661

1662+
chunk_offset = f->total + f->offset;
16621663
for (i = 0; i < num_chunks; i++) {
1664+
uint64_t end_offset;
1665+
16631666
if (chunks[i].write_fn(f, ctx)) {
16641667
error(_("failed writing chunk with id %"PRIx32""),
16651668
chunks[i].id);
16661669
return -1;
16671670
}
1671+
1672+
end_offset = f->total + f->offset;
1673+
if (end_offset - chunk_offset != chunks[i].size)
1674+
BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead",
1675+
chunks[i].size, chunks[i].id, end_offset - chunk_offset);
1676+
chunk_offset = end_offset;
16681677
}
16691678

16701679
stop_progress(&ctx->progress);

0 commit comments

Comments
 (0)