Skip to content

Commit 7cb776b

Browse files
sumeet4linuxrafaeljw
authored andcommitted
thermal: Replace sprintf() with sysfs_emit() for sysfs show functions
Replace all sprintf() calls with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred over sprintf() for formatting sysfs output as they provide better bounds checking and prevent potential buffer overflows. Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com> Link: https://patch.msgid.link/20260110092851.9078-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 1d97b8e commit 7cb776b

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

drivers/thermal/thermal_hwmon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
6363
if (ret)
6464
return ret;
6565

66-
return sprintf(buf, "%d\n", temperature);
66+
return sysfs_emit(buf, "%d\n", temperature);
6767
}
6868

6969
static ssize_t
@@ -84,7 +84,7 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
8484
if (ret)
8585
return ret;
8686

87-
return sprintf(buf, "%d\n", temperature);
87+
return sysfs_emit(buf, "%d\n", temperature);
8888
}
8989

9090

drivers/thermal/thermal_sysfs.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type_show(struct device *dev, struct device_attribute *attr, char *buf)
2929
{
3030
struct thermal_zone_device *tz = to_thermal_zone(dev);
3131

32-
return sprintf(buf, "%s\n", tz->type);
32+
return sysfs_emit(buf, "%s\n", tz->type);
3333
}
3434

3535
static ssize_t
@@ -41,7 +41,7 @@ temp_show(struct device *dev, struct device_attribute *attr, char *buf)
4141
ret = thermal_zone_get_temp(tz, &temperature);
4242

4343
if (!ret)
44-
return sprintf(buf, "%d\n", temperature);
44+
return sysfs_emit(buf, "%d\n", temperature);
4545

4646
if (ret == -EAGAIN)
4747
return -ENODATA;
@@ -57,9 +57,9 @@ mode_show(struct device *dev, struct device_attribute *attr, char *buf)
5757
guard(thermal_zone)(tz);
5858

5959
if (tz->mode == THERMAL_DEVICE_ENABLED)
60-
return sprintf(buf, "enabled\n");
60+
return sysfs_emit(buf, "enabled\n");
6161

62-
return sprintf(buf, "disabled\n");
62+
return sysfs_emit(buf, "disabled\n");
6363
}
6464

6565
static ssize_t
@@ -97,7 +97,7 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr,
9797
{
9898
struct thermal_trip *trip = thermal_trip_of_attr(attr, type);
9999

100-
return sprintf(buf, "%s\n", thermal_trip_type_name(trip->type));
100+
return sysfs_emit(buf, "%s\n", thermal_trip_type_name(trip->type));
101101
}
102102

103103
static ssize_t
@@ -142,7 +142,7 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr,
142142
{
143143
struct thermal_trip *trip = thermal_trip_of_attr(attr, temp);
144144

145-
return sprintf(buf, "%d\n", READ_ONCE(trip->temperature));
145+
return sysfs_emit(buf, "%d\n", READ_ONCE(trip->temperature));
146146
}
147147

148148
static ssize_t
@@ -188,7 +188,7 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
188188
{
189189
struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst);
190190

191-
return sprintf(buf, "%d\n", READ_ONCE(trip->hysteresis));
191+
return sysfs_emit(buf, "%d\n", READ_ONCE(trip->hysteresis));
192192
}
193193

194194
static ssize_t
@@ -213,7 +213,7 @@ policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
213213
{
214214
struct thermal_zone_device *tz = to_thermal_zone(dev);
215215

216-
return sprintf(buf, "%s\n", tz->governor->name);
216+
return sysfs_emit(buf, "%s\n", tz->governor->name);
217217
}
218218

219219
static ssize_t
@@ -260,7 +260,7 @@ sustainable_power_show(struct device *dev, struct device_attribute *devattr,
260260
struct thermal_zone_device *tz = to_thermal_zone(dev);
261261

262262
if (tz->tzp)
263-
return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
263+
return sysfs_emit(buf, "%u\n", tz->tzp->sustainable_power);
264264
else
265265
return -EIO;
266266
}
@@ -291,7 +291,7 @@ sustainable_power_store(struct device *dev, struct device_attribute *devattr,
291291
struct thermal_zone_device *tz = to_thermal_zone(dev); \
292292
\
293293
if (tz->tzp) \
294-
return sprintf(buf, "%d\n", tz->tzp->name); \
294+
return sysfs_emit(buf, "%d\n", tz->tzp->name); \
295295
else \
296296
return -EIO; \
297297
} \
@@ -505,15 +505,15 @@ cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
505505
{
506506
struct thermal_cooling_device *cdev = to_cooling_device(dev);
507507

508-
return sprintf(buf, "%s\n", cdev->type);
508+
return sysfs_emit(buf, "%s\n", cdev->type);
509509
}
510510

511511
static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
512512
char *buf)
513513
{
514514
struct thermal_cooling_device *cdev = to_cooling_device(dev);
515515

516-
return sprintf(buf, "%ld\n", cdev->max_state);
516+
return sysfs_emit(buf, "%ld\n", cdev->max_state);
517517
}
518518

519519
static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
@@ -526,7 +526,7 @@ static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
526526
ret = cdev->ops->get_cur_state(cdev, &state);
527527
if (ret)
528528
return ret;
529-
return sprintf(buf, "%ld\n", state);
529+
return sysfs_emit(buf, "%ld\n", state);
530530
}
531531

532532
static ssize_t
@@ -638,7 +638,7 @@ static ssize_t total_trans_show(struct device *dev,
638638
return 0;
639639

640640
spin_lock(&stats->lock);
641-
ret = sprintf(buf, "%u\n", stats->total_trans);
641+
ret = sysfs_emit(buf, "%u\n", stats->total_trans);
642642
spin_unlock(&stats->lock);
643643

644644
return ret;
@@ -664,8 +664,8 @@ time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
664664
update_time_in_state(stats);
665665

666666
for (i = 0; i <= cdev->max_state; i++) {
667-
len += sprintf(buf + len, "state%u\t%llu\n", i,
668-
ktime_to_ms(stats->time_in_state[i]));
667+
len += sysfs_emit_at(buf, len, "state%u\t%llu\n", i,
668+
ktime_to_ms(stats->time_in_state[i]));
669669
}
670670
spin_unlock(&stats->lock);
671671

@@ -846,7 +846,7 @@ trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
846846

847847
instance = container_of(attr, struct thermal_instance, attr);
848848

849-
return sprintf(buf, "%d\n", thermal_zone_trip_id(tz, instance->trip));
849+
return sysfs_emit(buf, "%d\n", thermal_zone_trip_id(tz, instance->trip));
850850
}
851851

852852
ssize_t
@@ -856,7 +856,7 @@ weight_show(struct device *dev, struct device_attribute *attr, char *buf)
856856

857857
instance = container_of(attr, struct thermal_instance, weight_attr);
858858

859-
return sprintf(buf, "%d\n", instance->weight);
859+
return sysfs_emit(buf, "%d\n", instance->weight);
860860
}
861861

862862
ssize_t weight_store(struct device *dev, struct device_attribute *attr,

0 commit comments

Comments
 (0)