git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 07/10] reftable/stack: adapt `format_name()` to handle allocation failures

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 14, 2024, 13:02 UTC
Message-ID
<1f08163009b87596188165863b89b62c5f521e00.1728910727.git.ps@pks.im>
In-Reply-To
<cover.1728910726.git.ps@pks.im>

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>
---
 reftable/stack.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c
index 6ba48ddce5d..e94eb3c4685 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -623,14 +623,14 @@ int reftable_stack_add(struct reftable_stack *st,
 	return 0;
 }
 
-static void format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
+static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
 {
 	char buf[100];
 	uint32_t rnd = (uint32_t)git_rand();
 	snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
 		 min, max, rnd);
 	reftable_buf_reset(dest);
-	reftable_buf_addstr(dest, buf);
+	return reftable_buf_addstr(dest, buf);
 }
 
 struct reftable_addition {
@@ -846,7 +846,10 @@ int reftable_addition_add(struct reftable_addition *add,
 	int tab_fd;
 
 	reftable_buf_reset(&next_name);
-	format_name(&next_name, add->next_update_index, add->next_update_index);
+
+	err = format_name(&next_name, add->next_update_index, add->next_update_index);
+	if (err < 0)
+		goto done;
 
 	stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
 	reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
@@ -893,7 +896,9 @@ int reftable_addition_add(struct reftable_addition *add,
 		goto done;
 	}
 
-	format_name(&next_name, wr->min_update_index, wr->max_update_index);
+	err = format_name(&next_name, wr->min_update_index, wr->max_update_index);
+	if (err < 0)
+		goto done;
 	reftable_buf_addstr(&next_name, ".ref");
 	stack_filename(&tab_file_name, add->stack, next_name.buf);
 
@@ -944,9 +949,11 @@ static int stack_compact_locked(struct reftable_stack *st,
 	struct tempfile *tab_file;
 	int tab_fd, err = 0;
 
-	format_name(&next_name,
-		    reftable_reader_min_update_index(st->readers[first]),
-		    reftable_reader_max_update_index(st->readers[last]));
+	err = format_name(&next_name, reftable_reader_min_update_index(st->readers[first]),
+			  reftable_reader_max_update_index(st->readers[last]));
+	if (err < 0)
+		goto done;
+
 	stack_filename(&tab_file_path, st, next_name.buf);
 	reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX");
 
@@ -1370,8 +1377,11 @@ static int stack_compact_range(struct reftable_stack *st,
 	 * it into place now.
 	 */
 	if (!is_empty_table) {
-		format_name(&new_table_name, st->readers[first]->min_update_index,
-			    st->readers[last]->max_update_index);
+		err = format_name(&new_table_name, st->readers[first]->min_update_index,
+				  st->readers[last]->max_update_index);
+		if (err < 0)
+			goto done;
+
 		reftable_buf_addstr(&new_table_name, ".ref");
 		stack_filename(&new_table_path, st, new_table_name.buf);
 
-- 
2.47.0.dirty
Previous: Patrick SteinhardtNext: Taylor Blau
Message 40 of 63 in “reftable: stop using `struct strbuf`”
  1. 00/10 reftable: stop using `struct strbuf`Patrick Steinhardt, Oct 11, 2024
  2. 01/10 reftable: stop using `strbuf_addbuf()`Patrick Steinhardt, Oct 11, 2024
  3. 02/10 reftable: stop using `strbuf_addf()`Patrick Steinhardt, Oct 11, 2024
  4. karthik nayakOct 11, 2024
  5. Patrick SteinhardtOct 14, 2024
  6. 03/10 reftable/basics: provide new `reftable_buf` interfacePatrick Steinhardt, Oct 11, 2024
  7. karthik nayakOct 11, 2024
  8. Patrick SteinhardtOct 14, 2024
  9. 04/10 reftable: convert from `strbuf` to `reftable_buf`Patrick Steinhardt, Oct 11, 2024
  10. karthik nayakOct 11, 2024
  11. Patrick SteinhardtOct 14, 2024
  12. 05/10 reftable/blocksource: adapt interface namePatrick Steinhardt, Oct 11, 2024
  13. 06/10 t/unit-tests: check for `reftable_buf` allocation errorsPatrick Steinhardt, Oct 11, 2024
  14. 07/10 reftable/stack: adapt `format_name()` to handle allocation failuresPatrick Steinhardt, Oct 11, 2024
  15. 08/10 reftable/record: adapt `reftable_record_key()` to handle allocation failuresPatrick Steinhardt, Oct 11, 2024
  16. 09/10 reftable/stack: adapt `stack_filename()` to handle allocation failuresPatrick Steinhardt, Oct 11, 2024
  17. 10/10 reftable: handle trivial `reftable_buf` errorsPatrick Steinhardt, Oct 11, 2024
  18. karthik nayakOct 11, 2024
  19. Patrick SteinhardtOct 14, 2024
  20. 00/10 reftable: stop using `struct strbuf`Patrick Steinhardt, Oct 14, 2024
  21. 01/10 reftable: stop using `strbuf_addbuf()`Patrick Steinhardt, Oct 14, 2024
  22. Taylor BlauOct 14, 2024
  23. 02/10 reftable: stop using `strbuf_addf()`Patrick Steinhardt, Oct 14, 2024
  24. Taylor BlauOct 14, 2024
  25. Patrick SteinhardtOct 15, 2024
  26. Taylor BlauOct 15, 2024
  27. 03/10 reftable/basics: provide new `reftable_buf` interfacePatrick Steinhardt, Oct 14, 2024
  28. Taylor BlauOct 14, 2024
  29. Patrick SteinhardtOct 15, 2024
  30. Eric SunshineOct 15, 2024
  31. Taylor BlauOct 15, 2024
  32. Patrick SteinhardtOct 16, 2024
  33. Taylor BlauOct 16, 2024
  34. Patrick SteinhardtOct 17, 2024
  35. Taylor BlauOct 17, 2024
  36. 04/10 reftable: convert from `strbuf` to `reftable_buf`Patrick Steinhardt, Oct 14, 2024
  37. Taylor BlauOct 14, 2024
  38. 05/10 reftable/blocksource: adapt interface namePatrick Steinhardt, Oct 14, 2024
  39. 06/10 t/unit-tests: check for `reftable_buf` allocation errorsPatrick Steinhardt, Oct 14, 2024
  40. 07/10 reftable/stack: adapt `format_name()` to handle allocation failuresPatrick Steinhardt, Oct 14, 2024
  41. Taylor BlauOct 14, 2024
  42. 08/10 reftable/record: adapt `reftable_record_key()` to handle allocation failuresPatrick Steinhardt, Oct 14, 2024
  43. 09/10 reftable/stack: adapt `stack_filename()` to handle allocation failuresPatrick Steinhardt, Oct 14, 2024
  44. 10/10 reftable: handle trivial `reftable_buf` errorsPatrick Steinhardt, Oct 14, 2024
  45. Taylor BlauOct 14, 2024
  46. Patrick SteinhardtOct 15, 2024
  47. shejialuoOct 15, 2024
  48. Patrick SteinhardtOct 15, 2024
  49. shejialuoOct 15, 2024
  50. 00/10 reftable: stop using `struct strbuf`Patrick Steinhardt, Oct 17, 2024
  51. 01/10 reftable: stop using `strbuf_addbuf()`Patrick Steinhardt, Oct 17, 2024
  52. 02/10 reftable: stop using `strbuf_addf()`Patrick Steinhardt, Oct 17, 2024
  53. 03/10 reftable/basics: provide new `reftable_buf` interfacePatrick Steinhardt, Oct 17, 2024
  54. 04/10 reftable: convert from `strbuf` to `reftable_buf`Patrick Steinhardt, Oct 17, 2024
  55. 05/10 reftable/blocksource: adapt interface namePatrick Steinhardt, Oct 17, 2024
  56. 06/10 t/unit-tests: check for `reftable_buf` allocation errorsPatrick Steinhardt, Oct 17, 2024
  57. 07/10 reftable/stack: adapt `format_name()` to handle allocation failuresPatrick Steinhardt, Oct 17, 2024
  58. 08/10 reftable/record: adapt `reftable_record_key()` to handle allocation failuresPatrick Steinhardt, Oct 17, 2024
  59. 09/10 reftable/stack: adapt `stack_filename()` to handle allocation failuresPatrick Steinhardt, Oct 17, 2024
  60. 10/10 reftable: handle trivial `reftable_buf` errorsPatrick Steinhardt, Oct 17, 2024
  61. Taylor BlauOct 17, 2024
  62. karthik nayakOct 18, 2024
  63. Taylor BlauOct 18, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.