Skip to contentSkip to conversation

gcc4 warnings, variable used uninitialized

KJ Stephan Peijnik 5 participants 7 messages Feb 21, 2006 – Feb 22, 2006 Kernel janitors

lore
Share & tools
lore
#0 lore raw

Hello list!

Does anyone know exactly how gcc4 decides whether a variable is undefined or not and how that should be handled kernel-wise?

I've come across a few snippets where for example a temporary u32 is declared and then a pointer to that variable is given to pci_read_config_dword. When that variable is used afterwards gcc4 complains about it being not initialized (Using gcc-4.0.2 and kernel 2.6.16-rc4 as of yesterday).

Should 'bugs' like those be fixed? I've found like a gazillion of them in the AGP drivers and other parts of the kernel. I'm thinking about that as it seems to be the best point to get myself started and I would appreciate comments on that topic.

Additionally, a comment on whether my last submitted patch is okay (kauditd_thread not returning a value) would be nice aswell. Probably including directions on where to send that patch to.

Thanks in advance.

Regards,

Stephan Peijnik

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#1 lore raw
Quoted 2 lines · Stephan Peijnik

Does anyone know exactly how gcc4 decides whether a variable is undefined or not and how that should be handled kernel-wise?

It has been found that gcc-4.0 gets it wrong. Most of the places where gcc-4.0 wrongly complains and gcc-3.4 does not have been fixed in gcc-4.1.

Quoted 10 lines

I've come across a few snippets where for example a temporary u32 is declared and then a pointer to that variable is given to pci_read_config_dword. When that variable is used afterwards gcc4 complains about it being not initialized (Using gcc-4.0.2 and kernel 2.6.16-rc4 as of yesterday).

Should 'bugs' like those be fixed? I've found like a gazillion of them in the AGP drivers and other parts of the kernel. I'm thinking about that as it seems to be the best point to get myself started and I would appreciate comments on that topic.

A small number of these bugs are valid and should be fixed, but it is not trivial to determine in some cases. What you say about this happening with pci_read_config_dword sounds like it is a gcc bug and not a bug in the kernel sources, so it should not be fixed in the kernel.

Can you be more specific with the example and/or reproduce it with gcc-4.1?

	Arnd <><

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#2 lore raw
In reply to Stephan Peijnik
Quoted 3 lines · Arnd Bergmann

It has been found that gcc-4.0 gets it wrong. Most of the places where gcc-4.0 wrongly complains and gcc-3.4 does not have been fixed in gcc-4.1.

Okay, I was about to go and use good old 3.4 for testing purposes. I've been wondering about the warnings anyways.

Quoted 5 lines

A small number of these bugs are valid and should be fixed, but it is not trivial to determine in some cases. What you say about this happening with pci_read_config_dword sounds like it is a gcc bug and not a bug in the kernel sources, so it should not be fixed in the kernel.

I wasn't that sure whether those warnings would be right and kept forever or not. Doing those minimalistic modifications probably wouldn't have done any harm, but you are right about where the problem(s) should be fixed.

Quoted 3 lines

Can you be more specific with the example and/or reproduce it with gcc-4.1?

I'm compiling a fresh copy of 4.1 right now, but a good example would be drivers/char/agp/generic.c's get_agp_version function. It defines a 'u32 ncapid'. A pointer to that u32 is then given to pci_read_config_dword and at exactly that point gcc-4.0 starts complaining. See below.

<snip>
void get_agp_version(struct agp_bridge_data *bridge)

{
	u32 ncapid;

	/* Exit early if already set by errata workarounds. */
	if (bridge->major_version != 0)
		return;

	pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);

[...]
</snip>

However, thanks for the response. On that topic everything is kind of clear to me now.

Regards,

Stephan Peijnik

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#3 lore raw
In reply to Stephan Peijnik
Quoted 15 lines · Stephan Peijnik

