Skip to content

Commit 07e5e81

Browse files
sumeet4linuxrafaeljw
authored andcommitted
powercap: Replace sprintf() with sysfs_emit() in sysfs show functions
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. Also, replace sprintf() with sysfs_emit() in show_constraint_name() and simplify the code by removing the redundant strlen() call since sysfs_emit() returns the length. Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com> Link: https://patch.msgid.link/20260111141237.12340-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 9ace475 commit 07e5e81

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

drivers/powercap/powercap_sys.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static ssize_t _attr##_show(struct device *dev, \
2727
\
2828
if (power_zone->ops->get_##_attr) { \
2929
if (!power_zone->ops->get_##_attr(power_zone, &value)) \
30-
len = sprintf(buf, "%lld\n", value); \
30+
len = sysfs_emit(buf, "%lld\n", value); \
3131
} \
3232
\
3333
return len; \
@@ -75,7 +75,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \
7575
pconst = &power_zone->constraints[id]; \
7676
if (pconst && pconst->ops && pconst->ops->get_##_attr) { \
7777
if (!pconst->ops->get_##_attr(power_zone, id, &value)) \
78-
len = sprintf(buf, "%lld\n", value); \
78+
len = sysfs_emit(buf, "%lld\n", value); \
7979
} \
8080
\
8181
return len; \
@@ -171,9 +171,8 @@ static ssize_t show_constraint_name(struct device *dev,
171171
if (pconst && pconst->ops && pconst->ops->get_name) {
172172
name = pconst->ops->get_name(power_zone, id);
173173
if (name) {
174-
sprintf(buf, "%.*s\n", POWERCAP_CONSTRAINT_NAME_LEN - 1,
175-
name);
176-
len = strlen(buf);
174+
len = sysfs_emit(buf, "%.*s\n",
175+
POWERCAP_CONSTRAINT_NAME_LEN - 1, name);
177176
}
178177
}
179178

@@ -350,7 +349,7 @@ static ssize_t name_show(struct device *dev,
350349
{
351350
struct powercap_zone *power_zone = to_powercap_zone(dev);
352351

353-
return sprintf(buf, "%s\n", power_zone->name);
352+
return sysfs_emit(buf, "%s\n", power_zone->name);
354353
}
355354

356355
static DEVICE_ATTR_RO(name);
@@ -438,7 +437,7 @@ static ssize_t enabled_show(struct device *dev,
438437
mode = false;
439438
}
440439

441-
return sprintf(buf, "%d\n", mode);
440+
return sysfs_emit(buf, "%d\n", mode);
442441
}
443442

444443
static ssize_t enabled_store(struct device *dev,

0 commit comments

Comments
 (0)