diff mbox series

[v2,03/11] reftable/writer: drop static variable used to initialize strbuf

Message ID aa2903e3e5ac76f82252e12919a8a09178e267ee.1715336798.git.ps@pks.im (mailing list archive)
State Superseded
Headers show
Series reftable: expose write options as config | expand

Commit Message

Patrick Steinhardt May 10, 2024, 10:29 a.m. UTC
We have a static variable in the reftable writer code that is merely
used to initialize the `last_key` of the writer. Convert the code to
instead use `strbuf_init()` and drop the variable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/writer.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Junio C Hamano May 10, 2024, 9:19 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> We have a static variable in the reftable writer code that is merely
> used to initialize the `last_key` of the writer. Convert the code to
> instead use `strbuf_init()` and drop the variable.

Nice.  There is no guarantee that a structure assignment of a
just-initialized empty strbuf to another will stay to be a trouble
free operation, and using an explicit _init() call is the right
thing to do.
diff mbox series

Patch

diff --git a/reftable/writer.c b/reftable/writer.c
index ad2f2e6c65..7df6e53699 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -117,15 +117,12 @@  static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ)
 	w->block_writer->restart_interval = w->opts.restart_interval;
 }
 
-static struct strbuf reftable_empty_strbuf = STRBUF_INIT;
-
 struct reftable_writer *
 reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
 		    int (*flush_func)(void *),
 		    void *writer_arg, struct reftable_write_options opts)
 {
 	struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp));
-	strbuf_init(&wp->block_writer_data.last_key, 0);
 
 	options_set_defaults(&opts);
 	if (opts.block_size >= (1 << 24)) {
@@ -133,7 +130,8 @@  reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
 		abort();
 	}
 
-	wp->last_key = reftable_empty_strbuf;
+	strbuf_init(&wp->block_writer_data.last_key, 0);
+	strbuf_init(&wp->last_key, 0);
 	REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
 	wp->write = writer_func;
 	wp->write_arg = writer_arg;