[PATCH] ACPI: Replace sprintf with sysfs_emit

Sumeet Pawnikar posted 1 patch 1 week, 5 days ago
drivers/acpi/device_sysfs.c | 20 ++++++++++----------
drivers/acpi/sysfs.c        | 30 +++++++++++++++---------------
2 files changed, 25 insertions(+), 25 deletions(-)
[PATCH] ACPI: Replace sprintf with sysfs_emit
Posted by Sumeet Pawnikar 1 week, 5 days ago
Replace all sprintf() calls with sysfs_emit() in sysfs show functions.
sysfs_emit() is preferred over sprintf() for formatting sysfs output
as it provides better bounds checking and prevents potential buffer
overflows.

Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com>
---
 drivers/acpi/device_sysfs.c | 20 ++++++++++----------
 drivers/acpi/sysfs.c        | 30 +++++++++++++++---------------
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c
index cd199fbe4dc9..a52dc35ff9ba 100644
--- a/drivers/acpi/device_sysfs.c
+++ b/drivers/acpi/device_sysfs.c
@@ -27,7 +27,7 @@ static ssize_t acpi_object_path(acpi_handle handle, char *buf)
 	if (result)
 		return result;
 
-	result = sprintf(buf, "%s\n", (char *)path.pointer);
+	result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
 	kfree(path.pointer);
 	return result;
 }
@@ -347,7 +347,7 @@ static ssize_t real_power_state_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%s\n", acpi_power_state_string(state));
+	return sysfs_emit(buf, "%s\n", acpi_power_state_string(state));
 }
 
 static DEVICE_ATTR_RO(real_power_state);
@@ -357,7 +357,7 @@ static ssize_t power_state_show(struct device *dev,
 {
 	struct acpi_device *adev = to_acpi_device(dev);
 
-	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
+	return sysfs_emit(buf, "%s\n", acpi_power_state_string(adev->power.state));
 }
 
 static DEVICE_ATTR_RO(power_state);
@@ -399,7 +399,7 @@ hid_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
 	struct acpi_device *acpi_dev = to_acpi_device(dev);
 
-	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
+	return sysfs_emit(buf, "%s\n", acpi_device_hid(acpi_dev));
 }
 static DEVICE_ATTR_RO(hid);
 
