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>
This commit is contained in:
Ben Hutchings 2019-03-20 23:01:03 -04:00 коммит произвёл Len Brown
Родитель 3316f99a9f
Коммит 8173c33698
1 изменённых файлов: 18 добавлений и 10 удалений

Просмотреть файл

@ -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;
@ -3475,14 +3476,14 @@ 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",
@ -3490,7 +3491,8 @@ dump_sysfs_cstate_config(void)
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);
@ -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",
@ -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);
@ -3531,7 +3535,8 @@ 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);
}
@ -3539,7 +3544,8 @@ dump_sysfs_pstate_config(void)
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);
}
@ -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, '-');
@ -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)