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

[PATCH] Fix -Wmaybe-uninitialized warnings under -O0

From
Denton Liu <liu.denton@gmail.com>
Date
Apr 1, 2020, 07:30 UTC
Message-ID
<33984eeaabbfbcfd4b9d3903549d8b7d6c4ced7e.1585726172.git.liu.denton@gmail.com>

When compiling Git under -O0, gcc (Arch Linux 9.3.0-1) 9.3.0 produces many -Wmaybe-uninitialized warnings. These are false positives since when Git is compiled under -O2, gcc is smart enough to see that the code paths that use these variables all initialise them beforehand. Nonetheless, these warnings block the compilation process when DEVELOPER=1 is enabled (which enables -Werror).

Fix these warnings by initializing these variables with dummy values (0, -1 or NULL as appropriate).

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/branch.c          | 2 +-
 diff.c                    | 2 +-
 fast-import.c             | 4 ++--
 http-backend.c            | 2 +-
 ref-filter.c              | 2 +-
 refs/packed-backend.c     | 2 +-
 t/helper/test-ref-store.c | 2 +-
 trace2/tr2_dst.c          | 2 +-
 trailer.c                 | 2 +-
 9 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index d8297f80ff..3669fba546 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -455,7 +455,7 @@ static void print_current_branch_name(void)
 {
 	int flags;
 	const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
-	const char *shortname;
+	const char *shortname = NULL;
 	if (!refname)
 		die(_("could not resolve HEAD"));
 	else if (!(flags & REF_ISSYMREF))
diff --git a/diff.c b/diff.c
index f2cfbf2214..99a35774d7 100644
--- a/diff.c
+++ b/diff.c
@@ -3263,7 +3263,7 @@ static void emit_binary_diff_body(struct diff_options *o,
 	void *delta;
 	void *deflated;
 	void *data;
-	unsigned long orig_size;
+	unsigned long orig_size = 0;
 	unsigned long delta_size;
 	unsigned long deflate_size;
 	unsigned long data_size;
diff --git a/fast-import.c b/fast-import.c
index b8b65a801c..0509c0b92f 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1829,7 +1829,7 @@ static void parse_original_identifier(void)
 
 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
 {
-	const char *data;
+	const char *data = NULL;
 	strbuf_reset(sb);
 
 	if (!skip_prefix(command_buf.buf, "data ", &data))
@@ -2719,7 +2719,7 @@ static void parse_new_commit(const char *arg)
 static void parse_new_tag(const char *arg)
 {
 	static struct strbuf msg = STRBUF_INIT;
-	const char *from;
+	const char *from = NULL;
 	char *tagger;
 	struct branch *s;
 	struct tag *t;
diff --git a/http-backend.c b/http-backend.c
index ec3144b444..1091cdf2cd 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -252,7 +252,7 @@ static void http_config(void)
 
 static struct rpc_service *select_service(struct strbuf *hdr, const char *name)
 {
-	const char *svc_name;
+	const char *svc_name = NULL;
 	struct rpc_service *svc = NULL;
 	int i;
 
diff --git a/ref-filter.c b/ref-filter.c
index b1812cb69a..6abd48f81a 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1938,7 +1938,7 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
 {
 	struct string_list prefixes = STRING_LIST_INIT_DUP;
 	struct string_list_item *prefix;
-	int ret;
+	int ret = 0;
 
 	if (!filter->match_as_path) {
 		/*
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 4458a0f69c..f37c6d19a6 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -627,7 +627,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
 
 	/* If the file has a header line, process it: */
 	if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
-		char *tmp, *p, *eol;
+		char *tmp, *p = NULL, *eol;
 		struct string_list traits = STRING_LIST_INIT_NODUP;
 
 		eol = memchr(snapshot->buf, '\n',
diff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c
index 799fc00aa1..d82dcb83a3 100644
--- a/t/helper/test-ref-store.c
+++ b/t/helper/test-ref-store.c
@@ -19,7 +19,7 @@ static unsigned int arg_flags(const char *arg, const char *name)
 
 static const char **get_store(const char **argv, struct ref_store **refs)
 {
-	const char *gitdir;
+	const char *gitdir = NULL;
 
 	if (!argv[0]) {
 		die("ref store required");
diff --git a/trace2/tr2_dst.c b/trace2/tr2_dst.c
index ae052a07fe..910301a53d 100644
--- a/trace2/tr2_dst.c
+++ b/trace2/tr2_dst.c
@@ -227,7 +227,7 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
 {
 	unsigned int uds_try = 0;
 	int fd;
-	int e;
+	int e = 0;
 	const char *path = NULL;
 
 	/*
diff --git a/trailer.c b/trailer.c
index 0c414f2fed..cac9d0a4d9 100644
--- a/trailer.c
+++ b/trailer.c
@@ -507,7 +507,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 	struct arg_item *item;
 	struct conf_info *conf;
 	char *name = NULL;
-	enum trailer_info_type type;
+	enum trailer_info_type type = -1;
 	int i;
 
 	if (!skip_prefix(conf_key, "trailer.", &trailer_item))
-- 
2.26.0.159.g23e2136ad0
Next: Jeff King
Message 1 of 31 in “Fix -Wmaybe-uninitialized warnings under -O0”
  1. Fix -Wmaybe-uninitialized warnings under -O0Denton Liu, Apr 1, 2020
  2. Jeff KingApr 1, 2020
  3. Denton LiuApr 1, 2020
  4. Jeff KingApr 3, 2020
  5. Jeff KingApr 3, 2020
  6. Denton LiuApr 4, 2020
  7. Jeff KingApr 4, 2020
  8. trace2: refactor to avoid gcc warning under -O3Ævar Arnfjörð Bjarmason, May 5, 2021
  9. Junio C HamanoMay 5, 2021
  10. Jeff KingMay 5, 2021
  11. Johannes SchindelinMay 5, 2021
  12. Junio C HamanoMay 6, 2021
  13. Johannes SchindelinMay 6, 2021
  14. Junio C HamanoMay 6, 2021
  15. Johannes SchindelinMay 11, 2021
  16. Jeff KingMay 11, 2021
  17. Junio C HamanoMay 11, 2021
  18. Jeff KingMay 11, 2021
  19. Junio C HamanoMay 11, 2021
  20. Junio C HamanoMay 11, 2021
  21. trace2: refactor to avoid gcc warning under -O3Ævar Arnfjörð Bjarmason, May 11, 2021
  22. Jeff HostetlerMay 11, 2021
  23. Jeff KingMay 11, 2021
  24. Jeff KingMay 11, 2021
  25. Junio C HamanoMay 11, 2021
  26. Junio C HamanoMay 20, 2021
  27. trace2: refactor to avoid gcc warning under -O3Ævar Arnfjörð Bjarmason, May 20, 2021
  28. Jeff KingMay 20, 2021
  29. Junio C HamanoMay 20, 2021
  30. Jeff KingMay 21, 2021
  31. Denton LiuApr 1, 2020

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.