diff mbox series

cpufreq: Update sscanf() to kstrtouint()

Message ID 20250519070938.931396-1-yubowen8@huawei.com
State New
Headers show
Series cpufreq: Update sscanf() to kstrtouint() | expand

Commit Message

Bowen Yu May 19, 2025, 7:09 a.m. UTC
In store_scaling_setspeed(), sscanf is still used to read to sysfs.
Newer kstrtox provide more features including overflow protection,
better errorhandling and allows for other systems of numeration. It
is therefore better to update sscanf() to kstrtouint().

Signed-off-by: Bowen Yu <yubowen8@huawei.com>
---
 drivers/cpufreq/cpufreq.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Rafael J. Wysocki May 21, 2025, 8:33 p.m. UTC | #1
On Mon, May 19, 2025 at 11:46 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Mon, 19 May 2025 15:09:38 +0800
> Bowen Yu <yubowen8@huawei.com> wrote:
>
> > In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> > Newer kstrtox provide more features including overflow protection,
> > better errorhandling and allows for other systems of numeration. It
> > is therefore better to update sscanf() to kstrtouint().
>
> This is a UAPI change.
> Since the value is a frequency there could easily be scripts
> that append Hz to the value.

Which would be incorrect because the value is in kHz.

They are not expected to do so in any case, though, so hopefully there
are none, in which case we don't want any to appear in the future.

> You're making them fail.

So if there are any, we'll get to know.
diff mbox series

Patch

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index be727da0be4d..0c842edd1a76 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -920,9 +920,9 @@  static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
 	if (!policy->governor || !policy->governor->store_setspeed)
 		return -EINVAL;
 
-	ret = sscanf(buf, "%u", &freq);
-	if (ret != 1)
-		return -EINVAL;
+	ret = kstrtouint(buf, 0, &freq);
+	if (ret)
+		return ret;
 
 	policy->governor->store_setspeed(policy, freq);