Re: [PATCH v3 09/22] reftable/writer: handle allocation failures in `reftable_new_writer()`
- From
René Scharfe <l.s.r@web.de>
- Date
- Sep 30, 2024, 17:40 UTC
- Message-ID
- <64253c71-915d-4862-8fa2-555ca2fc63cc@web.de>
- In-Reply-To
- <9edd1d84cdbd53d966ff5cfe9b75281dd5966b07.1727680272.git.ps@pks.im>
Am 30.09.24 um 10:08 schrieb Patrick Steinhardt:
Show 33 quoted lines
> diff --git a/reftable/writer.c b/reftable/writer.c
> index ed61aaf59c..54ec822e1c 100644
> --- a/reftable/writer.c
> +++ b/reftable/writer.c
> @@ -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) {
> + free(wp);Better use reftable_free() to free it, since you use reftable_calloc() to allocate it above.
Perhaps ban free(3), strdup(3) etc. at the end of reftable/basics.h, banned.h style?
René