git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [GSoC PATCH] rm: fix sign comparison warnings
@ 2025-03-12 20:19 Arnav Bhate
  2025-03-13  7:16 ` Junio C Hamano
  2025-03-16 10:13 ` [GSoC PATCH v2] " Arnav Bhate
  0 siblings, 2 replies; 15+ messages in thread
From: Arnav Bhate @ 2025-03-12 20:19 UTC (permalink / raw)
  To: git

There are multiple places in loops, where a signed and an
unsigned data type are compared. Git uses a mix of signed and unsigned
types to store lengths of arrays. This sometimes leads to using a signed
index for an array whose length is stored in an unsigned variable or
vice versa.

get_ours_cache_pos is a special case where the initial index
is derived from a signed variable, however, upon checking the usage of
the function, it is clear that it cannot be negative, and hence can be
replaced by an unsigned variable.

Replace signed data types with unsigned data types and vice versa
wherever necessary. In some cases, introduce a new variable, where both
signed and unsigned data types have been used to store lengths of arrays
in the same function, where previously only one variable was used to
iterate over both types. Remove #define DISABLE_SIGN_COMPARE_WARNINGS.

Signed-off-by: Arnav Bhate <bhatearnav@gmail.com>
---
 builtin/rm.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/builtin/rm.c b/builtin/rm.c
index 12ae086a55..4ebfa7539d 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -5,7 +5,6 @@
  */
 
 #define USE_THE_REPOSITORY_VARIABLE
-#define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "builtin.h"
 #include "advice.h"
@@ -42,7 +41,12 @@ static struct {
 
 static int get_ours_cache_pos(const char *path, int pos)
 {
-	int i = -pos - 1;
+	/*
+	 * This function is only called when pos < 0, so -pos - 1 is
+	 * greater than or equal to 0, so it can be safely be stored in
+	 * an unsigned int.
+	 */
+	unsigned int i = -pos - 1;
 
 	while ((i < the_repository->index->cache_nr) && !strcmp(the_repository->index->cache[i]->name, path)) {
 		if (ce_stage(the_repository->index->cache[i]) == 2)
@@ -58,7 +62,7 @@ static void print_error_files(struct string_list *files_list,
 			      int *errs)
 {
 	if (files_list->nr) {
-		int i;
+		unsigned int i;
 		struct strbuf err_msg = STRBUF_INIT;
 
 		strbuf_addstr(&err_msg, main_msg);
@@ -271,6 +275,7 @@ int cmd_rm(int argc,
 {
 	struct lock_file lock_file = LOCK_INIT;
 	int i, ret = 0;
+	unsigned int j;
 	struct pathspec pathspec;
 	char *seen;
 
@@ -314,8 +319,8 @@ int cmd_rm(int argc,
 	if (pathspec_needs_expanded_index(the_repository->index, &pathspec))
 		ensure_full_index(the_repository->index);
 
-	for (i = 0; i < the_repository->index->cache_nr; i++) {
-		const struct cache_entry *ce = the_repository->index->cache[i];
+	for (j = 0; j < the_repository->index->cache_nr; j++) {
+		const struct cache_entry *ce = the_repository->index->cache[j];
 
 		if (!include_sparse &&
 		    (ce_skip_worktree(ce) ||
-- 
2.48.1

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-03-29  6:07 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-12 20:19 [GSoC PATCH] rm: fix sign comparison warnings Arnav Bhate
2025-03-13  7:16 ` Junio C Hamano
2025-03-13 11:25   ` Karthik Nayak
2025-03-13 14:30     ` Arnav Bhate
2025-03-13 14:45       ` Karthik Nayak
2025-03-13 15:25     ` Junio C Hamano
2025-03-13 14:26   ` Arnav Bhate
2025-03-16 10:13 ` [GSoC PATCH v2] " Arnav Bhate
2025-03-17 16:47   ` Junio C Hamano
2025-03-17 17:05     ` Arnav Bhate
2025-03-17 17:07   ` [GSoC PATCH v3] " Arnav Bhate
2025-03-17 17:12     ` Arnav Bhate
2025-03-17 17:10   ` Arnav Bhate
2025-03-29  6:03     ` [GSoC PATCH v4] " Arnav Bhate
2025-03-29  6:07       ` Arnav Bhate

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).