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

[PATCH v2 04/10] commit-graph: fix sign comparison warnings

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 6, 2025, 12:00 UTC
Message-ID
<20250806-b4-pks-commit-graph-wo-the-repository-v2-4-911bae638e61@pks.im>
In-Reply-To
<20250806-b4-pks-commit-graph-wo-the-repository-v2-0-911bae638e61@pks.im>
The "commit-graph.c" file has a bunch of sign comparison warnings:
  - There are a bunch of variables that are declared as signed integers
    even though they are used to count entities, like for example
    `num_commit_graphs_before` and `num_commit_graphs_after`.
  - There are several cases where we use signed loop variables to
    iterate through an unsigned entity count.
  - In `write_graph_chunk_base_1()` we count how many chunks we have
    written in total. But while the value represents a positive
    quantity, we still return a signed integer that we then later
    compare with unsigned values.
  - The Bloom settings hash version is being assigned `-1` even though
    it's an unsigned value. This is used to indicate an unspecified
    value and relies on 1's complement.

Fix all of these cases by either using the proper variable type or by adding casts as required.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 54 +++++++++++++++++++++++++++---------------------------
 1 file changed, 27 insertions(+), 27 deletions(-)
diff --git a/commit-graph.c b/commit-graph.c
index ba04fe75db..6d5b5444e7 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1,5 +1,4 @@
 #define USE_THE_REPOSITORY_VARIABLE
-#define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "git-compat-util.h"
 #include "config.h"
