bulk-checkin: fix sign compare warnings

In file bulk-checkin.c, three warnings are emitted by "-Wsign-compare", two of which are caused by trivial loop iterator type mismatches. For the third case, the type of `rsize` from ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf); can be changed to size_t as both options of the ternary expression are unsigned and the signedness of the variable isn't really needed anywhere. To prevent `read_result != rsize` making a clash, it is to be noted that `read_result` is checked not to hold negative values. Therefore casting the variable to size_t is a safe operation and enough to remove the sign-compare warning. Fix issues accordingly, and remove `DISABLE_SIGN_COMPARE_WARNINGS` to enable "-Wsign-compare" for the file. Signed-off-by: Tuomas Ahola <taahol@utu.fi> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tuomas Ahola committed Mar 24, 2025 at 23:47 UTC 133d065dd6af135d53bce590f52b885c70c9a09b
1 file changed +6 -10
bulk-checkin.c
+6 -10
@@ -3,7 +3,6 @@
3 */
4
5 #define USE_THE_REPOSITORY_VARIABLE
6 -#define DISABLE_SIGN_COMPARE_WARNINGS
6
7 #include "git-compat-util.h"
8 #include "bulk-checkin.h"
@@ -56,7 +55,6 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
55 {
56 unsigned char hash[GIT_MAX_RAWSZ];
57 struct strbuf packname = STRBUF_INIT;
59 - int i;
58
59 if (!state->f)
60 return;
@@ -82,7 +80,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
80 finish_tmp_packfile(&packname, state->pack_tmp_name,
81 state->written, state->nr_written,
82 &state->pack_idx_opts, hash);
85 - for (i = 0; i < state->nr_written; i++)
83 + for (uint32_t i = 0; i < state->nr_written; i++)
84 free(state->written[i]);
85
86 clear_exit:
@@ -131,14 +129,12 @@ static void flush_batch_fsync(void)
129
130 static int already_written(struct bulk_checkin_packfile *state, struct object_id *oid)
131 {
134 - int i;
135 -
132 /* The object may already exist in the repository */
133 if (repo_has_object_file(the_repository, oid))
134 return 1;
135
136 /* Might want to keep the list sorted */
141 - for (i = 0; i < state->nr_written; i++)
137 + for (uint32_t i = 0; i < state->nr_written; i++)
138 if (oideq(&state->written[i]->oid, oid))
139 return 1;
140
@@ -182,13 +178,13 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
178
179 while (status != Z_STREAM_END) {
180 if (size && !s.avail_in) {
185 - ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
181 + size_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
182 ssize_t read_result = read_in_full(fd, ibuf, rsize);
183 if (read_result < 0)
184 die_errno("failed to read from '%s'", path);
189 - if (read_result != rsize)
190 - die("failed to read %d bytes from '%s'",
191 - (int)rsize, path);
185 + if ((size_t)read_result != rsize)
186 + die("failed to read %u bytes from '%s'",
187 + (unsigned)rsize, path);
188 offset += rsize;
189 if (*already_hashed_to < offset) {
190 size_t hsize = offset - *already_hashed_to;