reftable: stop using `BUG()` in trivial cases

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 18, 2025 at 10:20 UTC 445f9f4f35c663fb668425f8c8fe0a1d58e1d8c7
3 files changed +3 -9
reftable/iter.c
+1 -2
@@ -146,8 +146,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
146 static int indexed_table_ref_iter_seek(void *p UNUSED,
147 struct reftable_record *want UNUSED)
148 {
149 - BUG("seeking indexed table is not supported");
150 - return -1;
149 + return REFTABLE_API_ERROR;
150 }
151
152 static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
reftable/reader.c
-4
@@ -677,8 +677,6 @@ done:
677
678 void reftable_reader_incref(struct reftable_reader *r)
679 {
680 - if (!r->refcount)
681 - BUG("cannot increment ref counter of dead reader");
680 r->refcount++;
681 }
682
@@ -686,8 +684,6 @@ void reftable_reader_decref(struct reftable_reader *r)
684 {
685 if (!r)
686 return;
689 - if (!r->refcount)
690 - BUG("cannot decrement ref counter of dead reader");
687 if (--r->refcount)
688 return;
689 block_source_close(&r->source);
reftable/writer.c
+2 -3
@@ -158,7 +158,7 @@ int reftable_writer_new(struct reftable_writer **out,
158 opts = *_opts;
159 options_set_defaults(&opts);
160 if (opts.block_size >= (1 << 24))
161 - BUG("configured block size exceeds 16MB");
161 + return REFTABLE_API_ERROR;
162
163 reftable_buf_init(&wp->block_writer_data.last_key);
164 reftable_buf_init(&wp->last_key);
@@ -302,8 +302,7 @@ static int writer_add_record(struct reftable_writer *w,
302 }
303
304 if (block_writer_type(w->block_writer) != reftable_record_type(rec))
305 - BUG("record of type %d added to writer of type %d",
306 - reftable_record_type(rec), block_writer_type(w->block_writer));
305 + return REFTABLE_API_ERROR;
306
307 /*
308 * Try to add the record to the writer. If this succeeds then we're