Skip to content

Commit d2fc293

Browse files
arnavbhategitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit d2fc293

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

builtin/rm.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
#define USE_THE_REPOSITORY_VARIABLE
8-
#define DISABLE_SIGN_COMPARE_WARNINGS
98

109
#include "builtin.h"
1110
#include "advice.h"
@@ -40,14 +39,12 @@ static struct {
4039
} *entry;
4140
} list;
4241

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)
4443
{
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++;
5148
}
5249
return -1;
5350
}
@@ -58,7 +55,7 @@ static void print_error_files(struct string_list *files_list,
5855
int *errs)
5956
{
6057
if (files_list->nr) {
61-
int i;
58+
unsigned int i;
6259
struct strbuf err_msg = STRBUF_INIT;
6360

6461
strbuf_addstr(&err_msg, main_msg);
@@ -83,7 +80,7 @@ static void submodules_absorb_gitdir_if_needed(void)
8380

8481
pos = index_name_pos(the_repository->index, name, strlen(name));
8582
if (pos < 0) {
86-
pos = get_ours_cache_pos(name, pos);
83+
pos = get_ours_cache_pos(name, -pos - 1);
8784
if (pos < 0)
8885
continue;
8986
}
@@ -131,7 +128,7 @@ static int check_local_mod(struct object_id *head, int index_only)
131128
* Skip unmerged entries except for populated submodules
132129
* that could lose history when removed.
133130
*/
134-
pos = get_ours_cache_pos(name, pos);
131+
pos = get_ours_cache_pos(name, -pos - 1);
135132
if (pos < 0)
136133
continue;
137134

@@ -314,7 +311,7 @@ int cmd_rm(int argc,
314311
if (pathspec_needs_expanded_index(the_repository->index, &pathspec))
315312
ensure_full_index(the_repository->index);
316313

317-
for (i = 0; i < the_repository->index->cache_nr; i++) {
314+
for (unsigned int i = 0; i < the_repository->index->cache_nr; i++) {
318315
const struct cache_entry *ce = the_repository->index->cache[i];
319316

320317
if (!include_sparse &&

0 commit comments

Comments
 (0)