Skip to content

Commit 1c7ca23

Browse files
pks-tgitster
authored andcommitted
commit-graph: fix sign comparison warnings
The "commit-graph.c" file has a bunch of sign comparison warnings: - There are a bunch of variables that are declared as signed integers even though they are used to count entities, like for example `num_commit_graphs_before` and `num_commit_graphs_after`. - There are several cases where we use signed loop variables to iterate through an unsigned entity count. - In `write_graph_chunk_base_1()` we count how many chunks we have written in total. But while the value represents a positive quantity, we still return a signed integer that we then later compare with unsigned values. - The Bloom settings hash version is being assigned `-1` even though it's an unsigned value. This is used to indicate an unspecified value and relies on 1's complement. Fix all of these cases by either using the proper variable type or by adding casts as required. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 285b2c9 commit 1c7ca23

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

commit-graph.c

Lines changed: 27 additions & 27 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 "config.h"
@@ -570,7 +569,7 @@ static void validate_mixed_bloom_settings(struct commit_graph *g)
570569
static int add_graph_to_chain(struct commit_graph *g,
571570
struct commit_graph *chain,
572571
struct object_id *oids,
573-
int n)
572+
size_t n)
574573
{
575574
struct commit_graph *cur_g = chain;
576575

@@ -623,7 +622,7 @@ int open_commit_graph_chain(const char *chain_file,
623622
close(*fd);
624623
return 0;
625624
}
626-
if (st->st_size < the_hash_algo->hexsz) {
625+
if (st->st_size < (ssize_t) the_hash_algo->hexsz) {
627626
close(*fd);
628627
if (!st->st_size) {
629628
/* treat empty files the same as missing */
@@ -644,15 +643,16 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
644643
struct commit_graph *graph_chain = NULL;
645644
struct strbuf line = STRBUF_INIT;
646645
struct object_id *oids;
647-
int i = 0, valid = 1, count;
646+
int valid = 1;
648647
FILE *fp = xfdopen(fd, "r");
648+
size_t count;
649649

650650
count = st->st_size / (the_hash_algo->hexsz + 1);
651651
CALLOC_ARRAY(oids, count);
652652

653653
odb_prepare_alternates(r->objects);
654654

655-
for (i = 0; i < count; i++) {
655+
for (size_t i = 0; i < count; i++) {
656656
struct odb_source *source;
657657

658658
if (strbuf_getline_lf(&line, fp) == EOF)
@@ -1146,12 +1146,12 @@ struct write_commit_graph_context {
11461146
int num_generation_data_overflows;
11471147
unsigned long approx_nr_objects;
11481148
struct progress *progress;
1149-
int progress_done;
1149+
uint64_t progress_done;
11501150
uint64_t progress_cnt;
11511151

11521152
char *base_graph_name;
1153-
int num_commit_graphs_before;
1154-
int num_commit_graphs_after;
1153+
uint32_t num_commit_graphs_before;
1154+
uint32_t num_commit_graphs_after;
11551155
char **commit_graph_filenames_before;
11561156
char **commit_graph_filenames_after;
11571157
char **commit_graph_hash_after;
@@ -1182,7 +1182,7 @@ static int write_graph_chunk_fanout(struct hashfile *f,
11821182
void *data)
11831183
{
11841184
struct write_commit_graph_context *ctx = data;
1185-
int i, count = 0;
1185+
size_t i, count = 0;
11861186
struct commit **list = ctx->commits.list;
11871187

11881188
/*
@@ -1210,7 +1210,8 @@ static int write_graph_chunk_oids(struct hashfile *f,
12101210
{
12111211
struct write_commit_graph_context *ctx = data;
12121212
struct commit **list = ctx->commits.list;
1213-
int count;
1213+
size_t count;
1214+
12141215
for (count = 0; count < ctx->commits.nr; count++, list++) {
12151216
display_progress(ctx->progress, ++ctx->progress_cnt);
12161217
hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
@@ -1332,9 +1333,9 @@ static int write_graph_chunk_generation_data(struct hashfile *f,
13321333
void *data)
13331334
{
13341335
struct write_commit_graph_context *ctx = data;
1335-
int i, num_generation_data_overflows = 0;
1336+
int num_generation_data_overflows = 0;
13361337

1337-
for (i = 0; i < ctx->commits.nr; i++) {
1338+
for (size_t i = 0; i < ctx->commits.nr; i++) {
13381339
struct commit *c = ctx->commits.list[i];
13391340
timestamp_t offset;
13401341
repo_parse_commit(ctx->r, c);
@@ -1356,8 +1357,8 @@ static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
13561357
void *data)
13571358
{
13581359
struct write_commit_graph_context *ctx = data;
1359-
int i;
1360-
for (i = 0; i < ctx->commits.nr; i++) {
1360+
1361+
for (size_t i = 0; i < ctx->commits.nr; i++) {
13611362
struct commit *c = ctx->commits.list[i];
13621363
timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
13631364
display_progress(ctx->progress, ++ctx->progress_cnt);
@@ -1527,7 +1528,7 @@ static void add_missing_parents(struct write_commit_graph_context *ctx, struct c
15271528

15281529
static void close_reachable(struct write_commit_graph_context *ctx)
15291530
{
1530-
int i;
1531+
size_t i;
15311532
struct commit *commit;
15321533
enum commit_graph_split_flags flags = ctx->opts ?
15331534
ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
@@ -1621,10 +1622,9 @@ static void compute_reachable_generation_numbers(
16211622
struct compute_generation_info *info,
16221623
int generation_version)
16231624
{
1624-
int i;
16251625
struct commit_list *list = NULL;
16261626

1627-
for (i = 0; i < info->commits->nr; i++) {
1627+
for (size_t i = 0; i < info->commits->nr; i++) {
16281628
struct commit *c = info->commits->list[i];
16291629
timestamp_t gen;
16301630
repo_parse_commit(info->r, c);
@@ -1715,7 +1715,7 @@ static void set_generation_v2(struct commit *c, timestamp_t t,
17151715

17161716
static void compute_generation_numbers(struct write_commit_graph_context *ctx)
17171717
{
1718-
int i;
1718+
size_t i;
17191719
struct compute_generation_info info = {
17201720
.r = ctx->r,
17211721
.commits = &ctx->commits,
@@ -1794,10 +1794,10 @@ static void trace2_bloom_filter_write_statistics(struct write_commit_graph_conte
17941794

17951795
static void compute_bloom_filters(struct write_commit_graph_context *ctx)
17961796
{
1797-
int i;
1797+
size_t i;
17981798
struct progress *progress = NULL;
17991799
struct commit **sorted_commits;
1800-
int max_new_filters;
1800+
size_t max_new_filters;
18011801

18021802
init_bloom_filters();
18031803

@@ -1815,7 +1815,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
18151815
QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
18161816

18171817
max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1818-
ctx->opts->max_new_filters : ctx->commits.nr;
1818+
(size_t) ctx->opts->max_new_filters : ctx->commits.nr;
18191819

18201820
for (i = 0; i < ctx->commits.nr; i++) {
18211821
enum bloom_filter_computed computed = 0;
@@ -2018,10 +2018,10 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
20182018
stop_progress(&ctx->progress);
20192019
}
20202020

2021-
static int write_graph_chunk_base_1(struct hashfile *f,
2022-
struct commit_graph *g)
2021+
static size_t write_graph_chunk_base_1(struct hashfile *f,
2022+
struct commit_graph *g)
20232023
{
2024-
int num = 0;
2024+
size_t num = 0;
20252025

20262026
if (!g)
20272027
return 0;
@@ -2035,7 +2035,7 @@ static int write_graph_chunk_base(struct hashfile *f,
20352035
void *data)
20362036
{
20372037
struct write_commit_graph_context *ctx = data;
2038-
int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2038+
size_t num = write_graph_chunk_base_1(f, ctx->new_base_graph);
20392039

20402040
if (num != ctx->num_commit_graphs_after - 1) {
20412041
error(_("failed to write correct number of base graph ids"));
@@ -2481,7 +2481,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
24812481
if (stat(path.buf, &st) < 0)
24822482
continue;
24832483

2484-
if (st.st_mtime > expire_time)
2484+
if ((unsigned) st.st_mtime > expire_time)
24852485
continue;
24862486
if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
24872487
continue;
@@ -2577,7 +2577,7 @@ int write_commit_graph(struct odb_source *source,
25772577
ctx.changed_paths = 1;
25782578

25792579
/* don't propagate the hash_version unless unspecified */
2580-
if (bloom_settings.hash_version == -1)
2580+
if (bloom_settings.hash_version == (unsigned) -1)
25812581
bloom_settings.hash_version = g->bloom_filter_settings->hash_version;
25822582
bloom_settings.bits_per_entry = g->bloom_filter_settings->bits_per_entry;
25832583
bloom_settings.num_hashes = g->bloom_filter_settings->num_hashes;

0 commit comments

Comments
 (0)