All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix an "variable might be used uninitialized" gcc warning
@ 2011-12-16 22:44 Ramsay Jones
  2011-12-16 23:59 ` Jonathan Nieder
  0 siblings, 1 reply; 7+ messages in thread
From: Ramsay Jones @ 2011-12-16 22:44 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: GIT Mailing-list


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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2011-12-16 22:44 [PATCH] Fix an "variable might be used uninitialized" gcc warning Ramsay Jones
@ 2011-12-16 23:59 ` Jonathan Nieder
  2011-12-17 10:22   ` Andreas Schwab
  2012-01-31 18:36   ` Ramsay Jones
  0 siblings, 2 replies; 7+ messages in thread
From: Jonathan Nieder @ 2011-12-16 23:59 UTC (permalink / raw)
  To: Ramsay Jones; +Cc: Junio C Hamano, GIT Mailing-list

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?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2011-12-16 23:59 ` Jonathan Nieder
@ 2011-12-17 10:22   ` Andreas Schwab
  2012-01-31 18:36   ` Ramsay Jones
  1 sibling, 0 replies; 7+ messages in thread
From: Andreas Schwab @ 2011-12-17 10:22 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Ramsay Jones, Junio C Hamano, GIT Mailing-list

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."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2011-12-16 23:59 ` Jonathan Nieder
  2011-12-17 10:22   ` Andreas Schwab
@ 2012-01-31 18:36   ` Ramsay Jones
  2012-01-31 19:43     ` Jonathan Nieder
  1 sibling, 1 reply; 7+ messages in thread
From: Ramsay Jones @ 2012-01-31 18:36 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Junio C Hamano, GIT Mailing-list

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2012-01-31 18:36   ` Ramsay Jones
@ 2012-01-31 19:43     ` Jonathan Nieder
  2012-02-01  7:16       ` Miles Bader
  2012-02-02 18:25       ` Ramsay Jones
  0 siblings, 2 replies; 7+ messages in thread
From: Jonathan Nieder @ 2012-01-31 19:43 UTC (permalink / raw)
  To: Ramsay Jones; +Cc: Junio C Hamano, GIT Mailing-list

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2012-01-31 19:43     ` Jonathan Nieder
@ 2012-02-01  7:16       ` Miles Bader
  2012-02-02 18:25       ` Ramsay Jones
  1 sibling, 0 replies; 7+ messages in thread
From: Miles Bader @ 2012-02-01  7:16 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Ramsay Jones, Junio C Hamano, GIT Mailing-list

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix an "variable might be used uninitialized" gcc warning
  2012-01-31 19:43     ` Jonathan Nieder
  2012-02-01  7:16       ` Miles Bader
@ 2012-02-02 18:25       ` Ramsay Jones
  1 sibling, 0 replies; 7+ messages in thread
From: Ramsay Jones @ 2012-02-02 18:25 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Junio C Hamano, GIT Mailing-list

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-02-02 18:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-16 22:44 [PATCH] Fix an "variable might be used uninitialized" gcc warning Ramsay Jones
2011-12-16 23:59 ` Jonathan Nieder
2011-12-17 10:22   ` Andreas Schwab
2012-01-31 18:36   ` Ramsay Jones
2012-01-31 19:43     ` Jonathan Nieder
2012-02-01  7:16       ` Miles Bader
2012-02-02 18:25       ` Ramsay Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.