platform/x86: wmi: Replace kmalloc + sprintf() with kasprintf()
PATCH 1/1 platform/x86: wmi: Replace kmalloc + sprintf() with kasprintf()
kasprintf() does the job of two: kmalloc() and sprintf(). Replace two calls with one.
--- drivers/platform/x86/wmi.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/platform/x86/wmi.c +1 -2
@@ -933,12 +933,11 @@ static int wmi_dev_probe(struct device *dev) goto probe_failure; } - buf =kmalloc (strlen (wdriver->driver.name) +5 ,GFP_KERNEL ); + buf =kasprintf (GFP_KERNEL ,"wmi/%s" , wdriver->driver.name);if (!buf) { ret = -ENOMEM ; goto probe_string_failure; } -sprintf (buf,"wmi/%s" , wdriver->driver.name); wblock->char_dev.minor =MISC_DYNAMIC_MINOR ; wblock->char_dev.name = buf; wblock->char_dev.fops = &wmi_fops;
Signature · 1 line
2.16.1
From: Andy Shevchenko
Quoted 4 lines
Sent: 16 February 2018 15:40
kasprintf()does the job of two:kmalloc()andsprintf(). Replace two calls with one.
...
drivers/platform/x86/wmi.c static int wmi_dev_probe(struct device *dev) · line 936
Quoted 2 lines
drivers/platform/x86/wmi.c +1
-1- buf =kmalloc (strlen (wdriver->driver.name) +5 ,GFP_KERNEL ); + buf =kasprintf (GFP_KERNEL ,"wmi/%s" , wdriver->driver.name);
...
Except that kasprintf() has no idea how long a buffer is needed. It might even do the printf twice just to get the length.
David
Quoted 8 lines · David Laight
Quoted 2 lines
kasprintf()does the job of two:kmalloc()andsprintf(). Replace two calls with one....
drivers/platform/x86/wmi.c static int wmi_dev_probe(struct device *dev) · line 936
Quoted 2 lines
drivers/platform/x86/wmi.c +1
-1- buf =kmalloc (strlen (wdriver->driver.name) +5 ,GFP_KERNEL ); + buf =kasprintf (GFP_KERNEL ,"wmi/%s" , wdriver->driver.name);...
Except that
kasprintf()has no idea how long a buffer is needed.
Hmm...
Quoted 1 line
It might even do the printf twice just to get the length.
It does exactly that. So what?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Darren Hart
Reviewed-byQuoted 5 lines · David Laight
From: Andy Shevchenko
Quoted 4 lines
Sent: 16 February 2018 15:40
kasprintf()does the job of two:kmalloc()andsprintf(). Replace two calls with one.
Quoted 7 lines
...
drivers/platform/x86/wmi.c static int wmi_dev_probe(struct device *dev) · line 936
Quoted 2 lines
drivers/platform/x86/wmi.c +1
-1- buf =kmalloc (strlen (wdriver->driver.name) +5 ,GFP_KERNEL ); + buf =kasprintf (GFP_KERNEL ,"wmi/%s" , wdriver->driver.name);...
Except that
kasprintf()has no idea how long a buffer is needed. It might even do the printf twice just to get the length.
Sure, but this is one-time and non-critical path. Unfortunately there isn't any guidance in Documentation here. Of the 520 or so instances of kasprintf usages in the kernel, device name or similar is a very common pattern. Eliminating manual counting of characters for buffer allocation seems like a good plan to me.
So unless we want to argue that all those use cases are wrong, this would appear to be at least common practice, if not best practice for this particular pattern.
Signature · 2 lines
Darren Hart VMware Open Source Technology Center