reftable/iter: handle allocation failures when creating indexed table iter

Handle allocation failures in `new_indexed_table_ref_iter()`. While at
it, rename the function to match our coding style.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2024-10-02 12:56:14 +02:00 committed by Junio C Hamano
parent 5b67cc6477
commit cc6a9af5d7
3 changed files with 23 additions and 6 deletions

View file

@ -181,14 +181,20 @@ static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
}
}
int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest,
int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest,
struct reftable_reader *r, uint8_t *oid,
int oid_len, uint64_t *offsets, int offset_len)
{
struct indexed_table_ref_iter empty = INDEXED_TABLE_REF_ITER_INIT;
struct indexed_table_ref_iter *itr = reftable_calloc(1, sizeof(*itr));
struct indexed_table_ref_iter *itr;
int err = 0;
itr = reftable_calloc(1, sizeof(*itr));
if (!itr) {
err = REFTABLE_OUT_OF_MEMORY_ERROR;
goto out;
}
*itr = empty;
itr->r = r;
strbuf_add(&itr->oid, oid, oid_len);
@ -197,10 +203,16 @@ int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest,
itr->offset_len = offset_len;
err = indexed_table_ref_iter_next_block(itr);
if (err < 0)
goto out;
*dest = itr;
err = 0;
out:
if (err < 0) {
*dest = NULL;
reftable_free(itr);
} else {
*dest = itr;
}
return err;
}

View file

@ -82,7 +82,7 @@ void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it,
struct indexed_table_ref_iter *itr);
/* Takes ownership of `offsets` */
int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest,
int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest,
struct reftable_reader *r, uint8_t *oid,
int oid_len, uint64_t *offsets, int offset_len);

View file

@ -715,7 +715,7 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
goto done;
}
err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id),
err = indexed_table_ref_iter_new(&itr, r, oid, hash_size(r->hash_id),
got.u.obj.offsets,
got.u.obj.offset_len);
if (err < 0)
@ -740,6 +740,11 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
int err;
REFTABLE_ALLOC_ARRAY(ti, 1);
if (!ti) {
err = REFTABLE_OUT_OF_MEMORY_ERROR;
goto out;
}
table_iter_init(ti, r);
err = table_iter_seek_start(ti, BLOCK_TYPE_REF, 0);
if (err < 0)