reftable/basics: adjust `common_prefix_size()` to return `size_t`
The `common_prefix_size()` function computes the length of the common prefix between two buffers. As such its return value will always be an unsigned integer, as the length cannot be negative. Regardless of that, the function returns a signed integer, which is nonsensical and causes a couple of -Wsign-compare warnings all over the place. Adjust the function to return a `size_t` instead. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jan 20, 2025 at 17:17 UTC
5ac65f0d6b867ff031fda03779c2f2613f022b10
5 files changed
+10
-13
reftable/basics.c
+3
-5
@@ -263,14 +263,12 @@ int names_equal(const char **a, const char **b)
263
return a[i] == b[i];
264
}
265
266
-int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
266
+size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
267
{
268
- int p = 0;
269
- for (; p < a->len && p < b->len; p++) {
268
+ size_t p = 0;
269
+ for (; p < a->len && p < b->len; p++)
270
if (a->buf[p] != b->buf[p])
271
break;
272
- }
273
-
272
return p;
273
}
274
reftable/basics.h
+1
-1
@@ -169,7 +169,7 @@ static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
169
#endif
170
171
/* Find the longest shared prefix size of `a` and `b` */
172
-int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
172
+size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
173
174
int hash_size(enum reftable_hash id);
175
reftable/record.c
+2
-2
@@ -144,9 +144,9 @@ int reftable_encode_key(int *restart, struct string_view dest,
144
uint8_t extra)
145
{
146
struct string_view start = dest;
147
- int prefix_len = common_prefix_size(&prev_key, &key);
147
+ size_t prefix_len = common_prefix_size(&prev_key, &key);
148
uint64_t suffix_len = key.len - prefix_len;
149
- int n = put_var_int(&dest, (uint64_t)prefix_len);
149
+ int n = put_var_int(&dest, prefix_len);
150
if (n < 0)
151
return -1;
152
string_view_consume(&dest, n);
reftable/writer.c
+3
-4
@@ -577,7 +577,7 @@ static int writer_finish_section(struct reftable_writer *w)
577
578
struct common_prefix_arg {
579
struct reftable_buf *last;
580
- int max;
580
+ size_t max;
581
};
582
583
static void update_common(void *void_arg, void *key)
@@ -585,10 +585,9 @@ static void update_common(void *void_arg, void *key)
585
struct common_prefix_arg *arg = void_arg;
586
struct obj_index_tree_node *entry = key;
587
if (arg->last) {
588
- int n = common_prefix_size(&entry->hash, arg->last);
589
- if (n > arg->max) {
588
+ size_t n = common_prefix_size(&entry->hash, arg->last);
589
+ if (n > arg->max)
590
arg->max = n;
591
- }
591
}
592
arg->last = &entry->hash;
593
}
t/unit-tests/t-reftable-basics.c
+1
-1
@@ -120,7 +120,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
120
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
121
check(!reftable_buf_addstr(&a, cases[i].a));
122
check(!reftable_buf_addstr(&b, cases[i].b));
123
- check_int(common_prefix_size(&a, &b), ==, cases[i].want);
123
+ check_uint(common_prefix_size(&a, &b), ==, cases[i].want);
124
reftable_buf_reset(&a);
125
reftable_buf_reset(&b);
126
}