commit-graph.c: prevent overflow in `merge_commit_graph()`

When merging two commit graphs, ensure that we don't attempt to merge two graphs which, when combined, have more total commits than the 32-bit unsigned maximum. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jul 12, 2023 at 19:38 UTC d76e0a744d3a8c1713f0e913325cab7da92f01ef
1 file changed +5
commit-graph.c
+5
@@ -2179,6 +2179,11 @@ static void merge_commit_graph(struct write_commit_graph_context *ctx,
2179 uint32_t i;
2180 uint32_t offset = g->num_commits_in_base;
2181
2182 + if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2183 + die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2184 + oid_to_hex(&g->oid),
2185 + (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2186 +
2187 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2188
2189 for (i = 0; i < g->num_commits; i++) {