pathspec: fix sign comparison warnings

There are multiple places, especially 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. In some cases, where both signed and unsigned data types have been used to store lengths of arrays in the same function, only one variable was used to iterate over both types. Replace signed data types with unsigned data types and vice versa wherever necessary. Where both types of iterators are required, move the declaration inside the for loop. In cases where this is not possible, add appropriate cast. 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 30, 2025 at 23:15 UTC 6d29175c0f321f64a90385a2a3dab63ba8c76358
1 file changed +17 -15
pathspec.c
+17 -15
@@ -1,5 +1,4 @@
1 #define USE_THE_REPOSITORY_VARIABLE
2 -#define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "abspath.h"
@@ -35,7 +34,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
34 char *seen,
35 enum ps_skip_worktree_action sw_action)
36 {
38 - int num_unmatched = 0, i;
37 + int num_unmatched = 0;
38
39 /*
40 * Since we are walking the index as if we were walking the directory,
@@ -43,12 +42,12 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
42 * mistakenly think that the user gave a pathspec that did not match
43 * anything.
44 */
46 - for (i = 0; i < pathspec->nr; i++)
45 + for (int i = 0; i < pathspec->nr; i++)
46 if (!seen[i])
47 num_unmatched++;
48 if (!num_unmatched)
49 return;
51 - for (i = 0; i < istate->cache_nr; i++) {
50 + for (unsigned int i = 0; i < istate->cache_nr; i++) {
51 const struct cache_entry *ce = istate->cache[i];
52 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
53 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
@@ -78,7 +77,7 @@ char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
77 {
78 struct index_state *istate = the_repository->index;
79 char *seen = xcalloc(pathspec->nr, 1);
81 - int i;
80 + unsigned int i;
81
82 for (i = 0; i < istate->cache_nr; i++) {
83 struct cache_entry *ce = istate->cache[i];
@@ -130,7 +129,7 @@ static void prefix_magic(struct strbuf *sb, int prefixlen,
129 if (element[1] != '(') {
130 /* Process an element in shorthand form (e.g. ":!/<match>") */
131 strbuf_addstr(sb, ":(");
133 - for (int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
132 + for (unsigned int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
133 if ((magic & pathspec_magic[i].bit) &&
134 pathspec_magic[i].mnemonic) {
135 if (sb->buf[sb->len - 1] != '(')
@@ -341,7 +340,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
340
341 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
342 size_t len = strcspn_escaped(pos, ",)");
344 - int i;
343 + unsigned int i;
344
345 if (pos[len] == ',')
346 nextat = pos + len + 1; /* handle ',' */
@@ -354,7 +353,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
353 if (starts_with(pos, "prefix:")) {
354 char *endptr;
355 *prefix_len = strtol(pos + 7, &endptr, 10);
357 - if (endptr - pos != len)
356 + if ((size_t)(endptr - pos) != len)
357 die(_("invalid parameter for pathspec magic 'prefix'"));
358 continue;
359 }
@@ -400,7 +399,7 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
399
400 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
401 char ch = *pos;
403 - int i;
402 + unsigned int i;
403
404 /* Special case alias for '!' */
405 if (ch == '^') {
@@ -564,7 +563,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_)
563
564 void pathspec_magic_names(unsigned magic, struct strbuf *out)
565 {
567 - int i;
566 + unsigned int i;
567 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
568 const struct pathspec_magic *m = pathspec_magic + i;
569 if (!(magic & m->bit))
@@ -803,8 +802,8 @@ int match_pathspec_attrs(struct index_state *istate,
802 int pathspec_needs_expanded_index(struct index_state *istate,
803 const struct pathspec *pathspec)
804 {
806 - unsigned int i, pos;
807 - int res = 0;
805 + unsigned int pos;
806 + int i, res = 0;
807 char *skip_worktree_seen = NULL;
808
809 /*
@@ -845,7 +844,8 @@ int pathspec_needs_expanded_index(struct index_state *istate,
844 * - not-in-cone/bar*: may need expanded index
845 * - **.c: may need expanded index
846 */
848 - if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
847 + if (strspn(item.original + item.nowildcard_len, "*") ==
848 + (unsigned int)(item.len - item.nowildcard_len) &&
849 path_in_cone_mode_sparse_checkout(item.original, istate))
850 continue;
851
@@ -860,8 +860,10 @@ int pathspec_needs_expanded_index(struct index_state *istate,
860 * directory name and the sparse directory is the first
861 * component of the pathspec, need to expand the index.
862 */
863 - if (item.nowildcard_len > ce_namelen(ce) &&
864 - !strncmp(item.original, ce->name, ce_namelen(ce))) {
863 + if ((unsigned int)item.nowildcard_len >
864 + ce_namelen(ce) &&
865 + !strncmp(item.original, ce->name,
866 + ce_namelen(ce))) {
867 res = 1;
868 break;
869 }