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

[PATCH v3 5/7] reftable/block: fix error handling when searching restart points

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

When doing the binary search over restart points in a block we need to decode the record keys. This decoding step can result in an error when the block is corrupted, which we indicate to the caller of the binary search by setting `args.error = 1`. But the only caller that exists mishandles this because it in fact performs the error check before calling `binsearch()`.

Fix this bug by checking for errors at the right point in time. Furthermore, refactor `binsearch()` so that it aborts the search in case the callback function returns a negative value so that we don't needlessly continue to search the block.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/basics.c | 5 ++++-
 reftable/basics.h | 5 +++--
 reftable/block.c  | 9 ++++-----
 3 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/reftable/basics.c b/reftable/basics.c
index 2c5f34b39e..fea711db7e 100644
--- a/reftable/basics.c
+++ b/reftable/basics.c
@@ -39,8 +39,11 @@ size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
 	 */
 	while (hi - lo > 1) {
 		size_t mid = lo + (hi - lo) / 2;
+		int ret = f(mid, args);
+		if (ret < 0)
+			return sz;
 
-		if (f(mid, args))
+		if (ret > 0)
 			hi = mid;
 		else
 			lo = mid;
diff --git a/reftable/basics.h b/reftable/basics.h
index 2672520e76..523ecd5307 100644
--- a/reftable/basics.h
+++ b/reftable/basics.h
@@ -22,8 +22,9 @@ uint32_t get_be24(uint8_t *in);
 void put_be16(uint8_t *out, uint16_t i);
 
 /*
- * find smallest index i in [0, sz) at which f(i) is true, assuming
- * that f is ascending. Return sz if f(i) is false for all indices.
+ * find smallest index i in [0, sz) at which `f(i) > 0`, assuming that f is
+ * ascending. Return sz if `f(i) == 0` for all indices. The search is aborted
+ * and `sz` is returned in case `f(i) < 0`.
  *
  * Contrary to bsearch(3), this returns something useful if the argument is not
  * found.
diff --git a/reftable/block.c b/reftable/block.c
index 6cd4c053df..ca80a05e21 100644
--- a/reftable/block.c
+++ b/reftable/block.c
@@ -387,11 +387,6 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
 	int err = 0;
 	size_t i;
 
-	if (args.error) {
-		err = REFTABLE_FORMAT_ERROR;
-		goto done;
-	}
-
 	/*
 	 * Perform a binary search over the block's restart points, which
 	 * avoids doing a linear scan over the whole block. Like this, we
@@ -405,6 +400,10 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
 	 * too many record.
 	 */
 	i = binsearch(br->restart_count, &restart_needle_less, &args);
+	if (args.error) {
+		err = REFTABLE_FORMAT_ERROR;
+		goto done;
+	}
 
 	/*
 	 * Now there are multiple cases:
-- 
2.44.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 34 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.