@@ -408,7 +408,7 @@ static ssize_t uid_show(struct device *dev,
 {
 	struct acpi_device *acpi_dev = to_acpi_device(dev);
 
-	return sprintf(buf, "%s\n", acpi_device_uid(acpi_dev));
+	return sysfs_emit(buf, "%s\n", acpi_device_uid(acpi_dev));
 }
 static DEVICE_ATTR_RO(uid);
 
@@ -418,9 +418,9 @@ static ssize_t adr_show(struct device *dev,
 	struct acpi_device *acpi_dev = to_acpi_device(dev);
 
 	if (acpi_dev->pnp.bus_address > U32_MAX)
-		return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
+		return sysfs_emit(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
 	else
-		return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
+		return sysfs_emit(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
 }
 static DEVICE_ATTR_RO(adr);
 
@@ -482,7 +482,7 @@ sun_show(struct device *dev, struct device_attribute *attr,
 	if (ACPI_FAILURE(status))
 		return -EIO;
 
-	return sprintf(buf, "%llu\n", sun);
+	return sysfs_emit(buf, "%llu\n", sun);
 }
 static DEVICE_ATTR_RO(sun);
 
@@ -498,7 +498,7 @@ hrv_show(struct device *dev, struct device_attribute *attr,
 	if (ACPI_FAILURE(status))
 		return -EIO;
 
-	return sprintf(buf, "%llu\n", hrv);
+	return sysfs_emit(buf, "%llu\n", hrv);
 }
 static DEVICE_ATTR_RO(hrv);
 
@@ -513,7 +513,7 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr,
 	if (ACPI_FAILURE(status))
 		return -EIO;
 
-	return sprintf(buf, "%llu\n", sta);
+	return sysfs_emit(buf, "%llu\n", sta);
 }
 static DEVICE_ATTR_RO(status);
 
diff --git a/drivers/acpi/sysfs.c b/drivers/acpi/sysfs.c
index e596224302f4..1bd2f555e673 100644
--- a/drivers/acpi/sysfs.c
+++ b/drivers/acpi/sysfs.c
@@ -687,7 +687,7 @@ static ssize_t counter_show(struct kobject *kobj,
 	    acpi_irq_not_handled;
 	all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
 	    acpi_gpe_count;
-	size = sprintf(buf, "%8u", all_counters[index].count);
+	size = sysfs_emit(buf, "%8u", all_counters[index].count);
 
 	/* "gpe_all" or "sci" */
 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
@@ -698,29 +698,29 @@ static ssize_t counter_show(struct kobject *kobj,
 		goto end;
 
 	if (status & ACPI_EVENT_FLAG_ENABLE_SET)
-		size += sprintf(buf + size, "  EN");
+		size += sysfs_emit_at(buf, size, "  EN");
 	else
-		size += sprintf(buf + size, "    ");
+		size += sysfs_emit_at(buf, size, "    ");
 	if (status & ACPI_EVENT_FLAG_STATUS_SET)
-		size += sprintf(buf + size, " STS");
+		size += sysfs_emit_at(buf, size, " STS");
 	else
-		size += sprintf(buf + size, "    ");
+		size += sysfs_emit_at(buf, size, "    ");
 
 	if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
-		size += sprintf(buf + size, " invalid     ");
+		size += sysfs_emit_at(buf, size, " invalid     ");
 	else if (status & ACPI_EVENT_FLAG_ENABLED)
-		size += sprintf(buf + size, " enabled     ");
+		size += sysfs_emit_at(buf, size, " enabled     ");
 	else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
-		size += sprintf(buf + size, " wake_enabled");
+		size += sysfs_emit_at(buf, size, " wake_enabled");
 	else
-		size += sprintf(buf + size, " disabled    ");
+		size += sysfs_emit_at(buf, size, " disabled    ");
 	if (status & ACPI_EVENT_FLAG_MASKED)
-		size += sprintf(buf + size, " masked  ");
+		size += sysfs_emit_at(buf, size, " masked  ");
 	else
-		size += sprintf(buf + size, " unmasked");
+		size += sysfs_emit_at(buf, size, " unmasked");
 
 end:
-	size += sprintf(buf + size, "\n");
+	size += sysfs_emit_at(buf, size, "\n");
 	return result ? result : size;
 }
 
@@ -937,7 +937,7 @@ static void __exit interrupt_stats_exit(void)
 
 static ssize_t pm_profile_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
+	return sysfs_emit(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
 }
 
 static const struct kobj_attribute pm_profile_attr = __ATTR_RO(pm_profile);
@@ -946,7 +946,7 @@ static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, c
 {
 	struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
 
-	return sprintf(buf, "%d\n", hotplug->enabled);
+	return sysfs_emit(buf, "%d\n", hotplug->enabled);
 }
 
 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1000,7 +1000,7 @@ void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
 static ssize_t force_remove_show(struct kobject *kobj,
 				 struct kobj_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%d\n", 0);
+	return sysfs_emit(buf, "%d\n", 0);
 }
 
 static ssize_t force_remove_store(struct kobject *kobj,
-- 
2.43.0
Re: [PATCH] ACPI: Replace sprintf with sysfs_emit
Posted by Rafael J. Wysocki 1 week, 3 days ago
On Mon, Jan 26, 2026 at 10:40 AM Sumeet Pawnikar <sumeet4linux@gmail.com> wrote:
>
> Replace all sprintf() calls with sysfs_emit() in sysfs show functions.
> sysfs_emit() is preferred over sprintf() for formatting sysfs output
> as it provides better bounds checking and prevents potential buffer
> overflows.
>
> Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com>
> ---
>  drivers/acpi/device_sysfs.c | 20 ++++++++++----------
>  drivers/acpi/sysfs.c        | 30 +++++++++++++++---------------
>  2 files changed, 25 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c
> index cd199fbe4dc9..a52dc35ff9ba 100644
> --- a/drivers/acpi/device_sysfs.c
> +++ b/drivers/acpi/device_sysfs.c
> @@ -27,7 +27,7 @@ static ssize_t acpi_object_path(acpi_handle handle, char *buf)
>         if (result)
>                 return result;
>
> -       result = sprintf(buf, "%s\n", (char *)path.pointer);
> +       result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
>         kfree(path.pointer);
>         return result;
>  }
> @@ -347,7 +347,7 @@ static ssize_t real_power_state_show(struct device *dev,
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%s\n", acpi_power_state_string(state));
> +       return sysfs_emit(buf, "%s\n", acpi_power_state_string(state));
>  }
>
>  static DEVICE_ATTR_RO(real_power_state);
> @@ -357,7 +357,7 @@ static ssize_t power_state_show(struct device *dev,
>  {
>         struct acpi_device *adev = to_acpi_device(dev);
>
> -       return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
> +       return sysfs_emit(buf, "%s\n", acpi_power_state_string(adev->power.state));
>  }
>
>  static DEVICE_ATTR_RO(power_state);
> @@ -399,7 +399,7 @@ hid_show(struct device *dev, struct device_attribute *attr, char *buf)
>  {
>         struct acpi_device *acpi_dev = to_acpi_device(dev);
>
> -       return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
> +       return sysfs_emit(buf, "%s\n", acpi_device_hid(acpi_dev));
>  }
>  static DEVICE_ATTR_RO(hid);
>
> @@ -408,7 +408,7 @@ static ssize_t uid_show(struct device *dev,
>  {
>         struct acpi_device *acpi_dev = to_acpi_device(dev);
>
> -       return sprintf(buf, "%s\n", acpi_device_uid(acpi_dev));
> +       return sysfs_emit(buf, "%s\n", acpi_device_uid(acpi_dev));
>  }
>  static DEVICE_ATTR_RO(uid);
>
> @@ -418,9 +418,9 @@ static ssize_t adr_show(struct device *dev,
>         struct acpi_device *acpi_dev = to_acpi_device(dev);
>
>         if (acpi_dev->pnp.bus_address > U32_MAX)
> -               return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
> +               return sysfs_emit(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
>         else
> -               return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
> +               return sysfs_emit(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
>  }
>  static DEVICE_ATTR_RO(adr);
>
> @@ -482,7 +482,7 @@ sun_show(struct device *dev, struct device_attribute *attr,
>         if (ACPI_FAILURE(status))
>                 return -EIO;
>
> -       return sprintf(buf, "%llu\n", sun);
> +       return sysfs_emit(buf, "%llu\n", sun);
>  }
>  static DEVICE_ATTR_RO(sun);
>
> @@ -498,7 +498,7 @@ hrv_show(struct device *dev, struct device_attribute *attr,
>         if (ACPI_FAILURE(status))
>                 return -EIO;
>
> -       return sprintf(buf, "%llu\n", hrv);
> +       return sysfs_emit(buf, "%llu\n", hrv);
>  }
>  static DEVICE_ATTR_RO(hrv);
>
> @@ -513,7 +513,7 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr,
>         if (ACPI_FAILURE(status))
>                 return -EIO;
>
> -       return sprintf(buf, "%llu\n", sta);
> +       return sysfs_emit(buf, "%llu\n", sta);
>  }
>  static DEVICE_ATTR_RO(status);
>
> diff --git a/drivers/acpi/sysfs.c b/drivers/acpi/sysfs.c
> index e596224302f4..1bd2f555e673 100644
> --- a/drivers/acpi/sysfs.c
> +++ b/drivers/acpi/sysfs.c
> @@ -687,7 +687,7 @@ static ssize_t counter_show(struct kobject *kobj,
>             acpi_irq_not_handled;
>         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
>             acpi_gpe_count;
> -       size = sprintf(buf, "%8u", all_counters[index].count);
> +       size = sysfs_emit(buf, "%8u", all_counters[index].count);
>
>         /* "gpe_all" or "sci" */
>         if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
> @@ -698,29 +698,29 @@ static ssize_t counter_show(struct kobject *kobj,
>                 goto end;
>
>         if (status & ACPI_EVENT_FLAG_ENABLE_SET)
> -               size += sprintf(buf + size, "  EN");
> +               size += sysfs_emit_at(buf, size, "  EN");
>         else
> -               size += sprintf(buf + size, "    ");
> +               size += sysfs_emit_at(buf, size, "    ");
>         if (status & ACPI_EVENT_FLAG_STATUS_SET)
> -               size += sprintf(buf + size, " STS");
> +               size += sysfs_emit_at(buf, size, " STS");
>         else
> -               size += sprintf(buf + size, "    ");
> +               size += sysfs_emit_at(buf, size, "    ");
>
>         if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
> -               size += sprintf(buf + size, " invalid     ");
> +               size += sysfs_emit_at(buf, size, " invalid     ");
>         else if (status & ACPI_EVENT_FLAG_ENABLED)
> -               size += sprintf(buf + size, " enabled     ");
> +               size += sysfs_emit_at(buf, size, " enabled     ");
>         else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
> -               size += sprintf(buf + size, " wake_enabled");
> +               size += sysfs_emit_at(buf, size, " wake_enabled");
>         else
> -               size += sprintf(buf + size, " disabled    ");
> +               size += sysfs_emit_at(buf, size, " disabled    ");
>         if (status & ACPI_EVENT_FLAG_MASKED)
> -               size += sprintf(buf + size, " masked  ");
> +               size += sysfs_emit_at(buf, size, " masked  ");
>         else
> -               size += sprintf(buf + size, " unmasked");
> +               size += sysfs_emit_at(buf, size, " unmasked");
>
>  end:
> -       size += sprintf(buf + size, "\n");
> +       size += sysfs_emit_at(buf, size, "\n");
>         return result ? result : size;
>  }
>
> @@ -937,7 +937,7 @@ static void __exit interrupt_stats_exit(void)
>
>  static ssize_t pm_profile_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
>  {
> -       return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
> +       return sysfs_emit(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
>  }
>
>  static const struct kobj_attribute pm_profile_attr = __ATTR_RO(pm_profile);
> @@ -946,7 +946,7 @@ static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, c
>  {
>         struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
>
> -       return sprintf(buf, "%d\n", hotplug->enabled);
> +       return sysfs_emit(buf, "%d\n", hotplug->enabled);
>  }
>
>  static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
> @@ -1000,7 +1000,7 @@ void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
>  static ssize_t force_remove_show(struct kobject *kobj,
>                                  struct kobj_attribute *attr, char *buf)
>  {
> -       return sprintf(buf, "%d\n", 0);
> +       return sysfs_emit(buf, "%d\n", 0);
>  }
>
>  static ssize_t force_remove_store(struct kobject *kobj,
> --

Applied as 6.20 material, thanks!