Skip to contentSkip to conversation

fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

DISCUSSION Toralf Förster 8 participants 11 messages Mar 28, 2007 – Mar 31, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I compiled current git source 2.6.21-rc5-g28defbe and got this warning:
...
fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function
...

Signature · 3 lines
MfG/Sincerely

Toralf Förster
#1 lore raw
Quoted 6 lines · Toralf Förster

I compiled current git source 2.6.21-rc5-g28defbe and got this warning:
...
fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function
...

Most of these warnings are really GCC bugs. Please examine the code in question.

Lee

#2 lore raw
Quoted 8 lines · Lee Revell
Quoted 6 lines

I compiled current git source 2.6.21-rc5-g28defbe and got this warning:
...
fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function
...

Most of these warnings are really GCC bugs. Please examine the code in question.

Anyway imho this time gcc got it right?

From: Jiri Kosina <[email protected]>

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

Fixes this warning:

fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

drivers/usb/input/hid-tmff.c +0 -0
fs/block_dev.c +1 -1
index 575076c..e87d84a 100644
@@ -950,7 +950,7 @@ static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 				struct kobject *kobj)
 {
 	int res;
-	struct bd_holder *bo, *found;
+	struct bd_holder *bo, *found = NULL;
 
 	if (!kobj)
 		return -EINVAL;
#3 lore raw
Quoted 18 lines · On Wed, 28 Mar 2007 19:23:32 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

Fixes this warning:

fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

drivers/usb/input/hid-tmff.c +0 -0
fs/block_dev.c +0 -0
index 575076c..e87d84a 100644

fs/block_dev.c static int bd_claim_by_kobject(struct block_device *bdev, void *holder, · line 953

Quoted 5 lines
fs/block_dev.c +1 -1
@@ -950,7 +950,7 @@ static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 				struct kobject *kobj)
 {
 	int res;
-	struct bd_holder *bo, *found;
+	struct bd_holder *bo, *found = NULL;

that generates extra code and people get upset.

One approach which we could ue in here is

struct bd_holder *found = found; /* Suppress bogus gcc warning */

which (surprisingly) gcc will currently accept, and which shouldn't generate more code, although I haven't verified this.

But it's all rather ad-hoc and unpleasant. I tend to think that we should come up with some standardised way of squashing this warning - something which stands out when one is reading the code, like

	struct bd_holder *found;

	squash_bogus_uninit_warning(found);	/* useful comment goes here */

which is also unpleasant, but not as unpleasant as a screenful of warnings which hide real problems, IMO.

#4 lore raw
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Wed, 28 Mar 2007 19:23:32 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

[..]

Quoted 8 lines

fs/block_dev.c static int bd_claim_by_kobject(struct block_device *bdev, void *holder, · line 953

Quoted 6 lines
fs/block_dev.c +1 -1
@@ -950,7 +950,7 @@ static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 				struct kobject *kobj)
 {
 	int res;
-	struct bd_holder *bo, *found;
+	struct bd_holder *bo, *found = NULL;

that generates extra code and people get upset.

I, for one, not upset. On the contrary.

IMHO gcc should be smart enough to optimize that code properly with that "= NULL" added.

BTW with gcc 4.1.2 on x86_64 that warning doesn't get emitted, and it generates the same exact code with or without " = NULL". One could aruge, if people are upset about more code being generated because they use an older stable branch of gcc, it's _their_ problem.

Quoted 6 lines
	struct bd_holder *found;

	squash_bogus_uninit_warning(found);	/* useful comment goes here */

which is also unpleasant, but not as unpleasant as a screenful of warnings which hide real problems, IMO.

If there was such 'squash_bogus_uninit_warning' macro exist and in use, then this could have been a possible scenario:

A) There's some 200-lines long function.
B) It has a squash_bogus_uninit_warning() somewhere in the beginning.
C) Someone commits a patch that uses an uninitialized variable on _some_ cases and it doesn't generate a warning.
D) You get an 'heisenbug', since that pointer might point to something that is dereferencable without a fault, etc.

I think that warnings of these kind (assuming that they are not generated as a result of deficiencies in the latest stable version of gcc) exist for a damn good reason - the code should be fixed and that warning shouldn't be bypassed in semi-nasty ways.

Signature · 3 lines
Dan Aloni
XIV LTD, http://www.xivstorage.com
da-x (at) monatomic.org, dan (at) xiv.co.il
#5 lore raw
In reply to Andrew Morton
Quoted 10 lines · Andrew Morton
Quoted 7 lines · On Wed, 28 Mar 2007 19:23:32 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

Fixes this warning:

fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

Quoted 7 lines
Quoted 1 line

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

that generates extra code and people get upset.

One approach which we could ue in here is

struct bd_holder *found = found; /* Suppress bogus gcc warning */

