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

[PATCH v6 07/18] reftable: stop using `BUG()` in trivial cases

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 18, 2025, 09:20 UTC
Message-ID
<20250218-pks-reftable-drop-git-compat-util-v6-7-8c1f39fb4c02@pks.im>
In-Reply-To
<20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im>

Stop using `BUG()` in the remaining trivial cases that we still have in the reftable library. Instead of aborting the program, we'll now bubble up a `REFTABLE_API_ERROR` to indicate misuse of the calling conventions.

Note that in both `reftable_reader_{inc,dec}ref()` we simply stop calling `BUG()` altogether. The only situation where the counter should be zero is when the structure has already been free'd anyway, so we would run into undefined behaviour regardless of whether we try to abort the program or not.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/iter.c   | 3 +--
 reftable/reader.c | 4 ----
 reftable/writer.c | 5 ++---
 3 files changed, 3 insertions(+), 9 deletions(-)
diff --git a/reftable/iter.c b/reftable/iter.c
index 86e801ca9fb..b2ffb09c16b 100644
--- a/reftable/iter.c
+++ b/reftable/iter.c
@@ -146,8 +146,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
 static int indexed_table_ref_iter_seek(void *p UNUSED,
 				       struct reftable_record *want UNUSED)
 {
-	BUG("seeking indexed table is not supported");
-	return -1;
+	return REFTABLE_API_ERROR;
 }
 
 static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
diff --git a/reftable/reader.c b/reftable/reader.c
index de6e6dd9322..36a5633eded 100644
--- a/reftable/reader.c
+++ b/reftable/reader.c
@@ -677,8 +677,6 @@ int reftable_reader_new(struct reftable_reader **out,
 
 void reftable_reader_incref(struct reftable_reader *r)
 {
-	if (!r->refcount)
-		BUG("cannot increment ref counter of dead reader");
 	r->refcount++;
 }
 
@@ -686,8 +684,6 @@ void reftable_reader_decref(struct reftable_reader *r)
 {
 	if (!r)
 		return;
-	if (!r->refcount)
-		BUG("cannot decrement ref counter of dead reader");
 	if (--r->refcount)
 		return;
 	block_source_close(&r->source);
diff --git a/reftable/writer.c b/reftable/writer.c
index f3ab1035d61..239573ade24 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -158,7 +158,7 @@ int reftable_writer_new(struct reftable_writer **out,
 		opts = *_opts;
 	options_set_defaults(&opts);
 	if (opts.block_size >= (1 << 24))
-		BUG("configured block size exceeds 16MB");
+		return REFTABLE_API_ERROR;
 
 	reftable_buf_init(&wp->block_writer_data.last_key);
 	reftable_buf_init(&wp->last_key);
@@ -302,8 +302,7 @@ static int writer_add_record(struct reftable_writer *w,
 	}
 
 	if (block_writer_type(w->block_writer) != reftable_record_type(rec))
-		BUG("record of type %d added to writer of type %d",
-		    reftable_record_type(rec), block_writer_type(w->block_writer));
+		return REFTABLE_API_ERROR;
 
 	/*
 	 * Try to add the record to the writer. If this succeeds then we're
-- 
2.48.1.666.gff9fcf71b7.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 23 in “reftable: stop using "git-compat-util.h"”
  1. 00/18 reftable: stop using "git-compat-util.h"Patrick Steinhardt, Feb 18, 2025
  2. 01/18 reftable/stack: stop using `read_in_full()`Patrick Steinhardt, Feb 18, 2025
  3. 02/18 reftable/stack: stop using `write_in_full()`Patrick Steinhardt, Feb 18, 2025
  4. 03/18 reftable/blocksource: stop using `xmmap()`Patrick Steinhardt, Feb 18, 2025
  5. 04/18 reftable/record: stop using `COPY_ARRAY()`Patrick Steinhardt, Feb 18, 2025
  6. 05/18 reftable/record: stop using `BUG()` in `reftable_record_init()`Patrick Steinhardt, Feb 18, 2025
  7. 06/18 reftable/record: don't `BUG()` in `reftable_record_cmp()`Patrick Steinhardt, Feb 18, 2025
  8. 08/18 reftable/basics: stop using `st_mult()` in array allocatorsPatrick Steinhardt, Feb 18, 2025
  9. 07/18 reftable: stop using `BUG()` in trivial casesPatrick Steinhardt, Feb 18, 2025
  10. 10/18 reftable/reader: stop using `ARRAY_SIZE()` macroPatrick Steinhardt, Feb 18, 2025
  11. 09/18 reftable/basics: provide wrappers for big endian conversionPatrick Steinhardt, Feb 18, 2025
  12. 11/18 reftable/system: introduce `reftable_rand()`Patrick Steinhardt, Feb 18, 2025
  13. 12/18 reftable/stack: stop using `sleep_millisec()`Patrick Steinhardt, Feb 18, 2025
  14. 13/18 reftable/basics: stop using `SWAP()` macroPatrick Steinhardt, Feb 18, 2025
  15. 14/18 reftable/basics: introduce `REFTABLE_UNUSED` annotationPatrick Steinhardt, Feb 18, 2025
  16. 15/18 compat/mingw: split out POSIX-related bitsPatrick Steinhardt, Feb 18, 2025
  17. 16/18 git-compat-util.h: split out POSIX-emulating bitsPatrick Steinhardt, Feb 18, 2025
  18. 17/18 reftable: decouple from Git codebase by pulling in "compat/posix.h"Patrick Steinhardt, Feb 18, 2025
  19. 18/18 Makefile: skip reftable library for CoccinellePatrick Steinhardt, Feb 18, 2025
  20. Junio C HamanoFeb 18, 2025
  21. Junio C HamanoMar 11, 2025
  22. Patrick SteinhardtMar 12, 2025
  23. Patrick SteinhardtMar 20, 2025

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.