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

[PATCH 8/9] shallow: fix -Wsign-compare warnings

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 27, 2024, 10:46 UTC
Message-ID
<20241227-b4-pks-commit-reach-sign-compare-v1-8-07c59c2aa632@pks.im>
In-Reply-To
<20241227-b4-pks-commit-reach-sign-compare-v1-0-07c59c2aa632@pks.im>

Fix a couple of -Wsign-compare issues in "shallow.c" and mark the file as -Wsign-compare-clean. This change prepares the code for a refactoring of `repo_in_merge_bases_many()`, which will be adapted to accept the number of commits as `size_t` instead of `int`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 shallow.c | 38 ++++++++++++++++++--------------------
 shallow.h |  6 +++---
 2 files changed, 21 insertions(+), 23 deletions(-)
diff --git a/shallow.c b/shallow.c
index 82a8da3d730b2f4f6bda462c8ac347f0b10da993..b8fcfbef0f9cdbfa895d4d6e1214f67942f88d25 100644
--- a/shallow.c
+++ b/shallow.c
@@ -1,5 +1,4 @@
 #define USE_THE_REPOSITORY_VARIABLE
-#define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "git-compat-util.h"
 #include "hex.h"
@@ -134,7 +133,8 @@ static void free_depth_in_slab(int **ptr)
 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
 		int shallow_flag, int not_shallow_flag)
 {
-	int i = 0, cur_depth = 0;
+	size_t i = 0;
+	int cur_depth = 0;
 	struct commit_list *result = NULL;
 	struct object_array stack = OBJECT_ARRAY_INIT;
 	struct commit *commit = NULL;
@@ -335,16 +335,16 @@ static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
 				   const struct oid_array *extra,
 				   unsigned flags)
 {
-	struct write_shallow_data data;
-	int i;
-	data.out = out;
-	data.use_pack_protocol = use_pack_protocol;
-	data.count = 0;
-	data.flags = flags;
+	struct write_shallow_data data = {
+		.out = out,
+		.use_pack_protocol = use_pack_protocol,
+		.flags = flags,
+	};
+
 	for_each_commit_graft(write_one_shallow, &data);
 	if (!extra)
 		return data.count;
-	for (i = 0; i < extra->nr; i++) {
+	for (size_t i = 0; i < extra->nr; i++) {
 		strbuf_addstr(out, oid_to_hex(extra->oid + i));
 		strbuf_addch(out, '\n');
 		data.count++;
@@ -466,7 +466,6 @@ struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
  */
 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
 {
-	int i;
 	trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
 	memset(info, 0, sizeof(*info));
 	info->shallow = sa;
@@ -474,7 +473,7 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
 		return;
 	ALLOC_ARRAY(info->ours, sa->nr);
 	ALLOC_ARRAY(info->theirs, sa->nr);
-	for (i = 0; i < sa->nr; i++) {
+	for (size_t i = 0; i < sa->nr; i++) {
 		if (repo_has_object_file(the_repository, sa->oid + i)) {
 			struct commit_graft *graft;
 			graft = lookup_commit_graft(the_repository,
@@ -507,7 +506,7 @@ void clear_shallow_info(struct shallow_info *info)
 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
 {
 	struct object_id *oid = info->shallow->oid;
-	int i, dst;
+	size_t i, dst;
 	trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
 	for (i = dst = 0; i < info->nr_theirs; i++) {
 		if (i != dst)
@@ -560,7 +559,7 @@ static void paint_down(struct paint_info *info, const struct object_id *oid,
 {
 	unsigned int i, nr;
 	struct commit_list *head = NULL;
-	int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
+	size_t bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
 	size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
 	struct commit *c = lookup_commit_reference_gently(the_repository, oid,
 							  1);
@@ -660,7 +659,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
 	struct object_id *oid = info->shallow->oid;
 	struct oid_array *ref = info->ref;
 	unsigned int i, nr;
-	int *shallow, nr_shallow = 0;
+	size_t *shallow, nr_shallow = 0;
 	struct paint_info pi;
 
 	trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
@@ -735,7 +734,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
 
 struct commit_array {
 	struct commit **commits;
-	int nr, alloc;
+	size_t nr, alloc;
 };
 
 static int add_ref(const char *refname UNUSED,
@@ -753,12 +752,11 @@ static int add_ref(const char *refname UNUSED,
 	return 0;
 }
 
-static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
+static void update_refstatus(int *ref_status, size_t nr, uint32_t *bitmap)
 {
-	unsigned int i;
 	if (!ref_status)
 		return;
-	for (i = 0; i < nr; i++)
+	for (size_t i = 0; i < nr; i++)
 		if (bitmap[i / 32] & (1U << (i % 32)))
 			ref_status[i]++;
 }
@@ -773,8 +771,8 @@ static void post_assign_shallow(struct shallow_info *info,
 	struct object_id *oid = info->shallow->oid;
 	struct commit *c;
 	uint32_t **bitmap;
-	int dst, i, j;
-	int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
+	size_t dst, i, j;
+	size_t bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
 	struct commit_array ca;
 
 	trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
diff --git a/shallow.h b/shallow.h
index e9ca7e4bc80451a74dc10fb6d4e1a0b190dc4dcc..9bfeade93ead7421d7f2991862dde88ba546fc64 100644
--- a/shallow.h
+++ b/shallow.h
@@ -59,8 +59,8 @@ void prune_shallow(unsigned options);
  */
 struct shallow_info {
 	struct oid_array *shallow;
-	int *ours, nr_ours;
-	int *theirs, nr_theirs;
+	size_t *ours, nr_ours;
+	size_t *theirs, nr_theirs;
 	struct oid_array *ref;
 
 	/* for receive-pack */
@@ -69,7 +69,7 @@ struct shallow_info {
 	int *reachable;
 	int *shallow_ref;
 	struct commit **commits;
-	int nr_commits;
+	size_t nr_commits;
 };
 
 void prepare_shallow_info(struct shallow_info *, struct oid_array *);
-- 
2.48.0.rc0.184.g0fc57dec57.dirty
Previous: shejialuoNext: Patrick Steinhardt
Message 17 of 34 in “commit-reach: -Wsign-compare follow-ups”
  1. 0/9 commit-reach: -Wsign-compare follow-upsPatrick Steinhardt, Dec 27, 2024
  2. 1/9 prio-queue: fix type of `insertion_ctr`Patrick Steinhardt, Dec 27, 2024
  3. Jeff KingDec 27, 2024
  4. 2/9 commit-reach: fix index used to loop through unsigned integerPatrick Steinhardt, Dec 27, 2024
  5. Jeff KingDec 27, 2024
  6. 3/9 commit-reach: fix type of `min_commit_date`Patrick Steinhardt, Dec 27, 2024
  7. 4/9 commit-reach: use `size_t` to track indices in `remove_redundant()`Patrick Steinhardt, Dec 27, 2024
  8. Justin ToblerJan 3, 2025
  9. 5/9 commit-reach: use `size_t` to track indices in `get_reachable_subset()`Patrick Steinhardt, Dec 27, 2024
  10. 6/9 builtin/log: use `size_t` to track indicesPatrick Steinhardt, Dec 27, 2024
  11. Justin ToblerJan 3, 2025
  12. Patrick SteinhardtJan 3, 2025
  13. 7/9 builtin/log: fix remaining -Wsign-compare warningsPatrick Steinhardt, Dec 27, 2024
  14. shejialuoDec 27, 2024
  15. Patrick SteinhardtDec 27, 2024
  16. shejialuoDec 27, 2024
  17. 8/9 shallow: fix -Wsign-compare warningsPatrick Steinhardt, Dec 27, 2024
  18. 9/9 commit-reach: use `size_t` to track indices when computing merge basesPatrick Steinhardt, Dec 27, 2024
  19. Justin ToblerJan 3, 2025
  20. Patrick SteinhardtJan 3, 2025
  21. Junio C HamanoDec 27, 2024
  22. Junio C HamanoDec 27, 2024
  23. Jeff KingDec 27, 2024
  24. Patrick SteinhardtDec 28, 2024
  25. Junio C HamanoDec 28, 2024
  26. Patrick SteinhardtDec 28, 2024
  27. Junio C HamanoDec 28, 2024
  28. racy leak sanitizer builds, was Re: [PATCH 0/9] commit-reach: -Wsign-compare follow-upsJeff King, Dec 28, 2024
  29. Jeff KingDec 28, 2024
  30. Jeff KingDec 28, 2024
  31. René ScharfeDec 29, 2024
  32. Jeff KingDec 29, 2024
  33. Jeff KingDec 30, 2024
  34. Junio C HamanoDec 30, 2024

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.