reftable/basics: fix return type of `binsearch()` to be `size_t`

The `binsearch()` function can be used to find the first element for which a callback functions returns a truish value. But while the array size is of type `size_t`, the function in fact returns an `int` that is supposed to index into that array. Fix the function signature to return a `size_t`. This conversion does not change any semantics given that the function would only ever return a value in the range `[0, sz]` anyway. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 3, 2024 at 08:03 UTC 3e7b36d129a5972e20cab4e55c43a8628b4d2c9c
5 files changed +14 -16
reftable/basics.c
+1 -1
@@ -27,7 +27,7 @@ void put_be16(uint8_t *out, uint16_t i)
27 out[1] = (uint8_t)(i & 0xff);
28 }
29
30 -int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
30 +size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
31 {
32 size_t lo = 0;
33 size_t hi = sz;
reftable/basics.h
+1 -1
@@ -28,7 +28,7 @@ void put_be16(uint8_t *out, uint16_t i);
28 * Contrary to bsearch(3), this returns something useful if the argument is not
29 * found.
30 */
31 -int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
31 +size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
32
33 /*
34 * Frees a NULL terminated array of malloced strings. The array itself is also
reftable/basics_test.c
+3 -3
@@ -34,15 +34,15 @@ static void test_binsearch(void)
34
35 int i = 0;
36 for (i = 1; i < 11; i++) {
37 - int res;
37 + size_t res;
38 +
39 args.key = i;
40 res = binsearch(sz, &binsearch_func, &args);
41
42 if (res < sz) {
43 EXPECT(args.key < arr[res]);
43 - if (res > 0) {
44 + if (res > 0)
45 EXPECT(args.key >= arr[res - 1]);
45 - }
46 } else {
47 EXPECT(args.key == 10 || args.key == 11);
48 }
reftable/block.c
+2 -1
@@ -382,7 +382,8 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
382 };
383 struct block_iter next = BLOCK_ITER_INIT;
384 struct reftable_record rec;
385 - int err = 0, i;
385 + int err = 0;
386 + size_t i;
387
388 if (args.error) {
389 err = REFTABLE_FORMAT_ERROR;
reftable/refname.c
+7 -10
@@ -33,10 +33,9 @@ static int modification_has_ref(struct modification *mod, const char *name)
33 .names = mod->add,
34 .want = name,
35 };
36 - int idx = binsearch(mod->add_len, find_name, &arg);
37 - if (idx < mod->add_len && !strcmp(mod->add[idx], name)) {
36 + size_t idx = binsearch(mod->add_len, find_name, &arg);
37 + if (idx < mod->add_len && !strcmp(mod->add[idx], name))
38 return 0;
39 - }
39 }
40
41 if (mod->del_len > 0) {
@@ -44,10 +43,9 @@ static int modification_has_ref(struct modification *mod, const char *name)
43 .names = mod->del,
44 .want = name,
45 };
47 - int idx = binsearch(mod->del_len, find_name, &arg);
48 - if (idx < mod->del_len && !strcmp(mod->del[idx], name)) {
46 + size_t idx = binsearch(mod->del_len, find_name, &arg);
47 + if (idx < mod->del_len && !strcmp(mod->del[idx], name))
48 return 1;
50 - }
49 }
50
51 err = reftable_table_read_ref(&mod->tab, name, &ref);
@@ -77,7 +75,7 @@ static int modification_has_ref_with_prefix(struct modification *mod,
75 .names = mod->add,
76 .want = prefix,
77 };
80 - int idx = binsearch(mod->add_len, find_name, &arg);
78 + size_t idx = binsearch(mod->add_len, find_name, &arg);
79 if (idx < mod->add_len &&
80 !strncmp(prefix, mod->add[idx], strlen(prefix)))
81 goto done;
@@ -96,11 +94,10 @@ static int modification_has_ref_with_prefix(struct modification *mod,
94 .names = mod->del,
95 .want = ref.refname,
96 };
99 - int idx = binsearch(mod->del_len, find_name, &arg);
97 + size_t idx = binsearch(mod->del_len, find_name, &arg);
98 if (idx < mod->del_len &&
101 - !strcmp(ref.refname, mod->del[idx])) {
99 + !strcmp(ref.refname, mod->del[idx]))
100 continue;
103 - }
101 }
102
103 if (strncmp(ref.refname, prefix, strlen(prefix))) {