reftable/writer: handle allocation failures in writer_index_hash()

Handle allocation errors in `writer_index_hash()`. Adjust its only
caller in `reftable_writer_add_ref()` accordingly.

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:55:45 +02:00 committed by Junio C Hamano
parent 31f5b972e0
commit b680af2dba

View file

@ -186,18 +186,22 @@ static int obj_index_tree_node_compare(const void *a, const void *b)
&((const struct obj_index_tree_node *)b)->hash);
}
static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash)
static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash)
{
uint64_t off = w->next;
struct obj_index_tree_node want = { .hash = *hash };
struct obj_index_tree_node *key;
struct tree_node *node;
struct tree_node *node = tree_search(&want, &w->obj_index_tree,
&obj_index_tree_node_compare, 0);
struct obj_index_tree_node *key = NULL;
node = tree_search(&want, &w->obj_index_tree,
&obj_index_tree_node_compare, 0);
if (!node) {
struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT;
key = reftable_malloc(sizeof(struct obj_index_tree_node));
key = reftable_malloc(sizeof(*key));
if (!key)
return REFTABLE_OUT_OF_MEMORY_ERROR;
*key = empty;
strbuf_reset(&key->hash);
@ -208,12 +212,15 @@ static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash)
key = node->key;
}
if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off) {
return;
}
if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off)
return 0;
REFTABLE_ALLOC_GROW(key->offsets, key->offset_len + 1, key->offset_cap);
if (!key->offsets)
return REFTABLE_OUT_OF_MEMORY_ERROR;
key->offsets[key->offset_len++] = off;
return 0;
}
static int writer_add_record(struct reftable_writer *w,
@ -284,11 +291,11 @@ int reftable_writer_add_ref(struct reftable_writer *w,
.ref = *ref
},
};
int err = 0;
struct strbuf buf = STRBUF_INIT;
int err;
if (!ref->refname)
return REFTABLE_API_ERROR;
if (ref->update_index < w->min_update_index ||
if (!ref->refname ||
ref->update_index < w->min_update_index ||
ref->update_index > w->max_update_index)
return REFTABLE_API_ERROR;
@ -296,24 +303,32 @@ int reftable_writer_add_ref(struct reftable_writer *w,
err = writer_add_record(w, &rec);
if (err < 0)
return err;
goto out;
if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) {
struct strbuf h = STRBUF_INIT;
strbuf_add(&h, (char *)reftable_ref_record_val1(ref),
strbuf_add(&buf, (char *)reftable_ref_record_val1(ref),
hash_size(w->opts.hash_id));
writer_index_hash(w, &h);
strbuf_release(&h);
err = writer_index_hash(w, &buf);
if (err < 0)
goto out;
}
if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) {
struct strbuf h = STRBUF_INIT;
strbuf_add(&h, reftable_ref_record_val2(ref),
strbuf_reset(&buf);
strbuf_add(&buf, reftable_ref_record_val2(ref),
hash_size(w->opts.hash_id));
writer_index_hash(w, &h);
strbuf_release(&h);
err = writer_index_hash(w, &buf);
if (err < 0)
goto out;
}
return 0;
err = 0;
out:
strbuf_release(&buf);
return err;
}
int reftable_writer_add_refs(struct reftable_writer *w,