Skip to content

Commit 9d9fac0

Browse files
pks-tgitster
authored andcommitted
reftable/record: stop using BUG() in reftable_record_init()
We're aborting the program via `BUG()` in case `reftable_record_init()` was invoked with an unknown record type. This is bad because we may now die in library code, and because it makes us depend on the Git codebase. Refactor the code such that `reftable_record_init()` can return an error code to the caller. Adapt any callers accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a967966 commit 9d9fac0

File tree

7 files changed

+21
-13
lines changed

7 files changed

+21
-13
lines changed

reftable/block.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
508508
it->block_len = br->block_len;
509509
it->hash_size = br->hash_size;
510510

511-
reftable_record_init(&rec, block_reader_type(br));
511+
err = reftable_record_init(&rec, block_reader_type(br));
512+
if (err < 0)
513+
goto done;
512514

513515
/*
514516
* We're looking for the last entry less than the wanted key so that

reftable/merged.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ int merged_table_init_iter(struct reftable_merged_table *mt,
253253
}
254254

255255
for (size_t i = 0; i < mt->readers_len; i++) {
256-
reftable_record_init(&subiters[i].rec, typ);
256+
ret = reftable_record_init(&subiters[i].rec, typ);
257+
if (ret < 0)
258+
goto out;
259+
257260
ret = reader_init_iter(mt->readers[i], &subiters[i].iter, typ);
258261
if (ret < 0)
259262
goto out;

reftable/reader.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ static int table_iter_seek_linear(struct table_iter *ti,
360360
struct reftable_record rec;
361361
int err;
362362

363-
reftable_record_init(&rec, reftable_record_type(want));
363+
err = reftable_record_init(&rec, reftable_record_type(want));
364+
if (err < 0)
365+
goto done;
366+
364367
err = reftable_record_key(want, &want_key);
365368
if (err < 0)
366369
goto done;

reftable/record.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ reftable_record_vtable(struct reftable_record *rec)
13061306
abort();
13071307
}
13081308

1309-
void reftable_record_init(struct reftable_record *rec, uint8_t typ)
1309+
int reftable_record_init(struct reftable_record *rec, uint8_t typ)
13101310
{
13111311
memset(rec, 0, sizeof(*rec));
13121312
rec->type = typ;
@@ -1315,11 +1315,11 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ)
13151315
case BLOCK_TYPE_REF:
13161316
case BLOCK_TYPE_LOG:
13171317
case BLOCK_TYPE_OBJ:
1318-
return;
1318+
return 0;
13191319
case BLOCK_TYPE_INDEX:
13201320
reftable_buf_init(&rec->u.idx.last_key);
1321-
return;
1321+
return 0;
13221322
default:
1323-
BUG("unhandled record type");
1323+
return REFTABLE_API_ERROR;
13241324
}
13251325
}

reftable/record.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ struct reftable_record {
130130
} u;
131131
};
132132

133-
/* Initialize the reftable record for the given type */
134-
void reftable_record_init(struct reftable_record *rec, uint8_t typ);
133+
/* Initialize the reftable record for the given type. */
134+
int reftable_record_init(struct reftable_record *rec, uint8_t typ);
135135

136136
/* see struct record_vtable */
137137
int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);

t/unit-tests/t-reftable-pq.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void t_pq_record(void)
3232
char *last = NULL;
3333

3434
for (i = 0; i < N; i++) {
35-
reftable_record_init(&recs[i], BLOCK_TYPE_REF);
35+
check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
3636
recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i);
3737
}
3838

@@ -72,7 +72,7 @@ static void t_pq_index(void)
7272
size_t N = ARRAY_SIZE(recs), i;
7373

7474
for (i = 0; i < N; i++) {
75-
reftable_record_init(&recs[i], BLOCK_TYPE_REF);
75+
check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
7676
recs[i].u.ref.refname = (char *) "refs/heads/master";
7777
}
7878

@@ -111,7 +111,7 @@ static void t_merged_iter_pqueue_top(void)
111111
size_t N = ARRAY_SIZE(recs), i;
112112

113113
for (i = 0; i < N; i++) {
114-
reftable_record_init(&recs[i], BLOCK_TYPE_REF);
114+
check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
115115
recs[i].u.ref.refname = (char *) "refs/heads/master";
116116
}
117117

t/unit-tests/t-reftable-record.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void t_copy(struct reftable_record *rec)
1717
uint8_t typ;
1818

1919
typ = reftable_record_type(rec);
20-
reftable_record_init(&copy, typ);
20+
check(!reftable_record_init(&copy, typ));
2121
reftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);
2222
/* do it twice to catch memory leaks */
2323
reftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);

0 commit comments

Comments
 (0)