ACPI: Replace sprintf with sysfs_emit
PATCH 1/1 ACPI: Replace sprintf with sysfs_emit
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 <[email protected]>
--- drivers/acpi/device_sysfs.c | 20 ++++++++++---------- drivers/acpi/sysfs.c | 30 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-)
drivers/acpi/device_sysfs.c +10 -10
@@ -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);
drivers/acpi/sysfs.c +15 -15
@@ -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,
Signature · 1 line
2.43.0
Quoted 173 lines · Sumeet Pawnikar
Replace all
sprintf()calls withsysfs_emit()in sysfs show functions.sysfs_emit()is preferred oversprintf()for formatting sysfs output as it provides better bounds checking and prevents potential buffer overflows.Signed-off-by Sumeet Pawnikar <[email protected]>--- drivers/acpi/device_sysfs.c | 20 ++++++++++---------- drivers/acpi/sysfs.c | 30 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-)drivers/acpi/device_sysfs.c +10
-10@@ -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);drivers/acpi/sysfs.c +14
-14@@ -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,
drivers/acpi/sysfs.c void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
Quoted 9 lines
drivers/acpi/sysfs.c +1
-2@@ -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!