Skip to content

Commit 6d29175

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

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

pathspec.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#define USE_THE_REPOSITORY_VARIABLE
2-
#define DISABLE_SIGN_COMPARE_WARNINGS
32

43
#include "git-compat-util.h"
54
#include "abspath.h"
@@ -35,20 +34,20 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
3534
char *seen,
3635
enum ps_skip_worktree_action sw_action)
3736
{
38-
int num_unmatched = 0, i;
37+
int num_unmatched = 0;
3938

4039
/*
4140
* Since we are walking the index as if we were walking the directory,
4241
* we have to mark the matched pathspec as seen; otherwise we will
4342
* mistakenly think that the user gave a pathspec that did not match
4443
* anything.
4544
*/
46-
for (i = 0; i < pathspec->nr; i++)
45+
for (int i = 0; i < pathspec->nr; i++)
4746
if (!seen[i])
4847
num_unmatched++;
4948
if (!num_unmatched)
5049
return;
51-
for (i = 0; i < istate->cache_nr; i++) {
50+
for (unsigned int i = 0; i < istate->cache_nr; i++) {
5251
const struct cache_entry *ce = istate->cache[i];
5352
if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
5453
(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)
7877
{
7978
struct index_state *istate = the_repository->index;
8079
char *seen = xcalloc(pathspec->nr, 1);
81-
int i;
80+
unsigned int i;
8281

8382
for (i = 0; i < istate->cache_nr; i++) {
8483
struct cache_entry *ce = istate->cache[i];
@@ -130,7 +129,7 @@ static void prefix_magic(struct strbuf *sb, int prefixlen,
130129
if (element[1] != '(') {
131130
/* Process an element in shorthand form (e.g. ":!/<match>") */
132131
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++) {
134133
if ((magic & pathspec_magic[i].bit) &&
135134
pathspec_magic[i].mnemonic) {
136135
if (sb->buf[sb->len - 1] != '(')
@@ -341,7 +340,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
341340

342341
for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
343342
size_t len = strcspn_escaped(pos, ",)");
344-
int i;
343+
unsigned int i;
345344

346345
if (pos[len] == ',')
347346
nextat = pos + len + 1; /* handle ',' */
@@ -354,7 +353,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
354353
if (starts_with(pos, "prefix:")) {
355354
char *endptr;
356355
*prefix_len = strtol(pos + 7, &endptr, 10);
357-
if (endptr - pos != len)
356+
if ((size_t)(endptr - pos) != len)
358357
die(_("invalid parameter for pathspec magic 'prefix'"));
359358
continue;
360359
}
@@ -400,7 +399,7 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
400399

401400
for (pos = elem + 1; *pos && *pos != ':'; pos++) {
402401
char ch = *pos;
403-
int i;
402+
unsigned int i;
404403

405404
/* Special case alias for '!' */
406405
if (ch == '^') {
@@ -564,7 +563,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_)
564563

565564
void pathspec_magic_names(unsigned magic, struct strbuf *out)
566565
{
567-
int i;
566+
unsigned int i;
568567
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
569568
const struct pathspec_magic *m = pathspec_magic + i;
570569
if (!(magic & m->bit))
@@ -803,8 +802,8 @@ int match_pathspec_attrs(struct index_state *istate,
803802
int pathspec_needs_expanded_index(struct index_state *istate,
804803
const struct pathspec *pathspec)
805804
{
806-
unsigned int i, pos;
807-
int res = 0;
805+
unsigned int pos;
806+
int i, res = 0;
808807
char *skip_worktree_seen = NULL;
809808

810809
/*
@@ -845,7 +844,8 @@ int pathspec_needs_expanded_index(struct index_state *istate,
845844
* - not-in-cone/bar*: may need expanded index
846845
* - **.c: may need expanded index
847846
*/
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) &&
849849
path_in_cone_mode_sparse_checkout(item.original, istate))
850850
continue;
851851

@@ -860,8 +860,10 @@ int pathspec_needs_expanded_index(struct index_state *istate,
860860
* directory name and the sparse directory is the first
861861
* component of the pathspec, need to expand the index.
862862
*/
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))) {
865867
res = 1;
866868
break;
867869
}

0 commit comments

Comments
 (0)