@@ -569,7 +568,7 @@ static void validate_mixed_bloom_settings(struct commit_graph *g)
 static int add_graph_to_chain(struct commit_graph *g,
 			      struct commit_graph *chain,
 			      struct object_id *oids,
-			      int n)
+			      size_t n)
 {
 	struct commit_graph *cur_g = chain;
 
@@ -622,7 +621,7 @@ int open_commit_graph_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size < the_hash_algo->hexsz) {
+	if (st->st_size < (ssize_t) the_hash_algo->hexsz) {
 		close(*fd);
 		if (!st->st_size) {
 			/* treat empty files the same as missing */
@@ -643,15 +642,16 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
 	struct commit_graph *graph_chain = NULL;
 	struct strbuf line = STRBUF_INIT;
 	struct object_id *oids;
-	int i = 0, valid = 1, count;
+	int valid = 1;
 	FILE *fp = xfdopen(fd, "r");
+	size_t count;
 
 	count = st->st_size / (the_hash_algo->hexsz + 1);
 	CALLOC_ARRAY(oids, count);
 
 	odb_prepare_alternates(r->objects);
 
-	for (i = 0; i < count; i++) {
+	for (size_t i = 0; i < count; i++) {
 		struct odb_source *source;
 
 		if (strbuf_getline_lf(&line, fp) == EOF)
@@ -1145,12 +1145,12 @@ struct write_commit_graph_context {
 	int num_generation_data_overflows;
 	unsigned long approx_nr_objects;
 	struct progress *progress;
-	int progress_done;
+	uint64_t progress_done;
 	uint64_t progress_cnt;
 
 	char *base_graph_name;
-	int num_commit_graphs_before;
-	int num_commit_graphs_after;
+	uint32_t num_commit_graphs_before;
+	uint32_t num_commit_graphs_after;
 	char **commit_graph_filenames_before;
 	char **commit_graph_filenames_after;
 	char **commit_graph_hash_after;
@@ -1181,7 +1181,7 @@ static int write_graph_chunk_fanout(struct hashfile *f,
 				    void *data)
 {
 	struct write_commit_graph_context *ctx = data;
-	int i, count = 0;
+	size_t i, count = 0;
 	struct commit **list = ctx->commits.list;
 
 	/*
@@ -1209,7 +1209,8 @@ static int write_graph_chunk_oids(struct hashfile *f,
 {
 	struct write_commit_graph_context *ctx = data;
 	struct commit **list = ctx->commits.list;
-	int count;
+	size_t count;
+
 	for (count = 0; count < ctx->commits.nr; count++, list++) {
 		display_progress(ctx->progress, ++ctx->progress_cnt);
 		hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
@@ -1331,9 +1332,9 @@ static int write_graph_chunk_generation_data(struct hashfile *f,
 					     void *data)
 {
 	struct write_commit_graph_context *ctx = data;
-	int i, num_generation_data_overflows = 0;
+	int num_generation_data_overflows = 0;
 
-	for (i = 0; i < ctx->commits.nr; i++) {
+	for (size_t i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.list[i];
 		timestamp_t offset;
 		repo_parse_commit(ctx->r, c);
@@ -1355,8 +1356,8 @@ static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
 						      void *data)
 {
 	struct write_commit_graph_context *ctx = data;
-	int i;
-	for (i = 0; i < ctx->commits.nr; i++) {
+
+	for (size_t i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.list[i];
 		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
 		display_progress(ctx->progress, ++ctx->progress_cnt);
@@ -1526,7 +1527,7 @@ static void add_missing_parents(struct write_commit_graph_context *ctx, struct c
 
 static void close_reachable(struct write_commit_graph_context *ctx)
 {
-	int i;
+	size_t i;
 	struct commit *commit;
 	enum commit_graph_split_flags flags = ctx->opts ?
 		ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
@@ -1620,10 +1621,9 @@ static void compute_reachable_generation_numbers(
 			struct compute_generation_info *info,
 			int generation_version)
 {
-	int i;
 	struct commit_list *list = NULL;
 
-	for (i = 0; i < info->commits->nr; i++) {
+	for (size_t i = 0; i < info->commits->nr; i++) {
 		struct commit *c = info->commits->list[i];
 		timestamp_t gen;
 		repo_parse_commit(info->r, c);
@@ -1714,7 +1714,7 @@ static void set_generation_v2(struct commit *c, timestamp_t t,
 
 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
 {
-	int i;
+	size_t i;
 	struct compute_generation_info info = {
 		.r = ctx->r,
 		.commits = &ctx->commits,
@@ -1793,10 +1793,10 @@ static void trace2_bloom_filter_write_statistics(struct write_commit_graph_conte
 
 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
 {
-	int i;
+	size_t i;
 	struct progress *progress = NULL;
 	struct commit **sorted_commits;
-	int max_new_filters;
+	size_t max_new_filters;
 
 	init_bloom_filters();
 
@@ -1814,7 +1814,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
 		QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
 
 	max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
-		ctx->opts->max_new_filters : ctx->commits.nr;
+		(size_t) ctx->opts->max_new_filters : ctx->commits.nr;
 
 	for (i = 0; i < ctx->commits.nr; i++) {
 		enum bloom_filter_computed computed = 0;
@@ -2017,10 +2017,10 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
 	stop_progress(&ctx->progress);
 }
 
-static int write_graph_chunk_base_1(struct hashfile *f,
-				    struct commit_graph *g)
+static size_t write_graph_chunk_base_1(struct hashfile *f,
+				       struct commit_graph *g)
 {
-	int num = 0;
+	size_t num = 0;
 
 	if (!g)
 		return 0;
@@ -2034,7 +2034,7 @@ static int write_graph_chunk_base(struct hashfile *f,
 				    void *data)
 {
 	struct write_commit_graph_context *ctx = data;
-	int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
+	size_t num = write_graph_chunk_base_1(f, ctx->new_base_graph);
 
 	if (num != ctx->num_commit_graphs_after - 1) {
 		error(_("failed to write correct number of base graph ids"));
@@ -2480,7 +2480,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
 		if (stat(path.buf, &st) < 0)
 			continue;
 
-		if (st.st_mtime > expire_time)
+		if ((unsigned) st.st_mtime > expire_time)
 			continue;
 		if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
 			continue;
@@ -2576,7 +2576,7 @@ int write_commit_graph(struct odb_source *source,
 			ctx.changed_paths = 1;
 
 			/* don't propagate the hash_version unless unspecified */
-			if (bloom_settings.hash_version == -1)
+			if (bloom_settings.hash_version == (unsigned) -1)
 				bloom_settings.hash_version = g->bloom_filter_settings->hash_version;
 			bloom_settings.bits_per_entry = g->bloom_filter_settings->bits_per_entry;
 			bloom_settings.num_hashes = g->bloom_filter_settings->num_hashes;
-- 
2.51.0.rc0.215.g125493bb4a.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 42 of 69 in “commit-graph: remove reliance on global state”
  1. 0/9 commit-graph: remove reliance on global statePatrick Steinhardt, Aug 4, 2025
  2. 1/9 trace2: introduce function to trace unsigned integersPatrick Steinhardt, Aug 4, 2025
  3. Taylor BlauAug 4, 2025
  4. 2/9 commit-graph: stop using signed integers to count bloom filtersPatrick Steinhardt, Aug 4, 2025
  5. Oswald BuddenhagenAug 4, 2025
  6. Patrick SteinhardtAug 4, 2025
  7. Junio C HamanoAug 4, 2025
  8. Taylor BlauAug 4, 2025
  9. Patrick SteinhardtAug 6, 2025
  10. Oswald BuddenhagenAug 6, 2025
  11. Junio C HamanoAug 6, 2025
  12. Junio C HamanoAug 6, 2025
  13. Patrick SteinhardtAug 7, 2025
  14. Junio C HamanoAug 7, 2025
  15. Patrick SteinhardtAug 11, 2025
  16. Junio C HamanoAug 5, 2025
  17. Taylor BlauAug 4, 2025
  18. 3/9 commit-graph: fix type for some write optionsPatrick Steinhardt, Aug 4, 2025
  19. Taylor BlauAug 4, 2025
  20. 4/9 commit-graph: fix sign comparison warningsPatrick Steinhardt, Aug 4, 2025
  21. Taylor BlauAug 4, 2025
  22. Patrick SteinhardtAug 6, 2025
  23. 5/9 commit-graph: stop using `the_hash_algo` via macrosPatrick Steinhardt, Aug 4, 2025
  24. Taylor BlauAug 4, 2025
  25. 6/9 commit-graph: store the hash algorithm instead of its lengthPatrick Steinhardt, Aug 4, 2025
  26. Taylor BlauAug 4, 2025
  27. 7/9 commit-graph: stop using `the_hash_algo`Patrick Steinhardt, Aug 4, 2025
  28. Taylor BlauAug 4, 2025
  29. Patrick SteinhardtAug 6, 2025
  30. 8/9 commit-graph: stop using `the_repository`Patrick Steinhardt, Aug 4, 2025
  31. Taylor BlauAug 4, 2025
  32. 9/9 commit-graph: stop passing in redundant repositoryPatrick Steinhardt, Aug 4, 2025
  33. Derrick StoleeAug 5, 2025
  34. Patrick SteinhardtAug 6, 2025
  35. 00/10 commit-graph: remove reliance on global statePatrick Steinhardt, Aug 6, 2025
  36. 01/10 trace2: introduce function to trace unsigned integersPatrick Steinhardt, Aug 6, 2025
  37. 02/10 commit-graph: stop using signed integers to count Bloom filtersPatrick Steinhardt, Aug 6, 2025
  38. 03/10 commit-graph: fix type for some write optionsPatrick Steinhardt, Aug 6, 2025
  39. Oswald BuddenhagenAug 6, 2025
  40. Junio C HamanoAug 6, 2025
  41. Patrick SteinhardtAug 7, 2025
  42. 04/10 commit-graph: fix sign comparison warningsPatrick Steinhardt, Aug 6, 2025
  43. 05/10 commit-graph: stop using `the_hash_algo` via macrosPatrick Steinhardt, Aug 6, 2025
  44. 06/10 commit-graph: store the hash algorithm instead of its lengthPatrick Steinhardt, Aug 6, 2025
  45. 07/10 commit-graph: refactor `parse_commit_graph()` to take a repositoryPatrick Steinhardt, Aug 6, 2025
  46. 08/10 commit-graph: stop using `the_hash_algo`Patrick Steinhardt, Aug 6, 2025
  47. 09/10 commit-graph: stop using `the_repository`Patrick Steinhardt, Aug 6, 2025
  48. 10/10 commit-graph: stop passing in redundant repositoryPatrick Steinhardt, Aug 6, 2025
  49. 00/10 commit-graph: remove reliance on global statePatrick Steinhardt, Aug 7, 2025
  50. 01/10 trace2: introduce function to trace unsigned integersPatrick Steinhardt, Aug 7, 2025
  51. 02/10 commit-graph: stop using signed integers to count Bloom filtersPatrick Steinhardt, Aug 7, 2025
  52. 03/10 commit-graph: fix type for some write optionsPatrick Steinhardt, Aug 7, 2025
  53. Junio C HamanoAug 7, 2025
  54. Patrick SteinhardtAug 11, 2025
  55. 04/10 commit-graph: fix sign comparison warningsPatrick Steinhardt, Aug 7, 2025
  56. 05/10 commit-graph: stop using `the_hash_algo` via macrosPatrick Steinhardt, Aug 7, 2025
  57. 06/10 commit-graph: store the hash algorithm instead of its lengthPatrick Steinhardt, Aug 7, 2025
  58. 07/10 commit-graph: refactor `parse_commit_graph()` to take a repositoryPatrick Steinhardt, Aug 7, 2025
  59. 08/10 commit-graph: stop using `the_hash_algo`Patrick Steinhardt, Aug 7, 2025
  60. 09/10 commit-graph: stop using `the_repository`Patrick Steinhardt, Aug 7, 2025
  61. 10/10 commit-graph: stop passing in redundant repositoryPatrick Steinhardt, Aug 7, 2025
  62. 0/6 commit-graph: remove reliance on global statePatrick Steinhardt, Aug 15, 2025
  63. 1/6 commit-graph: stop using `the_hash_algo` via macrosPatrick Steinhardt, Aug 15, 2025
  64. 2/6 commit-graph: store the hash algorithm instead of its lengthPatrick Steinhardt, Aug 15, 2025
  65. 3/6 commit-graph: refactor `parse_commit_graph()` to take a repositoryPatrick Steinhardt, Aug 15, 2025
  66. 4/6 commit-graph: stop using `the_hash_algo`Patrick Steinhardt, Aug 15, 2025
  67. 5/6 commit-graph: stop using `the_repository`Patrick Steinhardt, Aug 15, 2025
  68. 6/6 commit-graph: stop passing in redundant repositoryPatrick Steinhardt, Aug 15, 2025
  69. Derrick StoleeAug 15, 2025

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.