Skip to content

Commit

Permalink
global: trivial conversions to fix -Wsign-compare warnings
Browse files Browse the repository at this point in the history
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed Dec 6, 2024
1 parent 25435e4 commit 80c9e70
Show file tree
Hide file tree
Showing 55 changed files with 105 additions and 238 deletions.
9 changes: 2 additions & 7 deletions advice.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "git-compat-util.h"
#include "advice.h"
#include "config.h"
Expand Down Expand Up @@ -162,7 +160,6 @@ void advise_if_enabled(enum advice_type type, const char *advice, ...)
int git_default_advice_config(const char *var, const char *value)
{
const char *k, *slot_name;
int i;

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

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

void list_config_advices(struct string_list *list, const char *prefix)
{
int i;

for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
for (size_t i = 0; i < ARRAY_SIZE(advice_setting); i++)
list_config_item(list, prefix, advice_setting[i].key);
}

Expand Down
5 changes: 1 addition & 4 deletions base85.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "git-compat-util.h"
#include "base85.h"

Expand Down Expand Up @@ -31,10 +29,9 @@ static const char en85[] = {
static char de85[256];
static void prep_base85(void)
{
int i;
if (de85['Z'])
return;
for (i = 0; i < ARRAY_SIZE(en85); i++) {
for (size_t i = 0; i < ARRAY_SIZE(en85); i++) {
int ch = en85[i];
de85[ch] = i + 1;
}
Expand Down
10 changes: 4 additions & 6 deletions builtin/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* Copyright (C) 2006 Linus Torvalds
*/

#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "advice.h"
#include "config.h"
Expand Down Expand Up @@ -42,9 +40,9 @@ static int chmod_pathspec(struct repository *repo,
char flip,
int show_only)
{
int i, ret = 0;
int ret = 0;

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

Expand Down Expand Up @@ -72,9 +70,9 @@ static int renormalize_tracked_files(struct repository *repo,
const struct pathspec *pathspec,
int flags)
{
int i, retval = 0;
int retval = 0;

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

if (!include_sparse &&
Expand Down
1 change: 0 additions & 1 deletion builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "config.h"
Expand Down
4 changes: 2 additions & 2 deletions builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"

Expand Down Expand Up @@ -367,7 +366,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
char *lbase_dir = NULL, *rbase_dir = NULL;
size_t ldir_len, rdir_len, wtdir_len;
const char *workdir, *tmp;
int ret = 0, i;
int ret = 0;
size_t i;
FILE *fp = NULL;
struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
NULL);
Expand Down
5 changes: 2 additions & 3 deletions builtin/for-each-repo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "config.h"
Expand Down Expand Up @@ -38,7 +37,7 @@ int cmd_for_each_repo(int argc,
{
static const char *config_key = NULL;
int keep_going = 0;
int i, result = 0;
int result = 0;
const struct string_list *values;
int err;

Expand All @@ -63,7 +62,7 @@ int cmd_for_each_repo(int argc,
else if (err)
return 0;

for (i = 0; i < values->nr; i++) {
for (size_t i = 0; i < values->nr; i++) {
int ret = run_command_on_repo(values->items[i].string, argc, argv);
if (ret) {
if (!keep_going)
Expand Down
4 changes: 1 addition & 3 deletions builtin/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "config.h"
Expand Down Expand Up @@ -131,7 +130,6 @@ static void list_config_help(enum show_config_type type)
struct string_list keys = STRING_LIST_INIT_DUP;
struct string_list keys_uniq = STRING_LIST_INIT_DUP;
struct string_list_item *item;
int i;

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

string_list_sort(&keys);
for (i = 0; i < keys.nr; i++) {
for (size_t i = 0; i < keys.nr; i++) {
const char *var = keys.items[i].string;
const char *wildcard, *tag, *cut;
const char *dot = NULL;
Expand Down
3 changes: 1 addition & 2 deletions builtin/mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,14 @@ static int split_maildir(const char *maildir, const char *dir,
char *file = NULL;
FILE *f = NULL;
int ret = -1;
int i;
struct string_list list = STRING_LIST_INIT_DUP;

list.cmp = maildir_filename_cmp;

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

for (i = 0; i < list.nr; i++) {
for (size_t i = 0; i < list.nr; i++) {
char *name;

free(file);
Expand Down
6 changes: 2 additions & 4 deletions builtin/merge-tree.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "tree-walk.h"
Expand Down Expand Up @@ -499,10 +498,9 @@ static int real_merge(struct merge_tree_options *o,
if (!result.clean) {
struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
const char *last = NULL;
int i;

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

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

Expand Down
4 changes: 1 addition & 3 deletions builtin/pack-redundant.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "gettext.h"
Expand Down Expand Up @@ -391,7 +390,6 @@ static int cmp_remaining_objects(const void *a, const void *b)
static void sort_pack_list(struct pack_list **pl)
{
struct pack_list **ary, *p;
int i;
size_t n = pack_list_size(*pl);

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

/* link them back again */
for (i = 0; i < n - 1; i++)
for (size_t i = 0; i < n - 1; i++)
ary[i]->next = ary[i + 1];
ary[n - 1]->next = NULL;
*pl = ary[0];
Expand Down
4 changes: 1 addition & 3 deletions builtin/pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "advice.h"
Expand Down Expand Up @@ -943,11 +942,10 @@ static int get_can_ff(struct object_id *orig_head,
static int already_up_to_date(struct object_id *orig_head,
struct oid_array *merge_heads)
{
int i;
struct commit *ours;

ours = lookup_commit_reference(the_repository, orig_head);
for (i = 0; i < merge_heads->nr; i++) {
for (size_t i = 0; i < merge_heads->nr; i++) {
struct commit_list *list = NULL;
struct commit *theirs;
int ok;
Expand Down
5 changes: 2 additions & 3 deletions builtin/push.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "advice.h"
Expand Down Expand Up @@ -420,7 +419,7 @@ static int do_push(int flags,
const struct string_list *push_options,
struct remote *remote)
{
int i, errs;
int errs;
struct strvec *url;
struct refspec *push_refspec = &rs;

Expand All @@ -435,7 +434,7 @@ static int do_push(int flags,
}
errs = 0;
url = push_url_of_remote(remote);
for (i = 0; i < url->nr; i++) {
for (size_t i = 0; i < url->nr; i++) {
struct transport *transport =
transport_get(remote, url->v[i]);
if (flags & TRANSPORT_PUSH_OPTIONS)
Expand Down
9 changes: 4 additions & 5 deletions builtin/rerere.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "config.h"
Expand Down Expand Up @@ -57,7 +56,7 @@ int cmd_rerere(int argc,
struct repository *repo UNUSED)
{
struct string_list merge_rr = STRING_LIST_INIT_DUP;
int i, autoupdate = -1, flags = 0;
int autoupdate = -1, flags = 0;

struct option options[] = {
OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
Expand Down Expand Up @@ -100,11 +99,11 @@ int cmd_rerere(int argc,
if (setup_rerere(the_repository, &merge_rr,
flags | RERERE_READONLY) < 0)
return 0;
for (i = 0; i < merge_rr.nr; i++)
for (size_t i = 0; i < merge_rr.nr; i++)
printf("%s\n", merge_rr.items[i].string);
} else if (!strcmp(argv[0], "remaining")) {
rerere_remaining(the_repository, &merge_rr);
for (i = 0; i < merge_rr.nr; i++) {
for (size_t i = 0; i < merge_rr.nr; i++) {
if (merge_rr.items[i].util != RERERE_RESOLVED)
printf("%s\n", merge_rr.items[i].string);
else
Expand All @@ -116,7 +115,7 @@ int cmd_rerere(int argc,
if (setup_rerere(the_repository, &merge_rr,
flags | RERERE_READONLY) < 0)
return 0;
for (i = 0; i < merge_rr.nr; i++) {
for (size_t i = 0; i < merge_rr.nr; i++) {
const char *path = merge_rr.items[i].string;
const struct rerere_id *id = merge_rr.items[i].util;
if (diff_two(rerere_path(id, "preimage"), path, path, path))
Expand Down
7 changes: 2 additions & 5 deletions builtin/stash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "abspath.h"
Expand Down Expand Up @@ -875,9 +874,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
struct tree *tree[ARRAY_SIZE(oid)];
struct tree_desc tree_desc[ARRAY_SIZE(oid)];
struct unpack_trees_options unpack_tree_opt = { 0 };
int i;

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

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

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

Expand Down
8 changes: 3 additions & 5 deletions builtin/submodule--helper.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "builtin.h"
#include "abspath.h"
Expand Down Expand Up @@ -196,7 +195,7 @@ static int module_list_compute(const char **argv,
struct pathspec *pathspec,
struct module_list *list)
{
int i, result = 0;
int result = 0;
char *ps_matched = NULL;

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

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

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

if (ps.nr) {
int i;
char *ps_matched = xcalloc(ps.nr, 1);

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

Expand Down
Loading

0 comments on commit 80c9e70

Please sign in to comment.