]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/basics: fix return type of `binsearch()` to be `size_t`
authorPatrick Steinhardt <ps@pks.im>
Wed, 3 Apr 2024 06:03:56 +0000 (08:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Apr 2024 16:16:49 +0000 (09:16 -0700)
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>
reftable/basics.c
reftable/basics.h
reftable/basics_test.c
reftable/block.c
reftable/refname.c

index 0785aff941bdc2d84181651a48f35cfaaefaede9..2c5f34b39e61179b7c3e9512c106b2e675e97ed8 100644 (file)
@@ -27,7 +27,7 @@ void put_be16(uint8_t *out, uint16_t i)
        out[1] = (uint8_t)(i & 0xff);
 }
 
-int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
+size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
 {
        size_t lo = 0;
        size_t hi = sz;
index 91f3533efee501faf3e461f7206dbc7a964da98e..2672520e765aaedac0b7dd9c427199978763465a 100644 (file)
@@ -28,7 +28,7 @@ void put_be16(uint8_t *out, uint16_t i);
  * Contrary to bsearch(3), this returns something useful if the argument is not
  * found.
  */
-int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
+size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
 
 /*
  * Frees a NULL terminated array of malloced strings. The array itself is also
index 1fcd2297256760c9c49c3844a23e73a1e91d0c75..dc1c87c5dfd36d6612e75d5b0d42c24bb472fa43 100644 (file)
@@ -34,15 +34,15 @@ static void test_binsearch(void)
 
        int i = 0;
        for (i = 1; i < 11; i++) {
-               int res;
+               size_t res;
+
                args.key = i;
                res = binsearch(sz, &binsearch_func, &args);
 
                if (res < sz) {
                        EXPECT(args.key < arr[res]);
-                       if (res > 0) {
+                       if (res > 0)
                                EXPECT(args.key >= arr[res - 1]);
-                       }
                } else {
                        EXPECT(args.key == 10 || args.key == 11);
                }
index e2a2cee58d2a35c6183e7083d3320d2520a397f4..422885bddb74daba6b73417b01a659c9710e2577 100644 (file)
@@ -382,7 +382,8 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
        };
        struct block_iter next = BLOCK_ITER_INIT;
        struct reftable_record rec;
-       int err = 0, i;
+       int err = 0;
+       size_t i;
 
        if (args.error) {
                err = REFTABLE_FORMAT_ERROR;
index 7570e4acf9eec740057a4c6ba8a615878f48799f..64eba1b8860750e33758f93da18e5d183e88c5b0 100644 (file)
@@ -33,10 +33,9 @@ static int modification_has_ref(struct modification *mod, const char *name)
                        .names = mod->add,
                        .want = name,
                };
-               int idx = binsearch(mod->add_len, find_name, &arg);
-               if (idx < mod->add_len && !strcmp(mod->add[idx], name)) {
+               size_t idx = binsearch(mod->add_len, find_name, &arg);
+               if (idx < mod->add_len && !strcmp(mod->add[idx], name))
                        return 0;
-               }
        }
 
        if (mod->del_len > 0) {
@@ -44,10 +43,9 @@ static int modification_has_ref(struct modification *mod, const char *name)
                        .names = mod->del,
                        .want = name,
                };
-               int idx = binsearch(mod->del_len, find_name, &arg);
-               if (idx < mod->del_len && !strcmp(mod->del[idx], name)) {
+               size_t idx = binsearch(mod->del_len, find_name, &arg);
+               if (idx < mod->del_len && !strcmp(mod->del[idx], name))
                        return 1;
-               }
        }
 
        err = reftable_table_read_ref(&mod->tab, name, &ref);
@@ -77,7 +75,7 @@ static int modification_has_ref_with_prefix(struct modification *mod,
                        .names = mod->add,
                        .want = prefix,
                };
-               int idx = binsearch(mod->add_len, find_name, &arg);
+               size_t idx = binsearch(mod->add_len, find_name, &arg);
                if (idx < mod->add_len &&
                    !strncmp(prefix, mod->add[idx], strlen(prefix)))
                        goto done;
@@ -96,11 +94,10 @@ static int modification_has_ref_with_prefix(struct modification *mod,
                                .names = mod->del,
                                .want = ref.refname,
                        };
-                       int idx = binsearch(mod->del_len, find_name, &arg);
+                       size_t idx = binsearch(mod->del_len, find_name, &arg);
                        if (idx < mod->del_len &&
-                           !strcmp(ref.refname, mod->del[idx])) {
+                           !strcmp(ref.refname, mod->del[idx]))
                                continue;
-                       }
                }
 
                if (strncmp(ref.refname, prefix, strlen(prefix))) {