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

7 messages from 2011-12-16 to 2012-02-02. Participants: Ramsay Jones, Jonathan Nieder, Andreas Schwab, Miles Bader.
Thread: https://gitlist.dev/t/29189

## Ramsay Jones, 2011-12-16 22:44

Subject: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <4EEBC9D6.6010204@ramsay1.demon.co.uk>
URL: https://gitlist.dev/e/4EEBC9D6.6010204%40ramsay1.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

```

## Jonathan Nieder, 2011-12-16 23:59

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <20111216235908.GA5858@elie.hsd1.il.comcast.net>
URL: https://gitlist.dev/e/20111216235908.GA5858%40elie.hsd1.il.comcast.net
In-Reply-To: <4EEBC9D6.6010204@ramsay1.demon.co.uk>

```
Ramsay Jones wrote:

>         CC builtin/checkout.o
>     builtin/checkout.c: In function `cmd_checkout':
>     builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
>         in this function
[...]
> [Note that only 2 out of the 3 versions of gcc I use issues this
> warning]

Which version of gcc is that?  Is gcc getting more sane, so we won't
have to worry about this after a while, or is the false positive a
new regression that should be reported to them?

```

## Andreas Schwab, 2011-12-17 10:22

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <m2iplffqgg.fsf@igel.home>
URL: https://gitlist.dev/e/m2iplffqgg.fsf%40igel.home
In-Reply-To: <20111216235908.GA5858@elie.hsd1.il.comcast.net>

```
Jonathan Nieder <jrnieder@gmail.com> writes:

> Ramsay Jones wrote:
>
>>         CC builtin/checkout.o
>>     builtin/checkout.c: In function `cmd_checkout':
>>     builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
>>         in this function
> [...]
>> [Note that only 2 out of the 3 versions of gcc I use issues this
>> warning]
>
> Which version of gcc is that?  Is gcc getting more sane, so we won't
> have to worry about this after a while, or is the false positive a
> new regression that should be reported to them?

The regression is that the function has been changed in a way that makes
it impossible to infer the intended flow.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Ramsay Jones, 2012-01-31 18:36

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <4F2834AD.20004@ramsay1.demon.co.uk>
URL: https://gitlist.dev/e/4F2834AD.20004%40ramsay1.demon.co.uk
In-Reply-To: <20111216235908.GA5858@elie.hsd1.il.comcast.net>

```
Jonathan Nieder wrote:
> Ramsay Jones wrote:
> 
>>         CC builtin/checkout.o
>>     builtin/checkout.c: In function `cmd_checkout':
>>     builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
>>         in this function
> [...]
>> [Note that only 2 out of the 3 versions of gcc I use issues this
>> warning]
> 
> Which version of gcc is that?  Is gcc getting more sane, so we won't
> have to worry about this after a while, or is the false positive a
> new regression that should be reported to them?

[Sorry for the late reply, I've been away from email for several weeks...]

The versions which complain are 3.4.4 and 4.1.2, whereas 4.4.0 compiles
the code without complaint. So, gcc *may* be getting more sane, but I wouldn't
bet on it! :-P

I've had examples of this kind of warning, which relies heavily on the
analysis performed primarily for the optimizer, come-and-go in gcc before; so
don't hold your breath (this is the most volatile part of the compiler).

Having said that, unless you are going to decree that the project only
supports gcc (and presumably only some particular versions of gcc), then you
may well find similar warnings triggered when using other compilers anyway ...

ATB,
Ramsay Jones

```

## Jonathan Nieder, 2012-01-31 19:43

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <20120131194302.GD12443@burratino>
URL: https://gitlist.dev/e/20120131194302.GD12443%40burratino
In-Reply-To: <4F2834AD.20004@ramsay1.demon.co.uk>

```
Ramsay Jones wrote:

> The versions which complain are 3.4.4 and 4.1.2, whereas 4.4.0 compiles
> the code without complaint. So, gcc *may* be getting more sane, but I wouldn't
> bet on it! :-P
>
> I've had examples of this kind of warning, which relies heavily on the
> analysis performed primarily for the optimizer, come-and-go in gcc before

Yep, judging from the commit message, Junio found the same warning
in 4.6.2.

[...]
> Having said that, unless you are going to decree that the project only
> supports gcc (and presumably only some particular versions of gcc), then you
> may well find similar warnings triggered when using other compilers anyway ...

Sure, when the control flow grows too complicated, that's probably worth
fixing anyway, for the sake of humans especially.

Sometimes gcc is the only crazy one, though. ;-)

Thanks for the update.
Jonathan

```

## Miles Bader, 2012-02-01 07:16

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <buo7h07rpl8.fsf@dhlpc061.dev.necel.com>
URL: https://gitlist.dev/e/buo7h07rpl8.fsf%40dhlpc061.dev.necel.com
In-Reply-To: <20120131194302.GD12443@burratino>

```
Jonathan Nieder <jrnieder@gmail.com> writes:
>> The versions which complain are 3.4.4 and 4.1.2, whereas 4.4.0 compiles
>> the code without complaint. So, gcc *may* be getting more sane, but I wouldn't
>> bet on it! :-P
>>
>> I've had examples of this kind of warning, which relies heavily on the
>> analysis performed primarily for the optimizer, come-and-go in gcc before
>
> Yep, judging from the commit message, Junio found the same warning
> in 4.6.2.
>
>> Having said that, unless you are going to decree that the project only
>> supports gcc (and presumably only some particular versions of gcc), then you
>> may well find similar warnings triggered when using other compilers anyway ...
>
> Sure, when the control flow grows too complicated, that's probably worth
> fixing anyway, for the sake of humans especially.
>
> Sometimes gcc is the only crazy one, though. ;-)

It's hard to see how any compiler could detect that "mode" always
receives a value here .... it would have to realize that "stage" always
becomes 2 before the loop is exited, and that seems to depend on
non-trivial properties of external data structures...

-miles

-- 
Joy, n. An emotion variously excited, but in its highest degree arising from
the contemplation of grief in another.

```

## Ramsay Jones, 2012-02-02 18:25

Subject: Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
Message-ID: <4F2AD519.2010706@ramsay1.demon.co.uk>
URL: https://gitlist.dev/e/4F2AD519.2010706%40ramsay1.demon.co.uk
In-Reply-To: <20120131194302.GD12443@burratino>

```
Jonathan Nieder wrote:
> Sure, when the control flow grows too complicated, that's probably worth
> fixing anyway, for the sake of humans especially.
> 
> Sometimes gcc is the only crazy one, though. ;-)

Indeed. :-D

ATB,
Ramsay Jones

```
