Skip to content

Commit 785632d

Browse files
sumeet4linuxrafaeljw
authored andcommitted
ACPI: sysfs: Replace sprintf() with sysfs_emit()
Replace all sprintf() calls with sysfs_emit() in sysfs show functions. sysfs_emit() is preferred to sprintf() for formatting sysfs output as it provides better bounds checking and prevents potential buffer overflows. Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com> [ rjw: Subject and changelog edits ] Link: https://patch.msgid.link/20260126093949.8910-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 2aa1e46 commit 785632d

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

drivers/acpi/device_sysfs.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static ssize_t acpi_object_path(acpi_handle handle, char *buf)
2727
if (result)
2828
return result;
2929

30-
result = sprintf(buf, "%s\n", (char *)path.pointer);
30+
result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
3131
kfree(path.pointer);
3232
return result;
3333
}
@@ -347,7 +347,7 @@ static ssize_t real_power_state_show(struct device *dev,
347347
if (ret)
348348
return ret;
349349

350-
return sprintf(buf, "%s\n", acpi_power_state_string(state));
350+
return sysfs_emit(buf, "%s\n", acpi_power_state_string(state));
351351
}
352352

353353
static DEVICE_ATTR_RO(real_power_state);
@@ -357,7 +357,7 @@ static ssize_t power_state_show(struct device *dev,
357357
{
358358
struct acpi_device *adev = to_acpi_device(dev);
359359

360-
return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
360+
return sysfs_emit(buf, "%s\n", acpi_power_state_string(adev->power.state));
361361
}
362362

363363
static DEVICE_ATTR_RO(power_state);
@@ -399,7 +399,7 @@ hid_show(struct device *dev, struct device_attribute *attr, char *buf)
399399
{
400400
struct acpi_device *acpi_dev = to_acpi_device(dev);
401401

402-
return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
402+
return sysfs_emit(buf, "%s\n", acpi_device_hid(acpi_dev));
403403
}
404404
static DEVICE_ATTR_RO(hid);
405405

@@ -435,7 +435,7 @@ static ssize_t uid_show(struct device *dev,
435435
{
436436
struct acpi_device *acpi_dev = to_acpi_device(dev);
437437

438-
return sprintf(buf, "%s\n", acpi_device_uid(acpi_dev));
438+
return sysfs_emit(buf, "%s\n", acpi_device_uid(acpi_dev));
439439
}
440440
static DEVICE_ATTR_RO(uid);
441441

@@ -445,9 +445,9 @@ static ssize_t adr_show(struct device *dev,
445445
struct acpi_device *acpi_dev = to_acpi_device(dev);
446446

447447
if (acpi_dev->pnp.bus_address > U32_MAX)
448-
return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
448+
return sysfs_emit(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
449449
else
450-
return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
450+
return sysfs_emit(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
451451
}
452452
static DEVICE_ATTR_RO(adr);
453453

@@ -509,7 +509,7 @@ sun_show(struct device *dev, struct device_attribute *attr,
509509
if (ACPI_FAILURE(status))
510510
return -EIO;
511511

512-
return sprintf(buf, "%llu\n", sun);
512+
return sysfs_emit(buf, "%llu\n", sun);
513513
}
514514
static DEVICE_ATTR_RO(sun);
515515

@@ -525,7 +525,7 @@ hrv_show(struct device *dev, struct device_attribute *attr,
525525
if (ACPI_FAILURE(status))
526526
return -EIO;
527527

528-
return sprintf(buf, "%llu\n", hrv);
528+
return sysfs_emit(buf, "%llu\n", hrv);
529529
}
530530
static DEVICE_ATTR_RO(hrv);
531531

@@ -540,7 +540,7 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr,
540540
if (ACPI_FAILURE(status))
541541
return -EIO;
542542

543-
return sprintf(buf, "%llu\n", sta);
543+
return sysfs_emit(buf, "%llu\n", sta);
544544
}
545545
static DEVICE_ATTR_RO(status);
546546

drivers/acpi/sysfs.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ static ssize_t counter_show(struct kobject *kobj,
687687
acpi_irq_not_handled;
688688
all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
689689
acpi_gpe_count;
690-
size = sprintf(buf, "%8u", all_counters[index].count);
690+
size = sysfs_emit(buf, "%8u", all_counters[index].count);
691691

692692
/* "gpe_all" or "sci" */
693693
if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
@@ -698,29 +698,29 @@ static ssize_t counter_show(struct kobject *kobj,
698698
goto end;
699699

700700
if (status & ACPI_EVENT_FLAG_ENABLE_SET)
701-
size += sprintf(buf + size, " EN");
701+
size += sysfs_emit_at(buf, size, " EN");
702702
else
703-
size += sprintf(buf + size, " ");
703+
size += sysfs_emit_at(buf, size, " ");
704704
if (status & ACPI_EVENT_FLAG_STATUS_SET)
705-
size += sprintf(buf + size, " STS");
705+
size += sysfs_emit_at(buf, size, " STS");
706706
else
707-
size += sprintf(buf + size, " ");
707+
size += sysfs_emit_at(buf, size, " ");
708708

709709
if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
710-
size += sprintf(buf + size, " invalid ");
710+
size += sysfs_emit_at(buf, size, " invalid ");
711711
else if (status & ACPI_EVENT_FLAG_ENABLED)
712-
size += sprintf(buf + size, " enabled ");
712+
size += sysfs_emit_at(buf, size, " enabled ");
713713
else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
714-
size += sprintf(buf + size, " wake_enabled");
714+
size += sysfs_emit_at(buf, size, " wake_enabled");
715715
else
716-
size += sprintf(buf + size, " disabled ");
716+
size += sysfs_emit_at(buf, size, " disabled ");
717717
if (status & ACPI_EVENT_FLAG_MASKED)
718-
size += sprintf(buf + size, " masked ");
718+
size += sysfs_emit_at(buf, size, " masked ");
719719
else
720-
size += sprintf(buf + size, " unmasked");
720+
size += sysfs_emit_at(buf, size, " unmasked");
721721

722722
end:
723-
size += sprintf(buf + size, "\n");
723+
size += sysfs_emit_at(buf, size, "\n");
724724
return result ? result : size;
725725
}
726726

@@ -937,7 +937,7 @@ static void __exit interrupt_stats_exit(void)
937937

938938
static ssize_t pm_profile_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
939939
{
940-
return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
940+
return sysfs_emit(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
941941
}
942942

943943
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
946946
{
947947
struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
948948

949-
return sprintf(buf, "%d\n", hotplug->enabled);
949+
return sysfs_emit(buf, "%d\n", hotplug->enabled);
950950
}
951951

952952
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,
10001000
static ssize_t force_remove_show(struct kobject *kobj,
10011001
struct kobj_attribute *attr, char *buf)
10021002
{
1003-
return sprintf(buf, "%d\n", 0);
1003+
return sysfs_emit(buf, "%d\n", 0);
10041004
}
10051005

10061006
static ssize_t force_remove_store(struct kobject *kobj,

0 commit comments

Comments
 (0)