Skip to content

Commit 1b1942d

Browse files
committed
midx-write: reenable signed comparison errors
Remove the remaining signed comparison warnings in midx-write.c so that they can be enforced as errors in the future. After the previous change, the remaining errors are due to iterator variables named 'i'. The strategy here involves defining the variable within the for loop syntax to make sure we use the appropriate bitness for the loop sentinel. This matters in at least one method where the variable was compared to uint32_t in some loops and size_t in others. While adjusting these loops, there were some where the loop boundary was checking against a uint32_t value _plus one_. These were replaced with non-strict comparisons, but also the value is checked to not be UINT32_MAX. Since the value is the number of incremental multi-pack- indexes, this is not a meaningful restriction. The new die() is about defensive programming more than it being realistically possible. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 194db7c commit 1b1942d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

midx-write.c

Lines changed: 18 additions & 17 deletions
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 "abspath.h"
53
#include "config.h"
@@ -845,7 +843,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
845843
uint32_t commits_nr,
846844
unsigned flags)
847845
{
848-
int ret, i;
846+
int ret;
849847
uint16_t options = 0;
850848
struct bitmap_writer writer;
851849
struct pack_idx_entry **index;
@@ -873,7 +871,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
873871
* this order).
874872
*/
875873
ALLOC_ARRAY(index, pdata->nr_objects);
876-
for (i = 0; i < pdata->nr_objects; i++)
874+
for (uint32_t i = 0; i < pdata->nr_objects; i++)
877875
index[i] = &pdata->objects[i].idx;
878876

879877
bitmap_writer_init(&writer, ctx->repo, pdata,
@@ -894,7 +892,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
894892
* happens between bitmap_writer_build_type_index() and
895893
* bitmap_writer_finish().
896894
*/
897-
for (i = 0; i < pdata->nr_objects; i++)
895+
for (uint32_t i = 0; i < pdata->nr_objects; i++)
898896
index[ctx->pack_order[i]] = &pdata->objects[i].idx;
899897

900898
bitmap_writer_select_commits(&writer, commits, commits_nr);
@@ -1058,7 +1056,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10581056
{
10591057
struct strbuf midx_name = STRBUF_INIT;
10601058
unsigned char midx_hash[GIT_MAX_RAWSZ];
1061-
uint32_t i, start_pack;
1059+
uint32_t start_pack;
10621060
struct hashfile *f = NULL;
10631061
struct lock_file lk;
10641062
struct tempfile *incr;
@@ -1174,7 +1172,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11741172
if (preferred_pack_name) {
11751173
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
11761174

1177-
for (i = 0; i < ctx.nr; i++) {
1175+
for (size_t i = 0; i < ctx.nr; i++) {
11781176
if (!cmp_idx_or_pack_name(preferred_pack_name,
11791177
ctx.info[i].pack_name)) {
11801178
ctx.preferred_pack_idx = i;
@@ -1199,7 +1197,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11991197
* pack-order has all of its objects selected from that pack
12001198
* (and not another pack containing a duplicate)
12011199
*/
1202-
for (i = 1; i < ctx.nr; i++) {
1200+
for (size_t i = 1; i < ctx.nr; i++) {
12031201
struct packed_git *p = ctx.info[i].p;
12041202

12051203
if (!oldest->num_objects || p->mtime < oldest->mtime) {
@@ -1243,7 +1241,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12431241
compute_sorted_entries(&ctx, start_pack);
12441242

12451243
ctx.large_offsets_needed = 0;
1246-
for (i = 0; i < ctx.entries_nr; i++) {
1244+
for (size_t i = 0; i < ctx.entries_nr; i++) {
12471245
if (ctx.entries[i].offset > 0x7fffffff)
12481246
ctx.num_large_offsets++;
12491247
if (ctx.entries[i].offset > 0xffffffff)
@@ -1253,10 +1251,10 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12531251
QSORT(ctx.info, ctx.nr, pack_info_compare);
12541252

12551253
if (packs_to_drop && packs_to_drop->nr) {
1256-
int drop_index = 0;
1254+
size_t drop_index = 0;
12571255
int missing_drops = 0;
12581256

1259-
for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1257+
for (size_t i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
12601258
int cmp = strcmp(ctx.info[i].pack_name,
12611259
packs_to_drop->items[drop_index].string);
12621260

@@ -1287,7 +1285,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12871285
* pack_perm[old_id] = new_id
12881286
*/
12891287
ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1290-
for (i = 0; i < ctx.nr; i++) {
1288+
for (size_t i = 0; i < ctx.nr; i++) {
12911289
if (ctx.info[i].expired) {
12921290
dropped_packs++;
12931291
ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
@@ -1296,7 +1294,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12961294
}
12971295
}
12981296

1299-
for (i = 0; i < ctx.nr; i++) {
1297+
for (size_t i = 0; i < ctx.nr; i++) {
13001298
if (ctx.info[i].expired)
13011299
continue;
13021300
pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
@@ -1442,6 +1440,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14421440
* have been freed in the previous if block.
14431441
*/
14441442

1443+
if (ctx.num_multi_pack_indexes_before == UINT32_MAX)
1444+
die("too many multi-pack-indexes");
1445+
14451446
CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1);
14461447

14471448
if (ctx.incremental) {
@@ -1474,15 +1475,15 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14741475
keep_hashes[ctx.num_multi_pack_indexes_before] =
14751476
xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
14761477

1477-
for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1478+
for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
14781479
uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
14791480

14801481
keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
14811482
r->hash_algo));
14821483
m = m->base_midx;
14831484
}
14841485

1485-
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1486+
for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
14861487
fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
14871488
} else {
14881489
keep_hashes[ctx.num_multi_pack_indexes_before] =
@@ -1500,7 +1501,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
15001501
ctx.incremental);
15011502

15021503
cleanup:
1503-
for (i = 0; i < ctx.nr; i++) {
1504+
for (size_t i = 0; i < ctx.nr; i++) {
15041505
if (ctx.info[i].p) {
15051506
close_pack(ctx.info[i].p);
15061507
free(ctx.info[i].p);
@@ -1513,7 +1514,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
15131514
free(ctx.pack_perm);
15141515
free(ctx.pack_order);
15151516
if (keep_hashes) {
1516-
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1517+
for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
15171518
free((char *)keep_hashes[i]);
15181519
free(keep_hashes);
15191520
}

0 commit comments

Comments
 (0)