]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/writer: handle allocation failures in `reftable_new_writer()`
authorPatrick Steinhardt <ps@pks.im>
Wed, 2 Oct 2024 10:55:48 +0000 (12:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 2 Oct 2024 14:53:52 +0000 (07:53 -0700)
Handle allocation failures in `reftable_new_writer()`. Adapt the
function to return an error code to return such failures. While at it,
rename it to match our code style as we have to touch up every callsite
anyway.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/reftable-writer.h
reftable/stack.c
reftable/writer.c
t/unit-tests/lib-reftable.c

index 189b1f4144f22827e5fa293e02ecc4829ef2e8e4..43623dc7c304a1b10894f0df5f31b9293d3a6c26 100644 (file)
@@ -90,11 +90,13 @@ struct reftable_stats {
        int object_id_len;
 };
 
-/* reftable_new_writer creates a new writer */
-struct reftable_writer *
-reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
-                   int (*flush_func)(void *),
-                   void *writer_arg, const struct reftable_write_options *opts);
+struct reftable_writer;
+
+/* Create a new writer. */
+int reftable_writer_new(struct reftable_writer **out,
+                       ssize_t (*writer_func)(void *, const void *, size_t),
+                       int (*flush_func)(void *),
+                       void *writer_arg, const struct reftable_write_options *opts);
 
 /* Set the range of update indices for the records we will add. When writing a
    table into a stack, the min should be at least
index 498fae846d7d45f661b4bf726c7de3f91afa7156..ea21ca6e5f546fb1ed73a4bbd0762d3bcf74820b 100644 (file)
@@ -808,8 +808,11 @@ int reftable_addition_add(struct reftable_addition *add,
        }
        tab_fd = get_tempfile_fd(tab_file);
 
-       wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd,
-                                &add->stack->opts);
+       err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush,
+                                 &tab_fd, &add->stack->opts);
+       if (err < 0)
+               goto done;
+
        err = write_table(wr, arg);
        if (err < 0)
                goto done;
@@ -898,8 +901,11 @@ static int stack_compact_locked(struct reftable_stack *st,
                goto done;
        }
 
-       wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush,
-                                &tab_fd, &st->opts);
+       err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush,
+                                 &tab_fd, &st->opts);
+       if (err < 0)
+               goto done;
+
        err = stack_write_compact(st, wr, first, last, config);
        if (err < 0)
                goto done;
index ed61aaf59c59ab54cbfea20ac6ff0e7696ec2c3e..8ab2e916d3da72e1733dc3f6ccc1874b01d2ff29 100644 (file)
@@ -117,13 +117,17 @@ static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ)
        w->block_writer->restart_interval = w->opts.restart_interval;
 }
 
-struct reftable_writer *
-reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
-                   int (*flush_func)(void *),
-                   void *writer_arg, const struct reftable_write_options *_opts)
+int reftable_writer_new(struct reftable_writer **out,
+                       ssize_t (*writer_func)(void *, const void *, size_t),
+                       int (*flush_func)(void *),
+                       void *writer_arg, const struct reftable_write_options *_opts)
 {
-       struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp));
        struct reftable_write_options opts = {0};
+       struct reftable_writer *wp;
+
+       wp = reftable_calloc(1, sizeof(*wp));
+       if (!wp)
+               return REFTABLE_OUT_OF_MEMORY_ERROR;
 
        if (_opts)
                opts = *_opts;
@@ -134,13 +138,19 @@ reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
        strbuf_init(&wp->block_writer_data.last_key, 0);
        strbuf_init(&wp->last_key, 0);
        REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
+       if (!wp->block) {
+               reftable_free(wp);
+               return REFTABLE_OUT_OF_MEMORY_ERROR;
+       }
        wp->write = writer_func;
        wp->write_arg = writer_arg;
        wp->opts = opts;
        wp->flush = flush_func;
        writer_reinit_block_writer(wp, BLOCK_TYPE_REF);
 
-       return wp;
+       *out = wp;
+
+       return 0;
 }
 
 void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
index ab1fa44a2824d503a959d10a2570b0cf518b51c7..54c26c43e775a110d8693f77d018005898fc5166 100644 (file)
@@ -22,9 +22,11 @@ static int strbuf_writer_flush(void *arg UNUSED)
 struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf,
                                                 struct reftable_write_options *opts)
 {
-       return reftable_new_writer(&strbuf_writer_write,
-                                  &strbuf_writer_flush,
-                                  buf, opts);
+       struct reftable_writer *writer;
+       int ret = reftable_writer_new(&writer, &strbuf_writer_write, &strbuf_writer_flush,
+                                     buf, opts);
+       check(!ret);
+       return writer;
 }
 
 void t_reftable_write_to_buf(struct strbuf *buf,