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

[PATCH v3 4/8] reftable/stack: fix compiler warning due to missing braces

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 12, 2025, 09:54 UTC
Message-ID
<20250812-pks-reftable-fixes-for-libgit2-v3-4-cf3b2267867e@pks.im>
In-Reply-To
<20250812-pks-reftable-fixes-for-libgit2-v3-0-cf3b2267867e@pks.im>

While perfectly legal, older compiler toolchains complain when zero-initializing structs that contain nested structs with `{0}`:

    /home/libgit2/source/deps/reftable/stack.c:862:35: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
            struct reftable_addition empty = REFTABLE_ADDITION_INIT;
                                             ^~~~~~~~~~~~~~~~~~~~~~
    /home/libgit2/source/deps/reftable/stack.c:707:33: note: expanded from macro 'REFTABLE_ADDITION_INIT'
    #define REFTABLE_ADDITION_INIT {0}
                                    ^

We had the discussion around whether or not we want to handle such bogus compiler errors in the past already [1]. Back then we basically decided that we do not care about such old-and-buggy compilers, so while we could fix the issue by using `{{0}}` instead this is not the preferred way to handle this in the Git codebase.

We have an easier fix though: we can just drop the macro altogether and handle initialization of the struct in `reftable_stack_addition_init()`. Callers are expected to call this function already, so this change even simplifies the calling convention.

[1]: https://lore.kernel.org/git/20220710081135.74964-1-sunshine@sunshineco.com/T/
Suggested-by: Carlo Arenas <carenas@gmail.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/stack.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c
index ed80710572..9db90cf4ed 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -664,8 +664,6 @@ struct reftable_addition {
 	uint64_t next_update_index;
 };
 
-#define REFTABLE_ADDITION_INIT {0}
-
 static void reftable_addition_close(struct reftable_addition *add)
 {
 	struct reftable_buf nm = REFTABLE_BUF_INIT;
@@ -693,6 +691,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
 	struct reftable_buf lock_file_name = REFTABLE_BUF_INIT;
 	int err;
 
+	memset(add, 0, sizeof(*add));
 	add->stack = st;
 
 	err = flock_acquire(&add->tables_list_lock, st->list_file,
@@ -739,8 +738,10 @@ static int stack_try_add(struct reftable_stack *st,
 					    void *arg),
 			 void *arg)
 {
-	struct reftable_addition add = REFTABLE_ADDITION_INIT;
-	int err = reftable_stack_init_addition(&add, st, 0);
+	struct reftable_addition add;
+	int err;
+
+	err = reftable_stack_init_addition(&add, st, 0);
 	if (err < 0)
 		goto done;
 
@@ -866,19 +867,18 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
 				struct reftable_stack *st,
 				unsigned int flags)
 {
-	int err = 0;
-	struct reftable_addition empty = REFTABLE_ADDITION_INIT;
+	int err;
 
 	REFTABLE_CALLOC_ARRAY(*dest, 1);
 	if (!*dest)
 		return REFTABLE_OUT_OF_MEMORY_ERROR;
 
-	**dest = empty;
 	err = reftable_stack_init_addition(*dest, st, flags);
 	if (err) {
 		reftable_free(*dest);
 		*dest = NULL;
 	}
+
 	return err;
 }
 
-- 
2.51.0.rc1.163.g2494970778.dirty
Previous: Patrick SteinhardtNext: Justin Tobler
Message 30 of 52 in “reftable: a couple of improvements for libgit2”
  1. 0/5 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 1, 2025
  2. 1/5 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 1, 2025
  3. 2/5 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 1, 2025
  4. 3/5 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 1, 2025
  5. Eric SunshineAug 1, 2025
  6. Patrick SteinhardtAug 4, 2025
  7. Junio C HamanoAug 4, 2025
  8. Patrick SteinhardtAug 5, 2025
  9. Carlo ArenasAug 12, 2025
  10. Patrick SteinhardtAug 12, 2025
  11. Junio C HamanoAug 12, 2025
  12. 4/5 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 1, 2025
  13. 5/5 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 1, 2025
  14. 0/6 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 4, 2025
  15. 1/6 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 4, 2025
  16. 2/6 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 4, 2025
  17. 3/6 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 4, 2025
  18. 4/6 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 4, 2025
  19. Justin ToblerAug 11, 2025
  20. 5/6 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 4, 2025
  21. Justin ToblerAug 11, 2025
  22. Patrick SteinhardtAug 12, 2025
  23. 6/6 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 4, 2025
  24. Justin ToblerAug 11, 2025
  25. Patrick SteinhardtAug 12, 2025
  26. 0/8 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 12, 2025
  27. 1/8 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 12, 2025
  28. 2/8 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 12, 2025
  29. 3/8 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 12, 2025
  30. 4/8 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 12, 2025
  31. Justin ToblerAug 12, 2025
  32. 5/8 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 12, 2025
  33. Justin ToblerAug 12, 2025
  34. Patrick SteinhardtAug 13, 2025
  35. 6/8 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 12, 2025
  36. 7/8 reftable: don't second-guess errors from flock interfacePatrick Steinhardt, Aug 12, 2025
  37. Justin ToblerAug 12, 2025
  38. 8/8 refs/reftable: always reload stacks when creating lockPatrick Steinhardt, Aug 12, 2025
  39. Justin ToblerAug 12, 2025
  40. Carlo ArenasAug 12, 2025
  41. Patrick SteinhardtAug 13, 2025
  42. Junio C HamanoAug 13, 2025
  43. 0/8 reftable: a couple of improvements for libgit2Patrick Steinhardt, Aug 13, 2025
  44. 1/8 reftable/writer: fix type used for number of recordsPatrick Steinhardt, Aug 13, 2025
  45. 2/8 reftable/writer: drop Git-specific `QSORT()` macroPatrick Steinhardt, Aug 13, 2025
  46. 3/8 reftable/stack: reorder code to avoid forward declarationsPatrick Steinhardt, Aug 13, 2025
  47. 4/8 reftable/stack: fix compiler warning due to missing bracesPatrick Steinhardt, Aug 13, 2025
  48. 5/8 reftable/stack: allow passing flags to `reftable_stack_add()`Patrick Steinhardt, Aug 13, 2025
  49. 6/8 reftable/stack: handle outdated stacks when compactingPatrick Steinhardt, Aug 13, 2025
  50. 7/8 reftable: don't second-guess errors from flock interfacePatrick Steinhardt, Aug 13, 2025
  51. 8/8 refs/reftable: always reload stacks when creating lockPatrick Steinhardt, Aug 13, 2025
  52. Justin ToblerAug 13, 2025

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.