Skip to content

Commit abe22e5

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 6c91e97 commit abe22e5

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);
@@ -1040,7 +1038,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10401038
{
10411039
struct strbuf midx_name = STRBUF_INIT;
10421040
unsigned char midx_hash[GIT_MAX_RAWSZ];
1043-
uint32_t i, start_pack;
1041+
uint32_t start_pack;
10441042
struct hashfile *f = NULL;
10451043
struct lock_file lk;
10461044
struct tempfile *incr;
@@ -1156,7 +1154,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11561154
if (preferred_pack_name) {
11571155
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
11581156

1159-
for (i = 0; i < ctx.nr; i++) {
1157+
for (size_t i = 0; i < ctx.nr; i++) {
11601158
if (!cmp_idx_or_pack_name(preferred_pack_name,
11611159
ctx.info[i].pack_name)) {
11621160
ctx.preferred_pack_idx = i;
@@ -1181,7 +1179,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11811179
* pack-order has all of its objects selected from that pack
11821180
* (and not another pack containing a duplicate)
11831181
*/
1184-
for (i = 1; i < ctx.nr; i++) {
1182+
for (size_t i = 1; i < ctx.nr; i++) {
11851183
struct packed_git *p = ctx.info[i].p;
11861184

11871185
if (!oldest->num_objects || p->mtime < oldest->mtime) {
@@ -1225,7 +1223,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12251223
compute_sorted_entries(&ctx, start_pack);
12261224

12271225
ctx.large_offsets_needed = 0;
1228-
for (i = 0; i < ctx.entries_nr; i++) {
1226+
for (size_t i = 0; i < ctx.entries_nr; i++) {
12291227
if (ctx.entries[i].offset > 0x7fffffff)
12301228
ctx.num_large_offsets++;
12311229
if (ctx.entries[i].offset > 0xffffffff)
@@ -1235,10 +1233,10 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12351233
QSORT(ctx.info, ctx.nr, pack_info_compare);
12361234

12371235
if (packs_to_drop && packs_to_drop->nr) {
1238-
int drop_index = 0;
1236+
size_t drop_index = 0;
12391237
int missing_drops = 0;
12401238

1241-
for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1239+
for (size_t i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
12421240
int cmp = strcmp(ctx.info[i].pack_name,
12431241
packs_to_drop->items[drop_index].string);
12441242

@@ -1269,7 +1267,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12691267
* pack_perm[old_id] = new_id
12701268
*/
12711269
ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1272-
for (i = 0; i < ctx.nr; i++) {
1270+
for (size_t i = 0; i < ctx.nr; i++) {
12731271
if (ctx.info[i].expired) {
12741272
dropped_packs++;
12751273
ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
@@ -1278,7 +1276,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12781276
}
12791277
}
12801278

1281-
for (i = 0; i < ctx.nr; i++) {
1279+
for (size_t i = 0; i < ctx.nr; i++) {
12821280
if (ctx.info[i].expired)
12831281
continue;
12841282
pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
@@ -1424,6 +1422,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14241422
* have been freed in the previous if block.
14251423
*/
14261424

1425+
if (ctx.num_multi_pack_indexes_before == UINT32_MAX)
1426+
die("too many multi-pack-indexes");
1427+
14271428
CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1);
14281429

14291430
if (ctx.incremental) {
@@ -1456,15 +1457,15 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14561457
keep_hashes[ctx.num_multi_pack_indexes_before] =
14571458
xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
14581459

1459-
for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1460+
for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
14601461
uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
14611462

14621463
keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
14631464
r->hash_algo));
14641465
m = m->base_midx;
14651466
}
14661467

1467-
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1468+
for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
14681469
fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
14691470
} else {
14701471
keep_hashes[ctx.num_multi_pack_indexes_before] =
@@ -1482,7 +1483,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14821483
ctx.incremental);
14831484

14841485
cleanup:
1485-
for (i = 0; i < ctx.nr; i++) {
1486+
for (size_t i = 0; i < ctx.nr; i++) {
14861487
if (ctx.info[i].p) {
14871488
close_pack(ctx.info[i].p);
14881489
free(ctx.info[i].p);
@@ -1495,7 +1496,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14951496
free(ctx.pack_perm);
14961497
free(ctx.pack_order);
14971498
if (keep_hashes) {
1498-
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1499+
for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
14991500
free((char *)keep_hashes[i]);
15001501
free(keep_hashes);
15011502
}

0 commit comments

Comments
 (0)