rm: fix sign comparison warnings

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 i, though derived from a signed variable is never negative. Move this part to the caller side and make i an unsigned argument of the function. Rename i to pos to make it descriptive, now that it is a function argument. Replace signed data types with unsigned data types and vice versa wherever necessary. Where both signed and unsigned data types have been used, define a new variable in the scope of the for loop for use as the iterator. Remove #define DISABLE_SIGN_COMPARE_WARNINGS. Signed-off-by: Arnav Bhate <bhatearnav@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Arnav Bhate committed Mar 29, 2025 at 11:33 UTC d2fc29380acb5bba36cbc7a12907630a5308b6b1
1 file changed +9 -12
builtin/rm.c
+9 -12
@@ -5,7 +5,6 @@
5 */
6
7 #define USE_THE_REPOSITORY_VARIABLE
8 -#define DISABLE_SIGN_COMPARE_WARNINGS
8
9 #include "builtin.h"
10 #include "advice.h"
@@ -40,14 +39,12 @@ static struct {
39 } *entry;
40 } list;
41
43 -static int get_ours_cache_pos(const char *path, int pos)
42 +static int get_ours_cache_pos(const char *path, unsigned int pos)
43 {
45 - int i = -pos - 1;
46 -
47 - while ((i < the_repository->index->cache_nr) && !strcmp(the_repository->index->cache[i]->name, path)) {
48 - if (ce_stage(the_repository->index->cache[i]) == 2)
49 - return i;
50 - i++;
44 + while ((pos < the_repository->index->cache_nr) && !strcmp(the_repository->index->cache[pos]->name, path)) {
45 + if (ce_stage(the_repository->index->cache[pos]) == 2)
46 + return pos;
47 + pos++;
48 }
49 return -1;
50 }
@@ -58,7 +55,7 @@ static void print_error_files(struct string_list *files_list,
55 int *errs)
56 {
57 if (files_list->nr) {
61 - int i;
58 + unsigned int i;
59 struct strbuf err_msg = STRBUF_INIT;
60
61 strbuf_addstr(&err_msg, main_msg);
@@ -83,7 +80,7 @@ static void submodules_absorb_gitdir_if_needed(void)
80
81 pos = index_name_pos(the_repository->index, name, strlen(name));
82 if (pos < 0) {
86 - pos = get_ours_cache_pos(name, pos);
83 + pos = get_ours_cache_pos(name, -pos - 1);
84 if (pos < 0)
85 continue;
86 }
@@ -131,7 +128,7 @@ static int check_local_mod(struct object_id *head, int index_only)
128 * Skip unmerged entries except for populated submodules
129 * that could lose history when removed.
130 */
134 - pos = get_ours_cache_pos(name, pos);
131 + pos = get_ours_cache_pos(name, -pos - 1);
132 if (pos < 0)
133 continue;
134
@@ -314,7 +311,7 @@ int cmd_rm(int argc,
311 if (pathspec_needs_expanded_index(the_repository->index, &pathspec))
312 ensure_full_index(the_repository->index);
313
317 - for (i = 0; i < the_repository->index->cache_nr; i++) {
314 + for (unsigned int i = 0; i < the_repository->index->cache_nr; i++) {
315 const struct cache_entry *ce = the_repository->index->cache[i];
316
317 if (!include_sparse &&