Skip to content

Commit 5ac65f0

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 072e3aa commit 5ac65f0

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

reftable/basics.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,12 @@ int names_equal(const char **a, const char **b)
263263
return a[i] == b[i];
264264
}
265265

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)
267267
{
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++)
270270
if (a->buf[p] != b->buf[p])
271271
break;
272-
}
273-
274272
return p;
275273
}
276274

reftable/basics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
169169
#endif
170170

171171
/* 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);
173173

174174
int hash_size(enum reftable_hash id);
175175

reftable/record.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ int reftable_encode_key(int *restart, struct string_view dest,
144144
uint8_t extra)
145145
{
146146
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);
148148
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);
150150
if (n < 0)
151151
return -1;
152152
string_view_consume(&dest, n);

reftable/writer.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,18 +577,17 @@ static int writer_finish_section(struct reftable_writer *w)
577577

578578
struct common_prefix_arg {
579579
struct reftable_buf *last;
580-
int max;
580+
size_t max;
581581
};
582582

583583
static void update_common(void *void_arg, void *key)
584584
{
585585
struct common_prefix_arg *arg = void_arg;
586586
struct obj_index_tree_node *entry = key;
587587
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)
590590
arg->max = n;
591-
}
592591
}
593592
arg->last = &entry->hash;
594593
}

t/unit-tests/t-reftable-basics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
120120
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
121121
check(!reftable_buf_addstr(&a, cases[i].a));
122122
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);
124124
reftable_buf_reset(&a);
125125
reftable_buf_reset(&b);
126126
}

0 commit comments

Comments
 (0)