Skip to content

Commit 6c59f64

Browse files
fassegJiri Kosina
authored andcommitted
tools/thermal: tmon: fix for segfault
Fixes a segfault occurring when e.g. <TAB> is pressed multiple times in the ncurses tmon application. The segfault is caused by incrementing cur_thermal_record in the main function without checking if it's value reached NR_THERMAL_RECORD immediately. Since the boundary check only occurred in update_thermal_data a race condition existed, which lead to an attempted read beyond the last element of the trec array. The fix was implemented by moving the cur_thermal_record incrementation to the update_thermal_data function using a temporary variable on which the boundary condition is checked before updating cur_thread_record, so that the variable is never incremented beyond the trec array's boundary. It seems the segfault does not occur on every machine: On a HP EliteBook G4 the segfault happens, while it does not happen on a Thinkpad T540p. Signed-off-by: Frank Asseg <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent b903036 commit 6c59f64

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

tools/thermal/tmon/sysfs.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ int zone_instance_to_index(int zone_inst)
486486
int update_thermal_data()
487487
{
488488
int i;
489+
int next_thermal_record = cur_thermal_record + 1;
489490
char tz_name[256];
490491
static unsigned long samples;
491492

@@ -495,9 +496,9 @@ int update_thermal_data()
495496
}
496497

497498
/* circular buffer for keeping historic data */
498-
if (cur_thermal_record >= NR_THERMAL_RECORDS)
499-
cur_thermal_record = 0;
500-
gettimeofday(&trec[cur_thermal_record].tv, NULL);
499+
if (next_thermal_record >= NR_THERMAL_RECORDS)
500+
next_thermal_record = 0;
501+
gettimeofday(&trec[next_thermal_record].tv, NULL);
501502
if (tmon_log) {
502503
fprintf(tmon_log, "%lu ", ++samples);
503504
fprintf(tmon_log, "%3.1f ", p_param.t_target);
@@ -507,11 +508,12 @@ int update_thermal_data()
507508
snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE,
508509
ptdata.tzi[i].instance);
509510
sysfs_get_ulong(tz_name, "temp",
510-
&trec[cur_thermal_record].temp[i]);
511+
&trec[next_thermal_record].temp[i]);
511512
if (tmon_log)
512513
fprintf(tmon_log, "%lu ",
513-
trec[cur_thermal_record].temp[i]/1000);
514+
trec[next_thermal_record].temp[i] / 1000);
514515
}
516+
cur_thermal_record = next_thermal_record;
515517
for (i = 0; i < ptdata.nr_cooling_dev; i++) {
516518
char cdev_name[256];
517519
unsigned long val;

tools/thermal/tmon/tmon.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ int main(int argc, char **argv)
336336
show_data_w();
337337
show_cooling_device();
338338
}
339-
cur_thermal_record++;
340339
time_elapsed += ticktime;
341340
controller_handler(trec[0].temp[target_tz_index] / 1000,
342341
&yk);

0 commit comments

Comments
 (0)