Skip to contentSkip to conversation

Fix an "variable might be used uninitialized" gcc warning

PATCH In review Ramsay Jones 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix an "variable might be used uninitialized" gcc warning

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.

---

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(-)
builtin/checkout.c +1 -1
index 787d468..f1984d9 100644
@@ -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) {
Signature · 1 line
1.7.8
#1 lore raw
Quoted 4 lines · Ramsay Jones

CC builtin/checkout.o
builtin/checkout.c: In function `cmd_checkout':
builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
in this function

[...]

Quoted 2 lines

[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?

#2 lore raw
Quoted 6 lines · Jonathan Nieder
Quoted 4 lines · Ramsay Jones

CC builtin/checkout.o
builtin/checkout.c: In function `cmd_checkout':
builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
in this function

Quoted 7 lines

[...]

Quoted 2 lines

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

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#3 lore raw
In reply to Jonathan Nieder
Quoted 6 lines · Jonathan Nieder
Quoted 4 lines · Ramsay Jones

CC builtin/checkout.o
builtin/checkout.c: In function `cmd_checkout':
builtin/checkout.c:160: warning: 'mode' might be used uninitialized \
in this function

Quoted 7 lines

[...]

Quoted 2 lines

[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

#4 lore raw
Quoted 6 lines · Ramsay Jones

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.

[...]

Quoted 3 lines

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

#5 lore raw
Quoted 10 lines · Jonathan Nieder
Quoted 6 lines

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.

Quoted 8 lines
Quoted 3 lines

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

Signature · 2 lines
Joy, n. An emotion variously excited, but in its highest degree arising from
the contemplation of grief in another.
#6 lore raw
In reply to Jonathan Nieder
Quoted 4 lines · Jonathan Nieder

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