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 <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Derrick Stolee
2025-09-05 19:26:17 +00:00
committed by Junio C Hamano
parent 68383ac9d4
commit 1f2bc6be1d

View File

@@ -1,5 +1,3 @@
#define DISABLE_SIGN_COMPARE_WARNINGS
#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h" #include "abspath.h"
#include "config.h" #include "config.h"
@@ -845,7 +843,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
uint32_t commits_nr, uint32_t commits_nr,
unsigned flags) unsigned flags)
{ {
int ret, i; int ret;
uint16_t options = 0; uint16_t options = 0;
struct bitmap_writer writer; struct bitmap_writer writer;
struct pack_idx_entry **index; struct pack_idx_entry **index;
@@ -873,7 +871,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
* this order). * this order).
*/ */
ALLOC_ARRAY(index, pdata->nr_objects); ALLOC_ARRAY(index, pdata->nr_objects);
for (i = 0; i < pdata->nr_objects; i++) for (uint32_t i = 0; i < pdata->nr_objects; i++)
index[i] = &pdata->objects[i].idx; index[i] = &pdata->objects[i].idx;
bitmap_writer_init(&writer, ctx->repo, pdata, bitmap_writer_init(&writer, ctx->repo, pdata,
@@ -894,7 +892,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
* happens between bitmap_writer_build_type_index() and * happens between bitmap_writer_build_type_index() and
* bitmap_writer_finish(). * bitmap_writer_finish().
*/ */
for (i = 0; i < pdata->nr_objects; i++) for (uint32_t i = 0; i < pdata->nr_objects; i++)
index[ctx->pack_order[i]] = &pdata->objects[i].idx; index[ctx->pack_order[i]] = &pdata->objects[i].idx;
bitmap_writer_select_commits(&writer, commits, commits_nr); bitmap_writer_select_commits(&writer, commits, commits_nr);
@@ -1056,7 +1054,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
{ {
struct strbuf midx_name = STRBUF_INIT; struct strbuf midx_name = STRBUF_INIT;
unsigned char midx_hash[GIT_MAX_RAWSZ]; unsigned char midx_hash[GIT_MAX_RAWSZ];
uint32_t i, start_pack; uint32_t start_pack;
struct hashfile *f = NULL; struct hashfile *f = NULL;
struct lock_file lk; struct lock_file lk;
struct tempfile *incr; struct tempfile *incr;
@@ -1172,7 +1170,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
if (preferred_pack_name) { if (preferred_pack_name) {
ctx.preferred_pack_idx = NO_PREFERRED_PACK; ctx.preferred_pack_idx = NO_PREFERRED_PACK;
for (i = 0; i < ctx.nr; i++) { for (size_t i = 0; i < ctx.nr; i++) {
if (!cmp_idx_or_pack_name(preferred_pack_name, if (!cmp_idx_or_pack_name(preferred_pack_name,
ctx.info[i].pack_name)) { ctx.info[i].pack_name)) {
ctx.preferred_pack_idx = i; ctx.preferred_pack_idx = i;
@@ -1204,7 +1202,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
* pack-order has all of its objects selected from that pack * pack-order has all of its objects selected from that pack
* (and not another pack containing a duplicate) * (and not another pack containing a duplicate)
*/ */
for (i = 1; i < ctx.nr; i++) { for (size_t i = 1; i < ctx.nr; i++) {
struct packed_git *p = ctx.info[i].p; struct packed_git *p = ctx.info[i].p;
if (!oldest->num_objects || p->mtime < oldest->mtime) { if (!oldest->num_objects || p->mtime < oldest->mtime) {
@@ -1249,7 +1247,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
compute_sorted_entries(&ctx, start_pack); compute_sorted_entries(&ctx, start_pack);
ctx.large_offsets_needed = 0; ctx.large_offsets_needed = 0;
for (i = 0; i < ctx.entries_nr; i++) { for (size_t i = 0; i < ctx.entries_nr; i++) {
if (ctx.entries[i].offset > 0x7fffffff) if (ctx.entries[i].offset > 0x7fffffff)
ctx.num_large_offsets++; ctx.num_large_offsets++;
if (ctx.entries[i].offset > 0xffffffff) if (ctx.entries[i].offset > 0xffffffff)
@@ -1259,10 +1257,10 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
QSORT(ctx.info, ctx.nr, pack_info_compare); QSORT(ctx.info, ctx.nr, pack_info_compare);
if (packs_to_drop && packs_to_drop->nr) { if (packs_to_drop && packs_to_drop->nr) {
int drop_index = 0; size_t drop_index = 0;
int missing_drops = 0; int missing_drops = 0;
for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) { for (size_t i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
int cmp = strcmp(ctx.info[i].pack_name, int cmp = strcmp(ctx.info[i].pack_name,
packs_to_drop->items[drop_index].string); packs_to_drop->items[drop_index].string);
@@ -1293,7 +1291,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
* pack_perm[old_id] = new_id * pack_perm[old_id] = new_id
*/ */
ALLOC_ARRAY(ctx.pack_perm, ctx.nr); ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
for (i = 0; i < ctx.nr; i++) { for (size_t i = 0; i < ctx.nr; i++) {
if (ctx.info[i].expired) { if (ctx.info[i].expired) {
dropped_packs++; dropped_packs++;
ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED; ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
@@ -1302,7 +1300,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
} }
} }
for (i = 0; i < ctx.nr; i++) { for (size_t i = 0; i < ctx.nr; i++) {
if (ctx.info[i].expired) if (ctx.info[i].expired)
continue; continue;
pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1; pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
@@ -1448,6 +1446,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
* have been freed in the previous if block. * have been freed in the previous if block.
*/ */
if (ctx.num_multi_pack_indexes_before == UINT32_MAX)
die(_("too many multi-pack-indexes"));
CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1); CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1);
if (ctx.incremental) { if (ctx.incremental) {
@@ -1480,7 +1481,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
keep_hashes[ctx.num_multi_pack_indexes_before] = keep_hashes[ctx.num_multi_pack_indexes_before] =
xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo)); xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) { for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
uint32_t j = ctx.num_multi_pack_indexes_before - i - 1; uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m), keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
@@ -1488,7 +1489,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
m = m->base_midx; m = m->base_midx;
} }
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++) for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]); fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
} else { } else {
keep_hashes[ctx.num_multi_pack_indexes_before] = keep_hashes[ctx.num_multi_pack_indexes_before] =
@@ -1506,7 +1507,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
ctx.incremental); ctx.incremental);
cleanup: cleanup:
for (i = 0; i < ctx.nr; i++) { for (size_t i = 0; i < ctx.nr; i++) {
if (ctx.info[i].p) { if (ctx.info[i].p) {
close_pack(ctx.info[i].p); close_pack(ctx.info[i].p);
free(ctx.info[i].p); free(ctx.info[i].p);
@@ -1519,7 +1520,7 @@ cleanup:
free(ctx.pack_perm); free(ctx.pack_perm);
free(ctx.pack_order); free(ctx.pack_order);
if (keep_hashes) { if (keep_hashes) {
for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++) for (uint32_t i = 0; i <= ctx.num_multi_pack_indexes_before; i++)
free((char *)keep_hashes[i]); free((char *)keep_hashes[i]);
free(keep_hashes); free(keep_hashes);
} }