Skip to contentSkip to conversation

drivers/char/random.c: fix uninitialized value warning

PATCH In review Constantine Shulyupin 3 participants 3 messages Jul 18, 2018 Random number driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/char/random.c: fix uninitialized value warning

Local variable t should be initialized by arch_get_random_int. Actually on failure of arch_get_random_int, value is not used. So, just keep the build clean with less warnings.

warning:
drivers/char/random.c: In function ‘write_pool.constprop’:
drivers/char/random.c:1912:11: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]

---
 drivers/char/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
index 283fe390e878..8e51846d0673 100644
@@ -1896,7 +1896,7 @@ static int
 write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
 {
 	size_t bytes;
-	__u32 t, buf[16];
+	__u32 t = 0, buf[16];
 	const char __user *p = buffer;
 
 	while (count > 0) {
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 9 lines · Constantine Shulyupin

Local variable t should be initialized by arch_get_random_int. Actually on failure of arch_get_random_int, value is not used. So, just keep the build clean with less warnings.

warning:
drivers/char/random.c: In function ‘write_pool.constprop’:
drivers/char/random.c:1912:11: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]

There was a more significant problem which Arnd pointed out, which is I had reversed the sense of the test in arch_get_random_int(). I've since fixed this, and the current random tree is clean of warnings.

- Ted

#2 lore raw
In reply to Constantine Shulyupin
Quoted 9 lines · Constantine Shulyupin

Local variable t should be initialized by arch_get_random_int. Actually on failure of arch_get_random_int, value is not used. So, just keep the build clean with less warnings.

warning:
drivers/char/random.c: In function ‘write_pool.constprop’:
drivers/char/random.c:1912:11: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]

You need to be much more careful with fixes like this one. As Ted explained, the warning was about a real bug here, and adding an incorrect initialization just made it harder to find that bug.

There are many cases in which the compiler does have a false-positive or a false-negative -Wmaybe-uninitialized warning, but in general you should assume that the compiler is correct. If you add a fake initialization because you think the compiler is wrong, it's best to link to the gcc bugzilla describing
that gcc bug, or open a new bug report if it still happens in the latest compiler
version and hasn't been reported before.

Arnd