drivers/base/cpu: s*nprintf() usage fixes / cleanups
v2 cover drivers/base/cpu: s*nprintf() usage fixes / cleanups
Hi,
this is a respin of my previous patch [*].
Now the scnprintf() conversion is done only in the needed places, and the second patch cleans up the superfluous s*nprintf() usages.
Takashi
[*] https://lore.kernel.org/r/[email protected]
===
Takashi Iwai (2):
drivers/base/cpu: Use scnprintf() for avoiding potential buffer
overflow
drivers/base/cpu: Simplify s*nprintf() usages
drivers/base/cpu.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
Signature · 1 line
2.16.4
PATCH v2 1/2 drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow
Since snprintf() returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing with scnprintf().
--- drivers/base/cpu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/base/cpu.c +3 -3
@@ -258,13 +258,13 @@ static ssize_t print_cpus_offline(struct device *dev, buf[n++] =',' ;if (nr_cpu_ids == total_cpus-1 ) - n +=snprintf (&buf[n], len - n,"%u" , nr_cpu_ids); + n +=scnprintf (&buf[n], len - n,"%u" , nr_cpu_ids);else - n +=snprintf (&buf[n], len - n,"%u-%d" , + n +=scnprintf (&buf[n], len - n,"%u-%d" , nr_cpu_ids, total_cpus-1 ); } - n +=snprintf (&buf[n], len - n,"\n" ); + n +=scnprintf (&buf[n], len - n,"\n" );return n; } static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
Signature · 1 line
2.16.4
PATCH v2 2/2 drivers/base/cpu: Simplify s*nprintf() usages
In reply to Takashi IwaiUse the simpler sprintf() instead of snprintf() or scnprintf() in a single-shot sysfs output callbacks where you are very sure that it won't go over PAGE_SIZE buffer limit.
--- drivers/base/cpu.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/base/cpu.c +4 -9
@@ -231,8 +231,7 @@ static struct cpu_attr cpu_attrs[] = {static ssize_t print_cpus_kernel_max (struct device *dev,struct device_attribute *attr,char *buf) { -int n =snprintf (buf,PAGE_SIZE -2 ,"%d\n" ,NR_CPUS -1 ); -return n; +return sprintf (buf,"%d\n" ,NR_CPUS -1 ); } static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
@@ -272,7 +271,7 @@ static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);static ssize_t print_cpus_isolated (struct device *dev,struct device_attribute *attr,char *buf) { -int n =0 , len =PAGE_SIZE -2 ; +int n;cpumask_var_t isolated;if (!alloc_cpumask_var (&isolated,GFP_KERNEL ))
@@ -280,7 +279,7 @@ static ssize_t print_cpus_isolated(struct device *dev,cpumask_andnot (isolated, cpu_possible_mask,housekeeping_cpumask (HK_FLAG_DOMAIN )); - n =scnprintf (buf, len,"%*pbl\n" ,cpumask_pr_args (isolated)); + n =sprintf (buf,"%*pbl\n" ,cpumask_pr_args (isolated));free_cpumask_var (isolated);
@@ -292,11 +291,7 @@ static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);static ssize_t print_cpus_nohz_full (struct device *dev,struct device_attribute *attr,char *buf) { -int n =0 , len =PAGE_SIZE -2 ; - - n =scnprintf (buf, len,"%*pbl\n" ,cpumask_pr_args (tick_nohz_full_mask)); - -return n; +return sprintf (buf,"%*pbl\n" ,cpumask_pr_args (tick_nohz_full_mask)); } static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);#endif
Signature · 1 line
2.16.4
Greg Kroah-Hartman
maintainerQuoted 9 lines · Takashi Iwai
Hi,
this is a respin of my previous patch [*].
Now thescnprintf()conversion is done only in the needed places, and the second patch cleans up the superfluous s*nprintf() usages.Takashi
Thanks for these, much nicer, I'll go queue them up now.
greg k-h