Skip to content

Commit 8173c33

Browse files
bwhackslenb
authored andcommitted
tools/power turbostat: Add checks for failure of fgets() and fscanf()
Most calls to fgets() and fscanf() are followed by error checks. Add an exit-on-error in the remaining cases. Signed-off-by: Ben Hutchings <ben@decadent.org.uk> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent 3316f99 commit 8173c33

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

‎tools/power/x86/turbostat/turbostat.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,8 @@ int get_thread_siblings(struct cpu_topology *thiscpu)
26432643
filep = fopen_or_die(path, "r");
26442644
do {
26452645
offset -= BITMASK_SIZE;
2646-
fscanf(filep, "%lx%c", &map, &character);
2646+
if (fscanf(filep, "%lx%c", &map, &character) != 2)
2647+
err(1, "%s: failed to parse file", path);
26472648
for (shift = 0; shift < BITMASK_SIZE; shift++) {
26482649
if ((map >> shift) & 0x1) {
26492650
so = shift + offset;
@@ -3475,22 +3476,23 @@ dump_sysfs_cstate_config(void)
34753476
input = fopen(path, "r");
34763477
if (input == NULL)
34773478
continue;
3478-
fgets(name_buf, sizeof(name_buf), input);
3479+
if (!fgets(name_buf, sizeof(name_buf), input))
3480+
err(1, "%s: failed to read file", path);
34793481

34803482
/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
34813483
sp = strchr(name_buf, '-');
34823484
if (!sp)
34833485
sp = strchrnul(name_buf, '\n');
34843486
*sp = '\0';
3485-
34863487
fclose(input);
34873488

34883489
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
34893490
base_cpu, state);
34903491
input = fopen(path, "r");
34913492
if (input == NULL)
34923493
continue;
3493-
fgets(desc, sizeof(desc), input);
3494+
if (!fgets(desc, sizeof(desc), input))
3495+
err(1, "%s: failed to read file", path);
34943496

34953497
fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
34963498
fclose(input);
@@ -3512,7 +3514,8 @@ dump_sysfs_pstate_config(void)
35123514
fprintf(outf, "NSFOD %s\n", path);
35133515
return;
35143516
}
3515-
fgets(driver_buf, sizeof(driver_buf), input);
3517+
if (!fgets(driver_buf, sizeof(driver_buf), input))
3518+
err(1, "%s: failed to read file", path);
35163519
fclose(input);
35173520

35183521
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
@@ -3522,7 +3525,8 @@ dump_sysfs_pstate_config(void)
35223525
fprintf(outf, "NSFOD %s\n", path);
35233526
return;
35243527
}
3525-
fgets(governor_buf, sizeof(governor_buf), input);
3528+
if (!fgets(governor_buf, sizeof(governor_buf), input))
3529+
err(1, "%s: failed to read file", path);
35263530
fclose(input);
35273531

35283532
fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
@@ -3531,15 +3535,17 @@ dump_sysfs_pstate_config(void)
35313535
sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
35323536
input = fopen(path, "r");
35333537
if (input != NULL) {
3534-
fscanf(input, "%d", &turbo);
3538+
if (fscanf(input, "%d", &turbo) != 1)
3539+
err(1, "%s: failed to parse number from file", path);
35353540
fprintf(outf, "cpufreq boost: %d\n", turbo);
35363541
fclose(input);
35373542
}
35383543

35393544
sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
35403545
input = fopen(path, "r");
35413546
if (input != NULL) {
3542-
fscanf(input, "%d", &turbo);
3547+
if (fscanf(input, "%d", &turbo) != 1)
3548+
err(1, "%s: failed to parse number from file", path);
35433549
fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
35443550
fclose(input);
35453551
}
@@ -5464,7 +5470,8 @@ void probe_sysfs(void)
54645470
input = fopen(path, "r");
54655471
if (input == NULL)
54665472
continue;
5467-
fgets(name_buf, sizeof(name_buf), input);
5473+
if (!fgets(name_buf, sizeof(name_buf), input))
5474+
err(1, "%s: failed to read file", path);
54685475

54695476
/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
54705477
sp = strchr(name_buf, '-');
@@ -5491,7 +5498,8 @@ void probe_sysfs(void)
54915498
input = fopen(path, "r");
54925499
if (input == NULL)
54935500
continue;
5494-
fgets(name_buf, sizeof(name_buf), input);
5501+
if (!fgets(name_buf, sizeof(name_buf), input))
5502+
err(1, "%s: failed to read file", path);
54955503
/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
54965504
sp = strchr(name_buf, '-');
54975505
if (!sp)

0 commit comments

Comments
 (0)