Skip to contentSkip to conversation

printk: avoid -Wsometimes-uninitialized warning

PATCH In review Arnd Bergmann 3 participants 4 messages Sep 28, 2021 – Oct 4, 2021

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

PATCH v2 1/1 printk: avoid -Wsometimes-uninitialized warning

From: Arnd Bergmann <[email protected]>

clang notices that the pi_get_entry() function would use uninitialized data if it was called with a non-NULL module pointer on a kernel that does not support modules:

kernel/printk/index.c:32:6: error: variable 'nr_entries' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

        if (!mod) {
            ^~~~

kernel/printk/index.c:38:13: note: uninitialized use occurs here
if (pos >= nr_entries)
^~~~~~~~~~
kernel/printk/index.c:32:2: note: remove the 'if' if its condition is always true

        if (!mod) {

Rework the condition to make it clear to the compiler that we are always in the second case. Unfortunately the #ifdef is still required as the definition of 'struct module' is hidden when modules are disabled.

Fixes 337015573718 ("printk: Userspace format indexing support")

--
v2: use a simpler trick of having an 'else' in the #ifdef
block, as Steven suggested.

---
 kernel/printk/index.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/printk/index.c +2 -3
index d3709408debe..43b45a916ff6 100644
@@ -26,10 +26,9 @@ static struct pi_entry *pi_get_entry(const struct module *mod, loff_t pos)
 	if (mod) {
 		entries = mod->printk_index_start;
 		nr_entries = mod->printk_index_size;
-	}
+	} else 
 #endif
-
-	if (!mod) {
+	{
 		/* vmlinux, comes from linker symbols */
 		entries = __start_printk_index;
 		nr_entries = __stop_printk_index - __start_printk_index;
Signature · 1 line
2.29.2
reviewer
#1 lore raw
Quoted 13 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang notices that the pi_get_entry() function would use uninitialized data if it was called with a non-NULL module pointer on a kernel that does not support modules:

kernel/printk/index.c:32:6: error: variable 'nr_entries' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

       if (!mod) {
           ^~~~

kernel/printk/index.c:38:13: note: uninitialized use occurs here
if (pos >= nr_entries)
^~~~~~~~~~
kernel/printk/index.c:32:2: note: remove the 'if' if its condition is always true

Quoted 7 lines
       if (!mod) {

Rework the condition to make it clear to the compiler that we are always in the second case. Unfortunately the #ifdef is still required as the definition of 'struct module' is hidden when modules are disabled.

Fixes 337015573718 ("printk: Userspace format indexing support")

This changelog should make it clear that this is theoretical and will never actually happen, which is salient information for people who are considering whether it should go in stable or similar.

Quoted 29 lines

--
v2: use a simpler trick of having an 'else' in the #ifdef
block, as Steven suggested.
---
kernel/printk/index.c | 5 ++---

1 file changed, 2 insertions(+), 3 deletions(-)
kernel/printk/index.c +2 -3
index d3709408debe..43b45a916ff6 100644
@@ -26,10 +26,9 @@ static struct pi_entry *pi_get_entry(const struct module *mod, loff_t pos)
 	if (mod) {
 		entries = mod->printk_index_start;
 		nr_entries = mod->printk_index_size;
-	}
+	} else
 #endif
-
-	if (!mod) {
+	{
 		/* vmlinux, comes from linker symbols */
 		entries = __start_printk_index;
 		nr_entries = __stop_printk_index - __start_printk_index;
Signature · 1 line
2.29.2
maintainer
#2 lore raw
Quoted 20 lines · Chris Down
Quoted 19 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang notices that the pi_get_entry() function would use uninitialized data if it was called with a non-NULL module pointer on a kernel that does not support modules:

kernel/printk/index.c:32:6: error: variable 'nr_entries' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

       if (!mod) {
           ^~~~

kernel/printk/index.c:38:13: note: uninitialized use occurs here
if (pos >= nr_entries)
^~~~~~~~~~
kernel/printk/index.c:32:2: note: remove the 'if' if its condition is always true

       if (!mod) {

Rework the condition to make it clear to the compiler that we are always in the second case. Unfortunately the #ifdef is still required as the definition of 'struct module' is hidden when modules are disabled.

Quoted 5 lines
Quoted 1 line
Fixes 337015573718 ("printk: Userspace format indexing support")

This changelog should make it clear that this is theoretical and will never actually happen, which is salient information for people who are considering whether it should go in stable or similar.

IMHO, the sentence "Rework the condition to make it clear that this is theoretical and will never actually happen" is rather clear.

Well, I am not a native speaker.

Anyway, I have pushed the patch into printk/linux.git, branch for-5.16.

Best Regards,
Petr

reviewer Acked-by
#3 lore raw
Quoted 21 lines · Petr Mladek
Quoted 20 lines · Chris Down
Quoted 19 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang notices that the pi_get_entry() function would use uninitialized data if it was called with a non-NULL module pointer on a kernel that does not support modules:

kernel/printk/index.c:32:6: error: variable 'nr_entries' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

       if (!mod) {
           ^~~~

kernel/printk/index.c:38:13: note: uninitialized use occurs here
if (pos >= nr_entries)
^~~~~~~~~~
kernel/printk/index.c:32:2: note: remove the 'if' if its condition is always true

       if (!mod) {

Rework the condition to make it clear to the compiler that we are always in the second case. Unfortunately the #ifdef is still required as the definition of 'struct module' is hidden when modules are disabled.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Fixes 337015573718 ("printk: Userspace format indexing support")

This changelog should make it clear that this is theoretical and will never actually happen, which is salient information for people who are considering whether it should go in stable or similar.

IMHO, the sentence "Rework the condition to make it clear that this is theoretical and will never actually happen" is rather clear.

Sounds good to me, thanks!

I guess it's unneeded at this point, but feel free to add:

Quoted 7 lines

Well, I am not a native speaker.

Anyway, I have pushed the patch into printk/linux.git, branch for-5.16.

Best Regards,
Petr