git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Nov 4, 2019, 09:59 UTC
Message-ID
<20191104095923.116086-2-gitter.spiros@gmail.com>
In-Reply-To
<20191104095923.116086-1-gitter.spiros@gmail.com>

Fix the LGTM warning fired by the rule that finds code that could convert the result of an integer multiplication to a larger type. Since the conversion applies after the multiplication, arithmetic overflow may still occur.

Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 commit-graph.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/commit-graph.c b/commit-graph.c
index a0f868522b..0be15f1cd4 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1415,8 +1415,8 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
 	chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
 	chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
-	chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
-	chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
+	chunk_offsets[2] = chunk_offsets[1] + (uint64_t)hashsz * ctx->commits.nr;
+	chunk_offsets[3] = chunk_offsets[2] + ((uint64_t)hashsz + 16) * ctx->commits.nr;
 
 	num_chunks = 3;
 	if (ctx->num_extra_edges) {
@@ -1426,7 +1426,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	}
 	if (ctx->num_commit_graphs_after > 1) {
 		chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
-						hashsz * (ctx->num_commit_graphs_after - 1);
+						(uint64_t)hashsz * (ctx->num_commit_graphs_after - 1);
 		num_chunks++;
 	}
 
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);
 	}
 	write_graph_chunk_fanout(f, ctx);
 	write_graph_chunk_oids(f, hashsz, ctx);
-- 
2.24.0.rc0.467.g566ccdd3e4.dirty
Previous: Elia PintoNext: Junio C Hamano
Message 2 of 15 in “ident.c: fix LGTM warning on the possible abuse of the '=' operator”
  1. 30/32 ident.c: fix LGTM warning on the possible abuse of the '=' operatorElia Pinto, Nov 4, 2019
  2. 31/32 commit-graph.c: fix code that could convert the result of an integer multiplication to a larger typeElia Pinto, Nov 4, 2019
  3. Junio C HamanoNov 6, 2019
  4. Danh DoanNov 7, 2019
  5. Junio C HamanoNov 7, 2019
  6. Danh DoanNov 7, 2019
  7. Johannes SchindelinNov 7, 2019
  8. Johannes SchindelinNov 7, 2019
  9. 32/32 date.c: fix code that may overflow 'int' before it is converted to 'time_t'Elia Pinto, Nov 4, 2019
  10. SZEDER GáborNov 4, 2019
  11. Elia PintoNov 4, 2019
  12. Phillip WoodNov 4, 2019
  13. Elia PintoNov 4, 2019
  14. Junio C HamanoNov 6, 2019
  15. Johannes SchindelinNov 6, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.