Skip to content

Commit 80c9e70

Browse files
pks-tgitster
authored andcommitted
global: trivial conversions to fix -Wsign-compare warnings
We have a bunch of loops which iterate up to an unsigned boundary using a signed index, which generates warnigs because we compare a signed and unsigned value in the loop condition. Address these sites for trivial cases and enable `-Wsign-compare` warnings for these code units. This patch only adapts those code units where we can drop the `DISABLE_SIGN_COMPARE_WARNINGS` macro in the same step. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 25435e4 commit 80c9e70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+105
-238
lines changed

advice.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define DISABLE_SIGN_COMPARE_WARNINGS
2-
31
#include "git-compat-util.h"
42
#include "advice.h"
53
#include "config.h"
@@ -162,7 +160,6 @@ void advise_if_enabled(enum advice_type type, const char *advice, ...)
162160
int git_default_advice_config(const char *var, const char *value)
163161
{
164162
const char *k, *slot_name;
165-
int i;
166163

167164
if (!strcmp(var, "color.advice")) {
168165
advice_use_color = git_config_colorbool(var, value);
@@ -181,7 +178,7 @@ int git_default_advice_config(const char *var, const char *value)
181178
if (!skip_prefix(var, "advice.", &k))
182179
return 0;
183180

184-
for (i = 0; i < ARRAY_SIZE(advice_setting); i++) {
181+
for (size_t i = 0; i < ARRAY_SIZE(advice_setting); i++) {
185182
if (strcasecmp(k, advice_setting[i].key))
186183
continue;
187184
advice_setting[i].level = git_config_bool(var, value)
@@ -195,9 +192,7 @@ int git_default_advice_config(const char *var, const char *value)
195192

196193
void list_config_advices(struct string_list *list, const char *prefix)
197194
{
198-
int i;
199-
200-
for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
195+
for (size_t i = 0; i < ARRAY_SIZE(advice_setting); i++)
201196
list_config_item(list, prefix, advice_setting[i].key);
202197
}
203198

base85.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define DISABLE_SIGN_COMPARE_WARNINGS
2-
31
#include "git-compat-util.h"
42
#include "base85.h"
53

@@ -31,10 +29,9 @@ static const char en85[] = {
3129
static char de85[256];
3230
static void prep_base85(void)
3331
{
34-
int i;
3532
if (de85['Z'])
3633
return;
37-
for (i = 0; i < ARRAY_SIZE(en85); i++) {
34+
for (size_t i = 0; i < ARRAY_SIZE(en85); i++) {
3835
int ch = en85[i];
3936
de85[ch] = i + 1;
4037
}

builtin/add.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* Copyright (C) 2006 Linus Torvalds
55
*/
66

7-
#define DISABLE_SIGN_COMPARE_WARNINGS
8-
97
#include "builtin.h"
108
#include "advice.h"
119
#include "config.h"
@@ -42,9 +40,9 @@ static int chmod_pathspec(struct repository *repo,
4240
char flip,
4341
int show_only)
4442
{
45-
int i, ret = 0;
43+
int ret = 0;
4644

47-
for (i = 0; i < repo->index->cache_nr; i++) {
45+
for (size_t i = 0; i < repo->index->cache_nr; i++) {
4846
struct cache_entry *ce = repo->index->cache[i];
4947
int err;
5048

@@ -72,9 +70,9 @@ static int renormalize_tracked_files(struct repository *repo,
7270
const struct pathspec *pathspec,
7371
int flags)
7472
{
75-
int i, retval = 0;
73+
int retval = 0;
7674

77-
for (i = 0; i < repo->index->cache_nr; i++) {
75+
for (size_t i = 0; i < repo->index->cache_nr; i++) {
7876
struct cache_entry *ce = repo->index->cache[i];
7977

8078
if (!include_sparse &&

builtin/branch.c

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
#define USE_THE_REPOSITORY_VARIABLE
9-
#define DISABLE_SIGN_COMPARE_WARNINGS
109

1110
#include "builtin.h"
1211
#include "config.h"

builtin/difftool.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414

1515
#define USE_THE_REPOSITORY_VARIABLE
16-
#define DISABLE_SIGN_COMPARE_WARNINGS
1716

1817
#include "builtin.h"
1918

@@ -367,7 +366,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
367366
char *lbase_dir = NULL, *rbase_dir = NULL;
368367
size_t ldir_len, rdir_len, wtdir_len;
369368
const char *workdir, *tmp;
370-
int ret = 0, i;
369+
int ret = 0;
370+
size_t i;
371371
FILE *fp = NULL;
372372
struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
373373
NULL);

builtin/for-each-repo.c

+2-3
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 "builtin.h"
54
#include "config.h"
@@ -38,7 +37,7 @@ int cmd_for_each_repo(int argc,
3837
{
3938
static const char *config_key = NULL;
4039
int keep_going = 0;
41-
int i, result = 0;
40+
int result = 0;
4241
const struct string_list *values;
4342
int err;
4443

@@ -63,7 +62,7 @@ int cmd_for_each_repo(int argc,
6362
else if (err)
6463
return 0;
6564

66-
for (i = 0; i < values->nr; i++) {
65+
for (size_t i = 0; i < values->nr; i++) {
6766
int ret = run_command_on_repo(values->items[i].string, argc, argv);
6867
if (ret) {
6968
if (!keep_going)

builtin/help.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
#define USE_THE_REPOSITORY_VARIABLE
6-
#define DISABLE_SIGN_COMPARE_WARNINGS
76

87
#include "builtin.h"
98
#include "config.h"
@@ -131,7 +130,6 @@ static void list_config_help(enum show_config_type type)
131130
struct string_list keys = STRING_LIST_INIT_DUP;
132131
struct string_list keys_uniq = STRING_LIST_INIT_DUP;
133132
struct string_list_item *item;
134-
int i;
135133

136134
for (p = config_name_list; *p; p++) {
137135
const char *var = *p;
@@ -158,7 +156,7 @@ static void list_config_help(enum show_config_type type)
158156
e->prefix, e->placeholder);
159157

160158
string_list_sort(&keys);
161-
for (i = 0; i < keys.nr; i++) {
159+
for (size_t i = 0; i < keys.nr; i++) {
162160
const char *var = keys.items[i].string;
163161
const char *wildcard, *tag, *cut;
164162
const char *dot = NULL;

builtin/mailsplit.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,14 @@ static int split_maildir(const char *maildir, const char *dir,
175175
char *file = NULL;
176176
FILE *f = NULL;
177177
int ret = -1;
178-
int i;
179178
struct string_list list = STRING_LIST_INIT_DUP;
180179

181180
list.cmp = maildir_filename_cmp;
182181

183182
if (populate_maildir_list(&list, maildir) < 0)
184183
goto out;
185184

186-
for (i = 0; i < list.nr; i++) {
185+
for (size_t i = 0; i < list.nr; i++) {
187186
char *name;
188187

189188
free(file);

builtin/merge-tree.c

+2-4
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 "builtin.h"
54
#include "tree-walk.h"
@@ -499,10 +498,9 @@ static int real_merge(struct merge_tree_options *o,
499498
if (!result.clean) {
500499
struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
501500
const char *last = NULL;
502-
int i;
503501

504502
merge_get_conflicted_files(&result, &conflicted_files);
505-
for (i = 0; i < conflicted_files.nr; i++) {
503+
for (size_t i = 0; i < conflicted_files.nr; i++) {
506504
const char *name = conflicted_files.items[i].string;
507505
struct stage_info *c = conflicted_files.items[i].util;
508506
if (!o->name_only)
@@ -586,7 +584,7 @@ int cmd_merge_tree(int argc,
586584

587585
if (xopts.nr && o.mode == MODE_TRIVIAL)
588586
die(_("--trivial-merge is incompatible with all other options"));
589-
for (int x = 0; x < xopts.nr; x++)
587+
for (size_t x = 0; x < xopts.nr; x++)
590588
if (parse_merge_opt(&o.merge_options, xopts.v[x]))
591589
die(_("unknown strategy option: -X%s"), xopts.v[x]);
592590

builtin/pack-redundant.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
#define USE_THE_REPOSITORY_VARIABLE
10-
#define DISABLE_SIGN_COMPARE_WARNINGS
1110

1211
#include "builtin.h"
1312
#include "gettext.h"
@@ -391,7 +390,6 @@ static int cmp_remaining_objects(const void *a, const void *b)
391390
static void sort_pack_list(struct pack_list **pl)
392391
{
393392
struct pack_list **ary, *p;
394-
int i;
395393
size_t n = pack_list_size(*pl);
396394

397395
if (n < 2)
@@ -405,7 +403,7 @@ static void sort_pack_list(struct pack_list **pl)
405403
QSORT(ary, n, cmp_remaining_objects);
406404

407405
/* link them back again */
408-
for (i = 0; i < n - 1; i++)
406+
for (size_t i = 0; i < n - 1; i++)
409407
ary[i]->next = ary[i + 1];
410408
ary[n - 1]->next = NULL;
411409
*pl = ary[0];

builtin/pull.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
#define USE_THE_REPOSITORY_VARIABLE
10-
#define DISABLE_SIGN_COMPARE_WARNINGS
1110

1211
#include "builtin.h"
1312
#include "advice.h"
@@ -943,11 +942,10 @@ static int get_can_ff(struct object_id *orig_head,
943942
static int already_up_to_date(struct object_id *orig_head,
944943
struct oid_array *merge_heads)
945944
{
946-
int i;
947945
struct commit *ours;
948946

949947
ours = lookup_commit_reference(the_repository, orig_head);
950-
for (i = 0; i < merge_heads->nr; i++) {
948+
for (size_t i = 0; i < merge_heads->nr; i++) {
951949
struct commit_list *list = NULL;
952950
struct commit *theirs;
953951
int ok;

builtin/push.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
#define USE_THE_REPOSITORY_VARIABLE
6-
#define DISABLE_SIGN_COMPARE_WARNINGS
76

87
#include "builtin.h"
98
#include "advice.h"
@@ -420,7 +419,7 @@ static int do_push(int flags,
420419
const struct string_list *push_options,
421420
struct remote *remote)
422421
{
423-
int i, errs;
422+
int errs;
424423
struct strvec *url;
425424
struct refspec *push_refspec = &rs;
426425

@@ -435,7 +434,7 @@ static int do_push(int flags,
435434
}
436435
errs = 0;
437436
url = push_url_of_remote(remote);
438-
for (i = 0; i < url->nr; i++) {
437+
for (size_t i = 0; i < url->nr; i++) {
439438
struct transport *transport =
440439
transport_get(remote, url->v[i]);
441440
if (flags & TRANSPORT_PUSH_OPTIONS)

builtin/rerere.c

+4-5
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 "builtin.h"
54
#include "config.h"
@@ -57,7 +56,7 @@ int cmd_rerere(int argc,
5756
struct repository *repo UNUSED)
5857
{
5958
struct string_list merge_rr = STRING_LIST_INIT_DUP;
60-
int i, autoupdate = -1, flags = 0;
59+
int autoupdate = -1, flags = 0;
6160

6261
struct option options[] = {
6362
OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
@@ -100,11 +99,11 @@ int cmd_rerere(int argc,
10099
if (setup_rerere(the_repository, &merge_rr,
101100
flags | RERERE_READONLY) < 0)
102101
return 0;
103-
for (i = 0; i < merge_rr.nr; i++)
102+
for (size_t i = 0; i < merge_rr.nr; i++)
104103
printf("%s\n", merge_rr.items[i].string);
105104
} else if (!strcmp(argv[0], "remaining")) {
106105
rerere_remaining(the_repository, &merge_rr);
107-
for (i = 0; i < merge_rr.nr; i++) {
106+
for (size_t i = 0; i < merge_rr.nr; i++) {
108107
if (merge_rr.items[i].util != RERERE_RESOLVED)
109108
printf("%s\n", merge_rr.items[i].string);
110109
else
@@ -116,7 +115,7 @@ int cmd_rerere(int argc,
116115
if (setup_rerere(the_repository, &merge_rr,
117116
flags | RERERE_READONLY) < 0)
118117
return 0;
119-
for (i = 0; i < merge_rr.nr; i++) {
118+
for (size_t i = 0; i < merge_rr.nr; i++) {
120119
const char *path = merge_rr.items[i].string;
121120
const struct rerere_id *id = merge_rr.items[i].util;
122121
if (diff_two(rerere_path(id, "preimage"), path, path, path))

builtin/stash.c

+2-5
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 "builtin.h"
54
#include "abspath.h"
@@ -875,9 +874,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
875874
struct tree *tree[ARRAY_SIZE(oid)];
876875
struct tree_desc tree_desc[ARRAY_SIZE(oid)];
877876
struct unpack_trees_options unpack_tree_opt = { 0 };
878-
int i;
879877

880-
for (i = 0; i < ARRAY_SIZE(oid); i++) {
878+
for (size_t i = 0; i < ARRAY_SIZE(oid); i++) {
881879
tree[i] = parse_tree_indirect(oid[i]);
882880
if (parse_tree(tree[i]) < 0)
883881
die(_("failed to parse tree"));
@@ -1559,12 +1557,11 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
15591557

15601558
repo_read_index_preload(the_repository, NULL, 0);
15611559
if (!include_untracked && ps->nr) {
1562-
int i;
15631560
char *ps_matched = xcalloc(ps->nr, 1);
15641561

15651562
/* TODO: audit for interaction with sparse-index. */
15661563
ensure_full_index(the_repository->index);
1567-
for (i = 0; i < the_repository->index->cache_nr; i++)
1564+
for (size_t i = 0; i < the_repository->index->cache_nr; i++)
15681565
ce_path_match(the_repository->index, the_repository->index->cache[i], ps,
15691566
ps_matched);
15701567

builtin/submodule--helper.c

+3-5
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 "builtin.h"
54
#include "abspath.h"
@@ -196,7 +195,7 @@ static int module_list_compute(const char **argv,
196195
struct pathspec *pathspec,
197196
struct module_list *list)
198197
{
199-
int i, result = 0;
198+
int result = 0;
200199
char *ps_matched = NULL;
201200

202201
parse_pathspec(pathspec, 0,
@@ -209,7 +208,7 @@ static int module_list_compute(const char **argv,
209208
if (repo_read_index(the_repository) < 0)
210209
die(_("index file corrupt"));
211210

212-
for (i = 0; i < the_repository->index->cache_nr; i++) {
211+
for (size_t i = 0; i < the_repository->index->cache_nr; i++) {
213212
const struct cache_entry *ce = the_repository->index->cache[i];
214213

215214
if (!match_pathspec(the_repository->index, pathspec, ce->name, ce_namelen(ce),
@@ -3398,7 +3397,6 @@ static void die_on_index_match(const char *path, int force)
33983397
die(_("index file corrupt"));
33993398

34003399
if (ps.nr) {
3401-
int i;
34023400
char *ps_matched = xcalloc(ps.nr, 1);
34033401

34043402
/* TODO: audit for interaction with sparse-index. */
@@ -3408,7 +3406,7 @@ static void die_on_index_match(const char *path, int force)
34083406
* Since there is only one pathspec, we just need to
34093407
* check ps_matched[0] to know if a cache entry matched.
34103408
*/
3411-
for (i = 0; i < the_repository->index->cache_nr; i++) {
3409+
for (size_t i = 0; i < the_repository->index->cache_nr; i++) {
34123410
ce_path_match(the_repository->index, the_repository->index->cache[i], &ps,
34133411
ps_matched);
34143412

0 commit comments

Comments
 (0)