Skip to contentSkip to conversation

Fix gcc4 warning,

PATCH In review Jesse Millan 3 participants 9 messages May 20, 2005 – May 26, 2005

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

PATCH 1/1 Fix gcc4 warning,

This patch eliminates the warning that is generated when passing an uninitialized variable to a function, and in that function it 'looks' like you may read the contents.

In this case, the address of a local variable 'idx' is passed to the function bvec_alloc_bs(). Inside bvec_alloc_bs(), it is possible that no value will be assigned to idx, in which case the function immediately returns null and does not go on to read it. Human eyes can see that its safe. Initializing it doen not hurt and it gets rid of the compiler warning.

fs/bio.c renamed from fs/bio.c~ +1 -1
@@ -159,7 +159,7 @@ struct bio *bio_alloc_bioset(unsigned in
 
 		bio_init(bio);
 		if (likely(nr_iovecs)) {
-			unsigned long idx;
+			unsigned long idx = 0;
 
 			bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
 			if (unlikely(!bvl)) {

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

#1 lore raw
Quoted 9 lines · Jesse Millan

This patch eliminates the warning that is generated when passing an uninitialized variable to a function, and in that function it 'looks' like you may read the contents.

In this case, the address of a local variable 'idx' is passed to the function bvec_alloc_bs(). Inside bvec_alloc_bs(), it is possible that no value will be assigned to idx, in which case the function immediately returns null and does not go on to read it. Human eyes can see that its safe.

I think this should go straight to http://gcc.gnu.org/bugzilla/enter_bug.cgi
_______________________________________________
Kernel-janitors mailing list
[email protected]
http://lists.osdl.org/mailman/listinfo/kernel-janitors

#2 lore raw
In reply to Jesse Millan
Quoted 9 lines · Jesse Millan

So, the compiler should be able to tell that either one of the two cases apply?

a: an assignment statement will be done on idx before its ever read

or

b: no assignment statement, control flow will never allow idx to be read

gcc is normally pretty good at detecting this. Since gcc-4.0, it appears to do more checking inside inline functions, where older versions just assumed that a variable is initialized after a reference to it is passed to an inline function.

I'd suggest fixing the inline function to always do this instead of fixing the caller. After all, this needs to be fixed in the kernel, even if future gcc versions become smarter about it gcc-4.0 will continue to be used.

	Arnd <><
fs/bio.c +1 -0
@@ -90,6 +90,7 @@
 		case  65 ... 128: *idx = 4; break;
 		case 129 ... BIO_MAX_PAGES: *idx = 5; break;
 		default:
+			*idx = 0;
 			return NULL;
 	}
 	/*

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

#3 lore raw
In reply to Jesse Millan
Quoted 4 lines · Jesse Millan

Its not easy to reproduce this warning in a simple program. And, I am not sure that it is a bug on gcc4's part. It looks like gcc4 emits the warning when it cannot prove beyond a shadow of doubt that the variable is initialized before use.

Right. The behavior of gcc4 makes perfect sense, I would not call it a bug at all, even though it could still become smarter.

Quoted 3 lines

In an effort to cut down on warnings, should these be fixed? It can go either way, the compiler should be smart enough and maybe it will be someday, or, do the initialization to get rid of a bunch of warnings.

As I said, they should be fixed in the kernel, the released compiler won't go away any more so it doesn't matter if future compilers are smarter than the current one.

	Arnd <><

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

#5 lore raw

PATCH 1/1 Fix gcc4 warning,

In reply to Jesse Millan

This patch eliminates the warning that is generated when passing a reference of an uninitialized variable to a function where it possible that the function will return without initializing that variable.

Might help I attach the patch.

fs/eventpoll.c renamed from fs/eventpoll.c~ +3 -0
@@ -748,6 +748,9 @@ eexit_3:
 eexit_2:
 	put_filp(file);
 eexit_1:
+	/* Reference went uninitialized. */
+	*efd = 0;
+	*efile = NULL;
 	return error;
 }

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

#6 lore raw

PATCH 1/1 Fix gcc4 warning,

In reply to Jesse Millan

Same as the last. This patch eliminates the warning that is generated when passing a reference of an uninitialized variable to a function where it possible that the function will return without initializing that variable.

The first execution path leaves both def and len uninitialized. The second leaves len uninitialized. In both cases, initializing them when they otherwise would not have been is close to pointless because they would not get used anyway. The change is only to suppress the compiler warning.

fs/cifs/asn1.c renamed from fs/cifs/asn1.c~ +9 -3
@@ -160,12 +160,18 @@ asn1_length_decode(struct asn1_ctx *ctx,
 {
 	unsigned char ch, cnt;
 
-	if (!asn1_octet_decode(ctx, &ch))
+	if (!asn1_octet_decode(ctx, &ch)) {
+		/* Function would have returned without initializing 'def' and 'len' */
+		*def = 0;
+		*len = 0;
 		return 0;
+	}
 
-	if (ch == 0x80)
+	if (ch == 0x80) {
 		*def = 0;
-	else {
+		/* Function would have returned without initializing 'len' */
+		*len = 0;
+	} else {
 		*def = 1;
 
 		if (ch < 0x80)

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

#7 lore raw

PATCH 1/1 Fix gcc4 warning,

In reply to Jesse Millan

Same same. This patch eliminates the warning that is generated when passing a reference of an uninitialized variable to a function where it possible that the function will return without initializing that variable.

fs/devfs/base.c renamed from fs/devfs/base.c~ +4 -1
@@ -1127,8 +1127,11 @@ static devfs_handle_t _devfs_make_parent
 
 	if (dir == NULL)
 		dir = _devfs_get_root_entry();
-	if (dir == NULL)
+	if (dir == NULL) {
+		/* Function would have returned without initializing 'leaf_pos' */
+		*leaf_pos = 0;
 		return NULL;
+	}
 	devfs_get(dir);
 	/*  Search for possible trailing component and ignore it  */
 	for (--namelen; (namelen > 0) && (name[namelen] != '/'); --namelen) ;

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

#8 lore raw
In reply to Jesse Millan
Quoted 3 lines · Jesse Millan

Your right, it could go either way. Arnd Bergmann had posted earlier and said that it would be better to make the change in the function itself. I took that advice.

I think what Mark was suggesting is to initialize the parameters at the start of the inner function (asn1_length_decode), which makes perfect sense to me, at least in this particular case.

What you should not do is initialize it in the calling function, because then you defeat gcc's ability to warn you about real uninitialized uses of the local variables in that function.

	Arnd <><

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