[PATCH v3 2/3] ACPI: battery: Use kstrtoul() over sscanf("%lu\n")
From: Rong Zhang
Date: Wed Jun 10 2026 - 16:11:42 EST
It is more preferred to use kstrto*() to parse a single number. The
function family properly returns an errno on error and is the correct
mechanism to parse data from sysfs.
The number base is set to 10 in order not to break the ABI.
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
Changes in v3:
- Address Sashiko's concerns on my last-minute changes:
- Set the number base to 10 in order not to break the ABI
- https://sashiko.dev/#/patchset/20260611-b4-acpi-battery-notification-v2-0-4e8ed651a151%40rong.moe
Changes in v2:
- New patch in series
- Since the series touches acpi_battery_alarm_store(), also convert
the use of sscanf("%lu\n") into the more preferred kstrtoul()
beforehand
---
drivers/acpi/battery.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
index 5f476c074c68..68b7a48357b4 100644
--- a/drivers/acpi/battery.c
+++ b/drivers/acpi/battery.c
@@ -675,9 +675,13 @@ static ssize_t acpi_battery_alarm_store(struct device *dev,
{
unsigned long x;
struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
+ int err;
- if (sscanf(buf, "%lu\n", &x) == 1)
- battery->alarm = x/1000;
+ err = kstrtoul(buf, 10, &x);
+ if (err)
+ return err;
+
+ battery->alarm = x / 1000;
if (acpi_battery_present(battery))
acpi_battery_set_alarm(battery);
return count;
--
2.53.0