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

In a similar spirit as previous commits, ensure that we don't overflow in a few spots within `fill_commit_graph_info()`: - First, when computing an offset into the commit data chunk, which can occur when the `lex_index` of the item we're looking up exceeds 2^32-1/GRAPH_DATA_WIDTH. - A similar issue when computing the generation date offset for commits with `lex_index` greater than 2^32-1/4. Note that in practice this will never overflow, since the left-hand operand is from calling `sizeof(...)` and is thus already a `size_t`. But wrap that in an `st_mult()` to make it clear that we intend to perform this computation using 64-bit operands. - Finally, a nearly identical issue as above when computing an offset into the `generation_data_overflow` chunk. 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 2740ed1c76df769aa1c6e75020ace72e2cc2e47f
1 file changed +3 -3
commit-graph.c
+3 -3
@@ -790,7 +790,7 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
790 die(_("invalid commit position. commit-graph is likely corrupt"));
791
792 lex_index = pos - g->num_commits_in_base;
793 - commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
793 + commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
794
795 graph_data = commit_graph_data_at(item);
796 graph_data->graph_pos = pos;
@@ -800,14 +800,14 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
800 item->date = (timestamp_t)((date_high << 32) | date_low);
801
802 if (g->read_generation_data) {
803 - offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
803 + offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
804
805 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
806 if (!g->chunk_generation_data_overflow)
807 die(_("commit-graph requires overflow generation data but has none"));
808
809 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
810 - graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
810 + graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos));
811 } else
812 graph_data->generation = item->date + offset;
813 } else