reftable/stack: adapt `format_name()` to handle allocation failures

The `format_name()` function cannot pass any errors to the caller as it has a `void` return type. Adapt it and its callers such that we can handle errors and start handling allocation failures. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Patrick Steinhardt committed Oct 17, 2024 at 06:54 UTC e693ccf2c9427dfd5d93db723ac68f49f550ed38
1 file changed +19 -9
reftable/stack.c
+19 -9
@@ -623,14 +623,14 @@ int reftable_stack_add(struct reftable_stack *st,
623 return 0;
624 }
625
626 -static void format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
626 +static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
627 {
628 char buf[100];
629 uint32_t rnd = (uint32_t)git_rand();
630 snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
631 min, max, rnd);
632 reftable_buf_reset(dest);
633 - reftable_buf_addstr(dest, buf);
633 + return reftable_buf_addstr(dest, buf);
634 }
635
636 struct reftable_addition {
@@ -846,7 +846,10 @@ int reftable_addition_add(struct reftable_addition *add,
846 int tab_fd;
847
848 reftable_buf_reset(&next_name);
849 - format_name(&next_name, add->next_update_index, add->next_update_index);
849 +
850 + err = format_name(&next_name, add->next_update_index, add->next_update_index);
851 + if (err < 0)
852 + goto done;
853
854 stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
855 reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
@@ -893,7 +896,9 @@ int reftable_addition_add(struct reftable_addition *add,
896 goto done;
897 }
898
896 - format_name(&next_name, wr->min_update_index, wr->max_update_index);
899 + err = format_name(&next_name, wr->min_update_index, wr->max_update_index);
900 + if (err < 0)
901 + goto done;
902 reftable_buf_addstr(&next_name, ".ref");
903 stack_filename(&tab_file_name, add->stack, next_name.buf);
904
@@ -944,9 +949,11 @@ static int stack_compact_locked(struct reftable_stack *st,
949 struct tempfile *tab_file;
950 int tab_fd, err = 0;
951
947 - format_name(&next_name,
948 - reftable_reader_min_update_index(st->readers[first]),
949 - reftable_reader_max_update_index(st->readers[last]));
952 + err = format_name(&next_name, reftable_reader_min_update_index(st->readers[first]),
953 + reftable_reader_max_update_index(st->readers[last]));
954 + if (err < 0)
955 + goto done;
956 +
957 stack_filename(&tab_file_path, st, next_name.buf);
958 reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX");
959
@@ -1370,8 +1377,11 @@ static int stack_compact_range(struct reftable_stack *st,
1377 * it into place now.
1378 */
1379 if (!is_empty_table) {
1373 - format_name(&new_table_name, st->readers[first]->min_update_index,
1374 - st->readers[last]->max_update_index);
1380 + err = format_name(&new_table_name, st->readers[first]->min_update_index,
1381 + st->readers[last]->max_update_index);
1382 + if (err < 0)
1383 + goto done;
1384 +
1385 reftable_buf_addstr(&new_table_name, ".ref");
1386 stack_filename(&new_table_path, st, new_table_name.buf);
1387