We're about to introduce our own `reftable_buf` type to replace
`strbuf`. One function we'll have to convert is `strbuf_addf()`, which
is used in a handful of places. This function uses `snprintf()`
internally, which makes porting it a bit more involved:
- It is not available on all platforms.
- Some platforms like Windows have broken implementations.
So by using `snprintf()` we'd also push the burden on downstream users
of the reftable library to make available a properly working version of
it.
Most callsites of `strbuf_addf()` are trivial to convert to not using
it. We do end up using `snprintf()` in our unit tests, but that isn't
much of a problem for downstream users of the reftable library.
While at it, remove a useless call to `strbuf_reset()` in
`t_reftable_stack_auto_compaction_with_locked_tables()`. We don't write
to the buffer before this and initialize it with `STRBUF_INIT`, so there
is no need to reset anything.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Patrick Steinhardt committedOct 17, 2024 at 06:53 UTC7fa7e14ebee9d82545bb4a8dcc1cac22ef51cfed
4 files changed+50-37
reftable/stack.c
+12-6
index 7e617c2591..d7bc1187df 100644--- a/reftable/stack.c+++ b/reftable/stack.c@@ -1387,12 +1387,18 @@ static int stack_compact_range(struct reftable_stack *st, * have just written. In case the compacted table became empty we * simply skip writing it. */- for (i = 0; i < first_to_replace; i++)- strbuf_addf(&tables_list_buf, "%s\n", names[i]);- if (!is_empty_table)- strbuf_addf(&tables_list_buf, "%s\n", new_table_name.buf);- for (i = last_to_replace + 1; names[i]; i++)- strbuf_addf(&tables_list_buf, "%s\n", names[i]);+ for (i = 0; i < first_to_replace; i++) {+ strbuf_addstr(&tables_list_buf, names[i]);+ strbuf_addstr(&tables_list_buf, "\n");+ }+ if (!is_empty_table) {+ strbuf_addstr(&tables_list_buf, new_table_name.buf);+ strbuf_addstr(&tables_list_buf, "\n");+ }+ for (i = last_to_replace + 1; names[i]; i++) {+ strbuf_addstr(&tables_list_buf, names[i]);+ strbuf_addstr(&tables_list_buf, "\n");+ } err = write_in_full(get_lock_file_fd(&tables_list_lock), tables_list_buf.buf, tables_list_buf.len);
t/unit-tests/t-reftable-block.c
+5-2
index d470060e8b..8077bbc5e7 100644--- a/t/unit-tests/t-reftable-block.c+++ b/t/unit-tests/t-reftable-block.c@@ -308,10 +308,13 @@ static void t_index_block_read_write(void) check(!ret); for (i = 0; i < N; i++) {- strbuf_init(&recs[i].u.idx.last_key, 9);+ char buf[128];++ snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);+ strbuf_init(&recs[i].u.idx.last_key, 9); recs[i].type = BLOCK_TYPE_INDEX;- strbuf_addf(&recs[i].u.idx.last_key, "branch%02"PRIuMAX, (uintmax_t)i);+ strbuf_addstr(&recs[i].u.idx.last_key, buf); recs[i].u.idx.offset = i; ret = block_writer_add(&bw, &recs[i]);