public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 5/7] kernel/cpu: Convert snprintf() to scnprintf()
@ 2022-09-26 18:52 Jules Irenge
  2022-09-28  9:35 ` Steven Price
  0 siblings, 1 reply; 2+ messages in thread
From: Jules Irenge @ 2022-09-26 18:52 UTC (permalink / raw)
  To: tglx
  Cc: steven.price, sathyanarayanan.kuppuswamy, linux-kernel,
	dave.hansen, ak, Jason

Coccinnelle reports a warning
Warning: Use scnprintf or sprintf
Adding to that, there has been a slow migration from snprintf to scnprintf.
This LWN article explains the rationale for this change
https: //lwn.net/Articles/69419/
Ie. snprintf() returns what *would* be the resulting length,
while scnprintf() returns the actual length.

Signed-off-by: Jules Irenge <jbi.octave@gmail.com>
---
 kernel/cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index bbad5e375d3b..f358d89c9580 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2512,7 +2512,7 @@ static ssize_t control_show(struct device *dev,
 {
 	const char *state = smt_states[cpu_smt_control];
 
-	return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
+	return scnprintf(buf, PAGE_SIZE - 2, "%s\n", state);
 }
 
 static ssize_t control_store(struct device *dev, struct device_attribute *attr,
@@ -2525,7 +2525,7 @@ static DEVICE_ATTR_RW(control);
 static ssize_t active_show(struct device *dev,
 			   struct device_attribute *attr, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
+	return scnprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
 }
 static DEVICE_ATTR_RO(active);
 
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 5/7] kernel/cpu: Convert snprintf() to scnprintf()
  2022-09-26 18:52 [PATCH 5/7] kernel/cpu: Convert snprintf() to scnprintf() Jules Irenge
@ 2022-09-28  9:35 ` Steven Price
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Price @ 2022-09-28  9:35 UTC (permalink / raw)
  To: Jules Irenge, tglx
  Cc: sathyanarayanan.kuppuswamy, linux-kernel, dave.hansen, ak, Jason

On 26/09/2022 19:52, Jules Irenge wrote:
> Coccinnelle reports a warning
> Warning: Use scnprintf or sprintf
> Adding to that, there has been a slow migration from snprintf to scnprintf.
> This LWN article explains the rationale for this change
> https: //lwn.net/Articles/69419/
> Ie. snprintf() returns what *would* be the resulting length,
> while scnprintf() returns the actual length.
> 
> Signed-off-by: Jules Irenge <jbi.octave@gmail.com>

Using sysfs_emit() would be even better in this case, although it's
slightly different because it outputs a maximum of PAGE_SIZE rather than
PAGE_SIZE - 2 bytes. But I can't see any reason for that "- 2" - it
appears to have been added when the sysfs entry was added by Thomas
Gleixner in commit 05736e4ac13c ("cpu/hotplug: Provide knobs to control
SMT") but I don't see any justification for it.

Steve

> ---
>  kernel/cpu.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/cpu.c b/kernel/cpu.c
> index bbad5e375d3b..f358d89c9580 100644
> --- a/kernel/cpu.c
> +++ b/kernel/cpu.c
> @@ -2512,7 +2512,7 @@ static ssize_t control_show(struct device *dev,
>  {
>  	const char *state = smt_states[cpu_smt_control];
>  
> -	return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
> +	return scnprintf(buf, PAGE_SIZE - 2, "%s\n", state);
>  }
>  
>  static ssize_t control_store(struct device *dev, struct device_attribute *attr,
> @@ -2525,7 +2525,7 @@ static DEVICE_ATTR_RW(control);
>  static ssize_t active_show(struct device *dev,
>  			   struct device_attribute *attr, char *buf)
>  {
> -	return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
> +	return scnprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
>  }
>  static DEVICE_ATTR_RO(active);
>  


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-28  9:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-26 18:52 [PATCH 5/7] kernel/cpu: Convert snprintf() to scnprintf() Jules Irenge
2022-09-28  9:35 ` Steven Price

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox