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

Re: [PATCH] config.c: fix a compiler warning

From
Jeff King <peff@peff.net>
Date
Apr 16, 2014, 15:29 UTC
Message-ID
<20140416152920.GA4691@sigill.intra.peff.net>
In-Reply-To
<20140416141353.GA9676@camelia.ucw.cz>
On Wed, Apr 16, 2014 at 04:13:53PM +0200, Stepan Kasal wrote:
> Date: Thu, 10 Apr 2014 16:37:15 +0200
> 
> This change fixes a gcc warning when building msysGit.
What warning? I'm assuming -Wuninitialized?
Show 13 quoted lines
> diff --git a/config.c b/config.c
> index 314d8ee..0b7e4f8 100644
> --- a/config.c
> +++ b/config.c
> @@ -572,7 +572,7 @@ static void die_bad_number(const char *name, const char *value)
>  
>  int git_config_int(const char *name, const char *value)
>  {
> -	int ret;
> +	int ret = 0;
>  	if (!git_parse_int(value, &ret))
>  		die_bad_number(name, value);
>  	return ret;

Hmph. Generally gcc should assume that a variable is initialized after a pointer to it is passed into a function. Unless it inlines that function and can see that it isn't. But if we do inline git_parse_int, we see that it "ret" is always initialized if it returns non-zero.

If it also inlines die_bad_number, it would see that we end in die(), which is marked NORETURN. But if it does not, it will not realize that we do not get to "return ret" in that case.

So perhaps a better solution is:
diff --git a/config.c b/config.c
index 6821cef..a30cb5c 100644
--- a/config.c
+++ b/config.c
@@ -557,6 +557,7 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 	return 1;
 }
 
+NORETURN
 static void die_bad_number(const char *name, const char *value)
 {
 	const char *reason = errno == ERANGE ?

Does that also silence the warning?

-Peff
Previous: Stepan KasalNext: Torsten Bögershausen
Message 2 of 5 in “config.c: fix a compiler warning”
  1. config.c: fix a compiler warningStepan Kasal, Apr 16, 2014
  2. Jeff KingApr 16, 2014
  3. Torsten BögershausenApr 16, 2014
  4. config.c: mark die_bad_number as NORETURNJeff King, Apr 16, 2014
  5. Junio C HamanoApr 16, 2014

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.