I'm compiling a fresh copy of 4.1 right now, but a good example would be drivers/char/agp/generic.c's get_agp_version function. It defines a 'u32 ncapid'. A pointer to that u32 is then given to pci_read_config_dword and at exactly that point gcc-4.0 starts complaining. See below.

<snip>
void get_agp_version(struct agp_bridge_data *bridge)

{
	u32 ncapid;

	/* Exit early if already set by errata workarounds. */
	if (bridge->major_version != 0)
		return;

	pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
Quoted 2 lines

[...]
</snip>

gcc bug. It doesn't seem to understand that sometimes ptrs to vars get passed to functions that instantiate them.

There are dozens and dozens of such instances in the kernel tree. 99% of the time that warning is completely bogus.

Dave

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#4 lore raw
In reply to Stephan Peijnik
Quoted 13 lines · Dave Jones
Quoted 10 lines

<snip>
void get_agp_version(struct agp_bridge_data *bridge)

{
     u32 ncapid;

     /* Exit early if already set by errata workarounds. */
     if (bridge->major_version != 0)
             return;

     pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
Quoted 2 lines

[...]
</snip>

Quoted 5 lines

gcc bug. It doesn't seem to understand that sometimes ptrs to vars get passed to functions that instantiate them.

There are dozens and dozens of such instances in the kernel tree. 99% of the time that warning is completely bogus.

I would not call it a bug. The problem is that gcc does not know if the third argument of pci_read_config_dword is used as an in parameter or out parameter.

If the pointer is used as an in parameter, then the code above is wrong and pci_read_config_dword will read some random memory (which gcc rightly should warn about). If the pointer only is used as out parameter then there is no problem but since gcc cannot tell, it issues a warning in any case. Because of that the warning will most likely give many false alarms and be quite unuseful, but it is not wrong I think.

BR Håkon Løvdal

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#5 lore raw
In reply to Stephan Peijnik
Quoted 17 lines · Håkon Løvdal
Quoted 16 lines · Dave Jones
Quoted 10 lines

<snip>
void get_agp_version(struct agp_bridge_data *bridge)

{
     u32 ncapid;

     /* Exit early if already set by errata workarounds. */
     if (bridge->major_version != 0)
             return;

     pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
Quoted 2 lines

[...]
</snip>

gcc bug. It doesn't seem to understand that sometimes ptrs to vars get passed to functions that instantiate them.

Quoted 6 lines
Quoted 2 lines

There are dozens and dozens of such instances in the kernel tree. 99% of the time that warning is completely bogus.

I would not call it a bug. The problem is that gcc does not know if the third argument of pci_read_config_dword is used as an in parameter or out parameter.

But gcc claims that you use it before actually initializing it (without actually really knowing it).

Analysis: At most it is possible to declare that parameter as "const <type> *" which implies that it isn't expected to be written to. This implies that the parameter is basically read-only (in theory). And *then* the C compiler can (and should) warn.

If the parameter has no "const" attached as above, the C compiler has no way (except analyzing the source code) to tell if the parameter, is read before written too or not.
So the compiler should shut up instead of (possibly) claiming falsely that the variable is used without initialization.

To go one step further one may say that if the parameter is not "const" it may be written to (otherwise it should be "const" in these days). But even this doesn't rule out that it is read before written too (where the warning makes sense).

To be more contructive: Is it OK to spend the (mostly useless) initialization code and time (ans shut up the warnings) or not? Or do we simply wait for a (in this aspect) sane gcc and the problem is solved anyway.
These are the real questions IMHO.

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#6 lore raw
In reply to Stephan Peijnik
Quoted 4 lines · Bernd Petrovitsch

To be more contructive: Is it OK to spend the (mostly useless) initialization code and time (ans shut up the warnings) or not? Or do we simply wait for a (in this aspect) sane gcc and the problem is solved anyway.

Ignore them, and wait for a fixed compiler.

Dave

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors