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

[PATCH] Fix an "variable might be used uninitialized" gcc warning

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Dec 16, 2011, 22:44 UTC
Message-ID
<4EEBC9D6.6010204@ramsay1.demon.co.uk>
In particular, gcc issues the following warning:
        CC builtin/checkout.o
    builtin/checkout.c: In function `cmd_checkout':
    builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
        in this function

However, the analysis performed by gcc is too conservative, in this case, since the mode variable will not be used uninitialised. Note that, if the mode variable is not set in the loop, then "threeway[1]" will also still be set to the null SHA1. This will then result in control leaving the function, almost directly after the loop, well before the potential use in the call to make_cache_entry().

In order to suppress the warning, we initialise the mode variable to zero in it's declaration.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Just in case you haven't found the time to apply your own patch!

[Note that only 2 out of the 3 versions of gcc I use issues this warning]

ATB, Ramsay Jones

 builtin/checkout.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 787d468..f1984d9 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -157,7 +157,7 @@ static int checkout_merged(int pos, struct checkout *state)
 	unsigned char sha1[20];
 	mmbuffer_t result_buf;
 	unsigned char threeway[3][20];
-	unsigned mode;
+	unsigned mode = 0;
 
 	memset(threeway, 0, sizeof(threeway));
 	while (pos < active_nr) {
-- 
1.7.8
Next: Jonathan Nieder
Message 1 of 7 in “Fix an "variable might be used uninitialized" gcc warning”
  1. Fix an "variable might be used uninitialized" gcc warningRamsay Jones, Dec 16, 2011
  2. Jonathan NiederDec 16, 2011
  3. Andreas SchwabDec 17, 2011
  4. Ramsay JonesJan 31, 2012
  5. Jonathan NiederJan 31, 2012
  6. Miles BaderFeb 1, 2012
  7. Ramsay JonesFeb 2, 2012

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.