Skip to contentSkip to conversation

vt: keyboard, fix uninitialized variables warning

PATCH Superseded Li Wang 4 participants 11 messages Mar 3, 2021 – Mar 4, 2021 TTY layer and serial drivers

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 vt: keyboard, fix uninitialized variables warning

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);
^~~~~~~~~~

---
 drivers/tty/vt/keyboard.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +2 -2
index 7763862..3e73d55 100644
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
 {
 	unsigned char kb_func;
 	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
 
 	if (get_user(kb_func, &user_kdgkb->kb_func))
 		return -EFAULT;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 18 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);
^~~~~~~~~~

---
 drivers/tty/vt/keyboard.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +0 -0
index 7763862..3e73d55 100644

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2056

Quoted 10 lines
drivers/tty/vt/keyboard.c +2 -2
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
 {
 	unsigned char kb_func;
 	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
 
 	if (get_user(kb_func, &user_kdgkb->kb_func))
 		return -EFAULT;

What compiler is providing these "warnings"?

Turns out it is impossible to hit, so this isn't actually fixing anything...

thanks,

greg k-h

author
#2 lore raw
Quoted 27 lines · Greg KH
Quoted 26 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

   return ret;
          ^~~
kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
   kfree(kbs);
   ^~~~~~~~~~
---
  drivers/tty/vt/keyboard.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +2 -2
index 7763862..3e73d55 100644
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  {
  	unsigned char kb_func;
  	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
 
Quoted 6 lines

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2056

Quoted 2 lines
drivers/tty/vt/keyboard.c +0 -0
  	if (get_user(kb_func, &user_kdgkb->kb_func))
  		return -EFAULT;

What compiler is providing these "warnings"?

Turns out it is impossible to hit, so this isn't actually fixing anything...

I tested it with gcc 8.2 for arm

for runtime codes view, indeed it is impossible to hit.

but for compiler view, gcc should give 'used uninitialized' warning, too.

thank for your reply,
LiWang.

Quoted 4 lines

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 32 lines · Li
Quoted 30 lines · Greg KH
Quoted 18 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

   return ret;
          ^~~
kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
   kfree(kbs);
   ^~~~~~~~~~
---
  drivers/tty/vt/keyboard.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +0 -0
index 7763862..3e73d55 100644

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2055

Quoted 9 lines
drivers/tty/vt/keyboard.c +2 -2
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  {
  	unsigned char kb_func;
  	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
  	if (get_user(kb_func, &user_kdgkb->kb_func))
  		return -EFAULT;

What compiler is providing these "warnings"?

Quoted 8 lines
Quoted 2 lines

Turns out it is impossible to hit, so this isn't actually fixing anything...

I tested it with gcc 8.2 for arm

for runtime codes view, indeed it is impossible to hit.

but for compiler view, gcc should give 'used uninitialized' warning, too.

Odd that no other compiler version does this right now, perhaps upgrade to a newer version of gcc? 8.2 is really old :(

thanks,

greg k-h

#4 lore raw
Quoted 35 lines · Greg KH
Quoted 34 lines · Li
Quoted 32 lines · Greg KH
Quoted 18 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

   return ret;
          ^~~
kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
   kfree(kbs);
   ^~~~~~~~~~
---
  drivers/tty/vt/keyboard.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +0 -0
index 7763862..3e73d55 100644

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2055

Quoted 9 lines
drivers/tty/vt/keyboard.c +2 -2
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  {
  	unsigned char kb_func;
  	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
  	if (get_user(kb_func, &user_kdgkb->kb_func))
  		return -EFAULT;

What compiler is providing these "warnings"?

Turns out it is impossible to hit, so this isn't actually fixing anything...

Quoted 8 lines
Quoted 5 lines

I tested it with gcc 8.2 for arm

for runtime codes view, indeed it is impossible to hit.

but for compiler view, gcc should give 'used uninitialized' warning, too.

Odd that no other compiler version does this right now, perhaps upgrade to a newer version of gcc? 8.2 is really old :(

But it's still supported. I think I can see why. We have a switch case without default, and probably that's how it makes that happen. So, the proper fix is to add default section AFAICT.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#5 lore raw
Quoted 37 lines · Andy Shevchenko
Quoted 36 lines · Greg KH
Quoted 35 lines · Li
Quoted 30 lines · Greg KH
Quoted 18 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

    return ret;
           ^~~
kernel-source/drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
    kfree(kbs);
    ^~~~~~~~~~
---
   drivers/tty/vt/keyboard.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +0 -0
index 7763862..3e73d55 100644

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2055

Quoted 9 lines
drivers/tty/vt/keyboard.c +2 -2
@@ -2049,8 +2049,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
   {
   	unsigned char kb_func;
   	unsigned long flags;
-	char *kbs;
-	int ret;
+	char *kbs = NULL;
+	int ret = -EINVAL;
   	if (get_user(kb_func, &user_kdgkb->kb_func))
   		return -EFAULT;

What compiler is providing these "warnings"?

Quoted 2 lines

Turns out it is impossible to hit, so this isn't actually fixing anything...

I tested it with gcc 8.2 for arm

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

for runtime codes view, indeed it is impossible to hit.

but for compiler view, gcc should give 'used uninitialized' warning, too.

Odd that no other compiler version does this right now, perhaps upgrade to a newer version of gcc? 8.2 is really old :(

But it's still supported. I think I can see why. We have a switch case without default, and probably that's how it makes that happen. So, the proper fix is to add default section AFAICT.

Hi all,

I tried the latest gcc and linux, these warnings disappear. it is not gcc issue, the issue is that I do not use the latest linux.

linux disables the 'used uninitialized' warning in the below commit, but mentioned by description, if want to debug kernel warning, use option to open. and 'great ... code ... never confuses the compiler'

I will according to Andy's suggest, send V2.
Thanks,
LiWang.

=====

From 78a5255ffb6a1af189a83e493d916ba1c54d8c75 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Sat, 9 May 2020 13:57:10 -0700
Subject: [PATCH] Stop the ad-hoc games with -Wno-maybe-initialized

We have some rather random rules about when we accept the "maybe-initialized" warnings, and when we don't.

For example, we consider it unreliable for gcc versions < 4.9, but also if -O3 is enabled, or if optimizing for size.  And then various kernel config options disabled it, because they know that they trigger that warning by confusing gcc sufficiently (ie PROFILE_ALL_BRANCHES).

And now gcc-10 seems to be introducing a lot of those warnings too, so it falls under the same heading as 4.9 did.

At the same time, we have a very straightforward way to _enable_ that warning when wanted: use "W=2" to enable more warnings.

So stop playing these ad-hoc games, and just disable that warning by default, with the known and straight-forward "if you want to work on the extra compiler warnings, use W=123".

Would it be great to have code that is always so obvious that it never confuses the compiler whether a variable is used initialized or not? Yes, it would.  In a perfect world, the compilers would be smarter, and our source code would be simpler.

That's currently not the world we live in, though.

author
#6 lore raw

PATCH v2 1/1 vt: keyboard, fix uninitialized variables warning

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);
^~~~~~~~~~

---
 drivers/tty/vt/keyboard.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/vt/keyboard.c +2 -0
index 7763862..62f1ecb 100644
@@ -2090,6 +2090,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
 
 		ret = 0;
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	kfree(kbs);
Signature · 1 line
2.7.4
maintainer
#7 lore raw
Quoted 18 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

   return ret;
          ^~~
drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
   kfree(kbs);
   ^~~~~~~~~~
---
  drivers/tty/vt/keyboard.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/tty/vt/keyboard.c +0 -0
index 7763862..62f1ecb 100644

drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2094

Quoted 5 lines
drivers/tty/vt/keyboard.c +2 -0
@@ -2090,6 +2090,8 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
 
  		ret = 0;
  		break;
+	default:
+		return -EINVAL;

I am not biased whether to add it or not, but I would return -ENOIOCTLCMD if we do.

Quoted 4 lines
drivers/tty/vt/keyboard.c +0 -0
  	}
 
  	kfree(kbs);
Signature · 2 lines
js
suse labs
maintainer
#8 lore raw
In reply to Li Wang
Quoted 7 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);
^~~~~~~~~~

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- You did not specify a description of why the patch is needed, or possibly, any description at all, in the email body. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/SubmittingPatches for what is needed in order to properly describe the change.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#9 lore raw
In reply to Li Wang
Quoted 6 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);

Let me add one more comment and summarize altogether:
- Jiri wants you to have different error code
- Greg (and I noticed that as well) wants you to add a proper commit message, and not just some output of some tool w/o context
- I want you to send a new version w/o chaining to the previous thread, so start new (email) thread every time you send a new version

Waiting for v3 in a separate email thread, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Li Wang

drivers/tty/vt/keyboard.c: In function 'vt_do_kdgkb_ioctl':
drivers/tty/vt/keyboard.c: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]

  return ret;
         ^~~

drivers/tty/vt/keyboard.c: warning: 'kbs' may be used uninitialized in this function [-Wmaybe-uninitialized]
kfree(kbs);

Quoted 8 lines

Let me add one more comment and summarize altogether:
- Jiri wants you to have different error code
- Greg (and I noticed that as well) wants you to add a proper commit message, and not just some output of some tool w/o context
- I want you to send a new version w/o chaining to the previous thread, so start new (email) thread every time you send a new version

Waiting for v3 in a separate email thread, thanks!

Last, but not least: add a changelog after the cutter '---' line, so people will know what have you done on the transition from vX to vX+1.

Signature · 2 lines
With Best Regards,
Andy Shevchenko