git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 1/7] reftable/basics: fix return type of `binsearch()` to be `size_t`

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 2, 2024, 17:24 UTC
Message-ID
<baa07ef14425ff45296c2eb677c07b085bacafb3.1712078263.git.ps@pks.im>
In-Reply-To
<cover.1712078263.git.ps@pks.im>

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>
---
 reftable/basics.c      |  2 +-
 reftable/basics.h      |  2 +-
 reftable/basics_test.c |  6 +++---
 reftable/block.c       |  3 ++-
 reftable/refname.c     | 17 +++++++----------
 5 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/reftable/basics.c b/reftable/basics.c
index 0785aff941..2c5f34b39e 100644
--- a/reftable/basics.c
+++ b/reftable/basics.c
@@ -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;
diff --git a/reftable/basics.h b/reftable/basics.h
index 91f3533efe..2672520e76 100644
--- a/reftable/basics.h
+++ b/reftable/basics.h
@@ -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
diff --git a/reftable/basics_test.c b/reftable/basics_test.c
index 1fcd229725..dc1c87c5df 100644
--- a/reftable/basics_test.c
+++ b/reftable/basics_test.c
@@ -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);
 		}
diff --git a/reftable/block.c b/reftable/block.c
index e2a2cee58d..422885bddb 100644
--- a/reftable/block.c
+++ b/reftable/block.c
@@ -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;
diff --git a/reftable/refname.c b/reftable/refname.c
index 7570e4acf9..64eba1b886 100644
--- a/reftable/refname.c
+++ b/reftable/refname.c
@@ -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))) {
-- 
2.44.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 30 of 45 in “reftable: improvements for the `binsearch()` mechanism”
  1. 0/7 reftable: improvements for the `binsearch()` mechanismPatrick Steinhardt, Mar 22, 2024
  2. 1/7 reftable/basics: fix return type of `binsearch()` to be `size_t`Patrick Steinhardt, Mar 22, 2024
  3. 2/7 reftable/basics: improve `binsearch()` testPatrick Steinhardt, Mar 22, 2024
  4. Justin ToblerMar 22, 2024
  5. Patrick SteinhardtMar 25, 2024
  6. 3/7 reftable/refname: refactor binary search over refnamesPatrick Steinhardt, Mar 22, 2024
  7. Justin ToblerMar 22, 2024
  8. Patrick SteinhardtMar 25, 2024
  9. 4/7 reftable/block: refactor binary search over restart pointsPatrick Steinhardt, Mar 22, 2024
  10. 5/7 reftable/block: fix error handling when searching restart pointsPatrick Steinhardt, Mar 22, 2024
  11. 6/7 reftable/record: extract function to decode key lengthsPatrick Steinhardt, Mar 22, 2024
  12. 7/7 reftable/block: avoid decoding keys when searching restart pointsPatrick Steinhardt, Mar 22, 2024
  13. 0/7 reftable: improvements for the `binsearch()` mechanismPatrick Steinhardt, Mar 25, 2024
  14. 1/7 reftable/basics: fix return type of `binsearch()` to be `size_t`Patrick Steinhardt, Mar 25, 2024
  15. 2/7 reftable/basics: improve `binsearch()` testPatrick Steinhardt, Mar 25, 2024
  16. 3/7 reftable/refname: refactor binary search over refnamesPatrick Steinhardt, Mar 25, 2024
  17. Justin ToblerApr 2, 2024
  18. Patrick SteinhardtApr 2, 2024
  19. 4/7 reftable/block: refactor binary search over restart pointsPatrick Steinhardt, Mar 25, 2024
  20. Justin ToblerApr 2, 2024
  21. Patrick SteinhardtApr 2, 2024
  22. Justin ToblerApr 2, 2024
  23. Patrick SteinhardtApr 3, 2024
  24. 5/7 reftable/block: fix error handling when searching restart pointsPatrick Steinhardt, Mar 25, 2024
  25. 6/7 reftable/record: extract function to decode key lengthsPatrick Steinhardt, Mar 25, 2024
  26. 7/7 reftable/block: avoid decoding keys when searching restart pointsPatrick Steinhardt, Mar 25, 2024
  27. Justin ToblerApr 2, 2024
  28. Patrick SteinhardtApr 2, 2024
  29. 0/7 reftable: improvements for the `binsearch()` mechanismPatrick Steinhardt, Apr 2, 2024
  30. 1/7 reftable/basics: fix return type of `binsearch()` to be `size_t`Patrick Steinhardt, Apr 2, 2024
  31. 2/7 reftable/basics: improve `binsearch()` testPatrick Steinhardt, Apr 2, 2024
  32. 3/7 reftable/refname: refactor binary search over refnamesPatrick Steinhardt, Apr 2, 2024
  33. 4/7 reftable/block: refactor binary search over restart pointsPatrick Steinhardt, Apr 2, 2024
  34. 5/7 reftable/block: fix error handling when searching restart pointsPatrick Steinhardt, Apr 2, 2024
  35. 6/7 reftable/record: extract function to decode key lengthsPatrick Steinhardt, Apr 2, 2024
  36. 7/7 reftable/block: avoid decoding keys when searching restart pointsPatrick Steinhardt, Apr 2, 2024
  37. Justin ToblerApr 2, 2024
  38. 0/7 reftable: improvements for the `binsearch()` mechanismPatrick Steinhardt, Apr 3, 2024
  39. 1/7 reftable/basics: fix return type of `binsearch()` to be `size_t`Patrick Steinhardt, Apr 3, 2024
  40. 2/7 reftable/basics: improve `binsearch()` testPatrick Steinhardt, Apr 3, 2024
  41. 3/7 reftable/refname: refactor binary search over refnamesPatrick Steinhardt, Apr 3, 2024
  42. 4/7 reftable/block: refactor binary search over restart pointsPatrick Steinhardt, Apr 3, 2024
  43. 5/7 reftable/block: fix error handling when searching restart pointsPatrick Steinhardt, Apr 3, 2024
  44. 6/7 reftable/record: extract function to decode key lengthsPatrick Steinhardt, Apr 3, 2024
  45. 7/7 reftable/block: avoid decoding keys when searching restart pointsPatrick Steinhardt, Apr 3, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.