Skip to content

Commit

Permalink
tools/power turbostat: Add checks for failure of fgets() and fscanf()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bwhacks authored and lenb committed Mar 21, 2019
1 parent 3316f99 commit 8173c33
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions tools/power/x86/turbostat/turbostat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,8 @@ int get_thread_siblings(struct cpu_topology *thiscpu)
filep = fopen_or_die(path, "r");
do {
offset -= BITMASK_SIZE;
fscanf(filep, "%lx%c", &map, &character);
if (fscanf(filep, "%lx%c", &map, &character) != 2)
err(1, "%s: failed to parse file", path);
for (shift = 0; shift < BITMASK_SIZE; shift++) {
if ((map >> shift) & 0x1) {
so = shift + offset;
Expand Down Expand Up @@ -3475,22 +3476,23 @@ dump_sysfs_cstate_config(void)
input = fopen(path, "r");
if (input == NULL)
continue;
fgets(name_buf, sizeof(name_buf), input);
if (!fgets(name_buf, sizeof(name_buf), input))
err(1, "%s: failed to read file", path);

/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
sp = strchr(name_buf, '-');
if (!sp)
sp = strchrnul(name_buf, '\n');
*sp = '\0';

fclose(input);

sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
base_cpu, state);
input = fopen(path, "r");
if (input == NULL)
continue;
fgets(desc, sizeof(desc), input);
if (!fgets(desc, sizeof(desc), input))
err(1, "%s: failed to read file", path);

fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
fclose(input);
Expand All @@ -3512,7 +3514,8 @@ dump_sysfs_pstate_config(void)
fprintf(outf, "NSFOD %s\n", path);
return;
}
fgets(driver_buf, sizeof(driver_buf), input);
if (!fgets(driver_buf, sizeof(driver_buf), input))
err(1, "%s: failed to read file", path);
fclose(input);

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

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

sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
input = fopen(path, "r");
if (input != NULL) {
fscanf(input, "%d", &turbo);
if (fscanf(input, "%d", &turbo) != 1)
err(1, "%s: failed to parse number from file", path);
fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
fclose(input);
}
Expand Down Expand Up @@ -5464,7 +5470,8 @@ void probe_sysfs(void)
input = fopen(path, "r");
if (input == NULL)
continue;
fgets(name_buf, sizeof(name_buf), input);
if (!fgets(name_buf, sizeof(name_buf), input))
err(1, "%s: failed to read file", path);

/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
sp = strchr(name_buf, '-');
Expand All @@ -5491,7 +5498,8 @@ void probe_sysfs(void)
input = fopen(path, "r");
if (input == NULL)
continue;
fgets(name_buf, sizeof(name_buf), input);
if (!fgets(name_buf, sizeof(name_buf), input))
err(1, "%s: failed to read file", path);
/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
sp = strchr(name_buf, '-');
if (!sp)
Expand Down

0 comments on commit 8173c33

Please sign in to comment.