Skip to content

Commit 3e7b36d

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11c821f commit 3e7b36d

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

reftable/basics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void put_be16(uint8_t *out, uint16_t i)
2727
out[1] = (uint8_t)(i & 0xff);
2828
}
2929

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)
3131
{
3232
size_t lo = 0;
3333
size_t hi = sz;

reftable/basics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void put_be16(uint8_t *out, uint16_t i);
2828
* Contrary to bsearch(3), this returns something useful if the argument is not
2929
* found.
3030
*/
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);
3232

3333
/*
3434
* Frees a NULL terminated array of malloced strings. The array itself is also

reftable/basics_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ static void test_binsearch(void)
3434

3535
int i = 0;
3636
for (i = 1; i < 11; i++) {
37-
int res;
37+
size_t res;
38+
3839
args.key = i;
3940
res = binsearch(sz, &binsearch_func, &args);
4041

4142
if (res < sz) {
4243
EXPECT(args.key < arr[res]);
43-
if (res > 0) {
44+
if (res > 0)
4445
EXPECT(args.key >= arr[res - 1]);
45-
}
4646
} else {
4747
EXPECT(args.key == 10 || args.key == 11);
4848
}

reftable/block.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
382382
};
383383
struct block_iter next = BLOCK_ITER_INIT;
384384
struct reftable_record rec;
385-
int err = 0, i;
385+
int err = 0;
386+
size_t i;
386387

387388
if (args.error) {
388389
err = REFTABLE_FORMAT_ERROR;

reftable/refname.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ static int modification_has_ref(struct modification *mod, const char *name)
3333
.names = mod->add,
3434
.want = name,
3535
};
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))
3838
return 0;
39-
}
4039
}
4140

4241
if (mod->del_len > 0) {
4342
struct find_arg arg = {
4443
.names = mod->del,
4544
.want = name,
4645
};
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))
4948
return 1;
50-
}
5149
}
5250

5351
err = reftable_table_read_ref(&mod->tab, name, &ref);
@@ -77,7 +75,7 @@ static int modification_has_ref_with_prefix(struct modification *mod,
7775
.names = mod->add,
7876
.want = prefix,
7977
};
80-
int idx = binsearch(mod->add_len, find_name, &arg);
78+
size_t idx = binsearch(mod->add_len, find_name, &arg);
8179
if (idx < mod->add_len &&
8280
!strncmp(prefix, mod->add[idx], strlen(prefix)))
8381
goto done;
@@ -96,11 +94,10 @@ static int modification_has_ref_with_prefix(struct modification *mod,
9694
.names = mod->del,
9795
.want = ref.refname,
9896
};
99-
int idx = binsearch(mod->del_len, find_name, &arg);
97+
size_t idx = binsearch(mod->del_len, find_name, &arg);
10098
if (idx < mod->del_len &&
101-
!strcmp(ref.refname, mod->del[idx])) {
99+
!strcmp(ref.refname, mod->del[idx]))
102100
continue;
103-
}
104101
}
105102

106103
if (strncmp(ref.refname, prefix, strlen(prefix))) {

0 commit comments

Comments
 (0)