Well, that would be correct except the warning is an actual kernel bug. Read Jiri's message (which you also quoted):

Quoted 1 line

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

So in this case it has to be initialized to NULL or there's a potential BUG() lurking.

Cheers,
Kyle Moffett

#6 lore raw
Quoted 23 lines · Kyle Moffett
Quoted 16 lines · Andrew Morton
Quoted 5 lines · On Wed, 28 Mar 2007 19:23:32 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

Fixes this warning:

Quoted 7 lines

fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

Quoted 5 lines

that generates extra code and people get upset.

One approach which we could ue in here is

struct bd_holder *found = found; /* Suppress bogus gcc warning */

Quoted 8 lines

Well, that would be correct except the warning is an actual kernel bug. Read Jiri's message (which you also quoted):

Quoted 3 lines

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

So in this case it has to be initialized to NULL or there's a potential BUG() lurking.

No, the code is correct and it's impossible that the variable ever gets read uninitialized.

And BTW, i386 gcc 4.1 doesn't give me a warning for this. Toralf, which gcc version and architecture did you see this with?

Quoted 2 lines

Cheers,
Kyle Moffett

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#7 lore raw

2007/3/31, Adrian Bunk <[email protected]>:

Quoted 34 lines · Kyle Moffett
Quoted 23 lines · Andrew Morton
Quoted 16 lines
Quoted 5 lines · On Wed, 28 Mar 2007 19:23:32 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

blockdev: bd_claim_by_kobject() could check value of unititalized pointer

Fixes this warning:

Quoted 7 lines

fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

Quoted 5 lines

that generates extra code and people get upset.

One approach which we could ue in here is

struct bd_holder *found = found; /* Suppress bogus gcc warning */

Quoted 8 lines

Well, that would be correct except the warning is an actual kernel bug. Read Jiri's message (which you also quoted):

Quoted 3 lines

struct bd_holder *found is initialized only when bd_claim() returns zero. If it returns nonzero, ptr stays uninitialized. Later the value of the pointer is checked.

So in this case it has to be initialized to NULL or there's a potential BUG() lurking.

Quoted 6 lines

No, the code is correct and it's impossible that the variable ever gets read uninitialized.

And BTW, i386 gcc 4.1 doesn't give me a warning for this. Toralf, which gcc version and architecture did you see this with?

I am also using i386 gcc 4.1.1, and I did receive many warnings of such kind yesterday. I think we should fix them.

And the reason for the existence of such things is we just want to use them for writing first instead of reading, thus ignore the initialization.

Signature · 1 line
So Dark The Con Of Man.
#8 lore raw
In reply to Adrian Bunk

Am Freitag, 30. März 2007 21:47 schrieb Adrian Bunk:

Quoted 11 lines

No, the code is correct and it's impossible that the variable ever gets read uninitialized.

And BTW, i386 gcc 4.1 doesn't give me a warning for this. Toralf, which gcc version and architecture did you see this with?

cu
Adrian

Hi, I use currently the following version:

gcc version 3.4.6 (Gentoo 3.4.6-r2, ssp-3.4.6-1.0, pie-8.7.10)

Signature · 3 lines
MfG/Sincerely

Toralf Förster
#9 lore raw
Quoted 9 lines · Toralf Förster

Am Freitag, 30. März 2007 21:47 schrieb Adrian Bunk:

Quoted 7 lines

No, the code is correct and it's impossible that the variable ever gets read uninitialized.

And BTW, i386 gcc 4.1 doesn't give me a warning for this. Toralf, which gcc version and architecture did you see this with?

Quoted 7 lines
Quoted 3 lines

cu
Adrian

Hi, I use currently the following version:

gcc version 3.4.6 (Gentoo 3.4.6-r2, ssp-3.4.6-1.0, pie-8.7.10)

For finding warnings it's better to use the latest gcc version - gcc is evalving, and more recent versions tend to both give more valid warnings and give less warnings for non-problems (like in this case).

Quoted 3 lines

MfG/Sincerely

Toralf Förster

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#10 lore raw
In reply to Lee Revell
Quoted 11 lines · Lee Revell
Quoted 7 lines · Toralf Förster

I compiled current git source 2.6.21-rc5-g28defbe and got this warning:
...
fs/block_dev.c: In function `bd_claim_by_kobject':
fs/block_dev.c:953: warning: 'found' might be used uninitialized in this function
...

Most of these warnings are really GCC bugs. Please examine the code in question.

The word "GCC bugs" is a bit hard for them.

gcc gives different warnings for code where it could prove that a variable gets used uninitialized and code where it's not sure whether a variable will always be used initialized.

There are even cases where it's technically impossible for gcc to determine that a variable always gets initialized.

And these "might be used uninitialized" warnings have already found several kernel bugs.

Quoted 1 line

Lee

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed