diff options
author | Jaroslav Škarvada <jskarvad@redhat.com> | 2015-05-12 17:19:34 +0200 |
---|---|---|
committer | Alexandra Yates <alexandra.yates@linux.intel.com> | 2015-09-17 17:10:09 -0700 |
commit | 981f1c6a5388e604aaf6f9cf514990282f8da379 (patch) | |
tree | 6f108270548676b2ba00a2d24b8e94a867c4fb99 | |
parent | dd0c258346f2e113a0da5a82791754b47d71c1f1 (diff) | |
download | powertop-2.0-981f1c6a5388e604aaf6f9cf514990282f8da379.tar.gz |
Use PATH_MAX from limits.h and snprintf insteaf of sprintf
Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
35 files changed, 228 insertions, 194 deletions
diff --git a/src/calibrate/calibrate.cpp b/src/calibrate/calibrate.cpp index 98abfe9..eacaeec 100644 --- a/src/calibrate/calibrate.cpp +++ b/src/calibrate/calibrate.cpp @@ -34,6 +34,7 @@ #include <math.h> #include <sys/types.h> #include <errno.h> +#include <limits.h> #include "../parameters/parameters.h" extern "C" { @@ -87,14 +88,14 @@ static void restore_all_sysfs(void) static void find_all_usb_callback(const char *d_name) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/power/active_duration", d_name); if (access(filename, R_OK) != 0) return; - sprintf(filename, "/sys/bus/usb/devices/%s/power/idVendor", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/power/idVendor", d_name); file.open(filename, ios::in); if (file) { file.getline(filename, 4096); @@ -103,7 +104,7 @@ static void find_all_usb_callback(const char *d_name) return; } - sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/power/control", d_name); save_sysfs(filename); usb_devices.push_back(filename); } @@ -123,8 +124,8 @@ static void suspend_all_usb_devices(void) static void find_all_rfkill_callback(const char *d_name) { - char filename[4096]; - sprintf(filename, "/sys/class/rfkill/%s/soft", d_name); + char filename[PATH_MAX]; + snprintf(filename, PATH_MAX, "/sys/class/rfkill/%s/soft", d_name); if (access(filename, R_OK) != 0) return; save_sysfs(filename); @@ -153,14 +154,14 @@ static void unrfkill_all_radios(void) static void find_backlight_callback(const char *d_name) { - char filename[4096]; - sprintf(filename, "/sys/class/backlight/%s/brightness", d_name); + char filename[PATH_MAX]; + snprintf(filename, PATH_MAX, "/sys/class/backlight/%s/brightness", d_name); if (access(filename, R_OK) != 0) return; save_sysfs(filename); backlight_devices.push_back(filename); - sprintf(filename, "/sys/class/backlight/%s/max_brightness", d_name); + snprintf(filename, PATH_MAX, "/sys/class/backlight/%s/max_brightness", d_name); blmax = read_sysfs(filename); } @@ -179,8 +180,8 @@ static void lower_backlight(void) static void find_scsi_link_callback(const char *d_name) { - char filename[4096]; - sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); + char filename[PATH_MAX]; + snprintf(filename, PATH_MAX, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); if (access(filename, R_OK)!=0) return; diff --git a/src/cpu/abstract_cpu.cpp b/src/cpu/abstract_cpu.cpp index a3a9ffa..0d15b3f 100644 --- a/src/cpu/abstract_cpu.cpp +++ b/src/cpu/abstract_cpu.cpp @@ -27,6 +27,7 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> +#include <limits.h> #include "cpu.h" #include "../lib.h" @@ -438,19 +439,19 @@ void abstract_cpu::change_effective_frequency(uint64_t time, uint64_t frequency) void abstract_cpu::wiggle(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream ifile; ofstream ofile; uint64_t minf,maxf; /* wiggle a CPU so that we have a record of it at the start and end of the perf trace */ - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_max_freq", first_cpu); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_max_freq", first_cpu); ifile.open(filename, ios::in); ifile >> maxf; ifile.close(); - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_min_freq", first_cpu); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_min_freq", first_cpu); ifile.open(filename, ios::in); ifile >> minf; ifile.close(); @@ -461,7 +462,7 @@ void abstract_cpu::wiggle(void) ofile.open(filename, ios::out); ofile << minf; ofile.close(); - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_max_freq", first_cpu); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_max_freq", first_cpu); ofile.open(filename, ios::out); ofile << minf; ofile.close(); diff --git a/src/cpu/intel_cpus.cpp b/src/cpu/intel_cpus.cpp index cf3ecf7..3cf5eec 100644 --- a/src/cpu/intel_cpus.cpp +++ b/src/cpu/intel_cpus.cpp @@ -35,6 +35,7 @@ #include <string.h> #include <errno.h> #include <unistd.h> +#include <limits.h> #include "../lib.h" #include "../parameters/parameters.h" @@ -146,7 +147,7 @@ nhm_core::nhm_core(int model) void nhm_core::measurement_start(void) { ifstream file; - char filename[4096]; + char filename[PATH_MAX]; /* the abstract function needs to be first since it clears all state */ abstract_cpu::measurement_start(); @@ -172,7 +173,7 @@ void nhm_core::measurement_start(void) } - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); file.open(filename, ios::in); @@ -494,7 +495,7 @@ void nhm_package::measurement_end(void) void nhm_cpu::measurement_start(void) { ifstream file; - char filename[4096]; + char filename[PATH_MAX]; cpu_linux::measurement_start(); @@ -506,7 +507,7 @@ void nhm_cpu::measurement_start(void) insert_cstate("active", _("C0 active"), 0, aperf_before, 1); - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); file.open(filename, ios::in); diff --git a/src/devices/ahci.cpp b/src/devices/ahci.cpp index 72e889f..a394f98 100644 --- a/src/devices/ahci.cpp +++ b/src/devices/ahci.cpp @@ -28,6 +28,7 @@ #include <stdio.h> #include <sys/types.h> #include <dirent.h> +#include <limits.h> using namespace std; @@ -130,37 +131,37 @@ ahci::ahci(char *_name, char *path): device() register_sysfs_path(sysfs_path); - sprintf(devname, "ahci:%s", _name); + snprintf(devname, 128, "ahci:%s", _name); strncpy(name, devname, sizeof(name)); active_index = get_param_index("ahci-link-power-active"); partial_index = get_param_index("ahci-link-power-partial"); - sprintf(buffer, "%s-active", name); + snprintf(buffer, 4096, "%s-active", name); active_rindex = get_result_index(buffer); - sprintf(buffer, "%s-partial", name); + snprintf(buffer, 4096, "%s-partial", name); partial_rindex = get_result_index(buffer); - sprintf(buffer, "%s-slumber", name); + snprintf(buffer, 4096, "%s-slumber", name); slumber_rindex = get_result_index(buffer); - sprintf(buffer, "%s-devslp", name); + snprintf(buffer, 4096, "%s-devslp", name); devslp_rindex = get_result_index(buffer); diskname = model_name(path, _name); if (strlen(diskname.c_str()) == 0) - sprintf(humanname, _("SATA link: %s"), _name); + snprintf(humanname, 4096, _("SATA link: %s"), _name); else - sprintf(humanname, _("SATA disk: %s"), diskname.c_str()); + snprintf(humanname, 4096, _("SATA disk: %s"), diskname.c_str()); } void ahci::start_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "%s/ahci_alpm_active", sysfs_path); + snprintf(filename, PATH_MAX, "%s/ahci_alpm_active", sysfs_path); try { file.open(filename, ios::in); if (file) { @@ -195,32 +196,32 @@ void ahci::start_measurement(void) void ahci::end_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; char powername[4096]; ifstream file; double p; double total; try { - sprintf(filename, "%s/ahci_alpm_active", sysfs_path); + snprintf(filename, 4096, "%s/ahci_alpm_active", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_active; } file.close(); - sprintf(filename, "%s/ahci_alpm_partial", sysfs_path); + snprintf(filename, 4096, "%s/ahci_alpm_partial", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_partial; } file.close(); - sprintf(filename, "%s/ahci_alpm_slumber", sysfs_path); + snprintf(filename, 4096, "%s/ahci_alpm_slumber", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_slumber; } file.close(); - sprintf(filename, "%s/ahci_alpm_devslp", sysfs_path); + snprintf(filename, 4096, "%s/ahci_alpm_devslp", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_devslp; @@ -244,28 +245,28 @@ void ahci::end_measurement(void) p = (end_active - start_active) / total * 100.0; if (p < 0) p = 0; - sprintf(powername, "%s-active", name); + snprintf(powername, 4096, "%s-active", name); report_utilization(powername, p); /* percent in partial */ p = (end_partial - start_partial) / total * 100.0; if (p < 0) p = 0; - sprintf(powername, "%s-partial", name); + snprintf(powername, 4096, "%s-partial", name); report_utilization(powername, p); /* percent in slumber */ p = (end_slumber - start_slumber) / total * 100.0; if (p < 0) p = 0; - sprintf(powername, "%s-slumber", name); + snprintf(powername, 4096, "%s-slumber", name); report_utilization(powername, p); /* percent in devslp */ p = (end_devslp - start_devslp) / total * 100.0; if (p < 0) p = 0; - sprintf(powername, "%s-devslp", name); + snprintf(powername, 4096, "%s-devslp", name); report_utilization(powername, p); } @@ -291,7 +292,7 @@ void create_all_ahcis(void) { struct dirent *entry; DIR *dir; - char filename[4096]; + char filename[PATH_MAX]; dir = opendir("/sys/class/scsi_host/"); if (!dir) @@ -305,7 +306,7 @@ void create_all_ahcis(void) break; if (entry->d_name[0] == '.') continue; - sprintf(filename, "/sys/class/scsi_host/%s/ahci_alpm_accounting", entry->d_name); + snprintf(filename, PATH_MAX, "/sys/class/scsi_host/%s/ahci_alpm_accounting", entry->d_name); check_file.open(filename, ios::in); check_file.get(); @@ -318,7 +319,7 @@ void create_all_ahcis(void) continue; file << 1 ; file.close(); - sprintf(filename, "/sys/class/scsi_host/%s", entry->d_name); + snprintf(filename, PATH_MAX, "/sys/class/scsi_host/%s", entry->d_name); bl = new class ahci(entry->d_name, filename); all_devices.push_back(bl); diff --git a/src/devices/ahci.h b/src/devices/ahci.h index 99813d7..7431fb5 100644 --- a/src/devices/ahci.h +++ b/src/devices/ahci.h @@ -27,6 +27,7 @@ #include <string> +#include <limits.h> #include "device.h" #include "../parameters/parameters.h" #include <stdint.h> @@ -36,7 +37,7 @@ class ahci: public device { uint64_t start_partial, end_partial; uint64_t start_slumber, end_slumber; uint64_t start_devslp, end_devslp; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; int partial_rindex; int active_rindex; diff --git a/src/devices/alsa.cpp b/src/devices/alsa.cpp index a1fca71..961a9e5 100644 --- a/src/devices/alsa.cpp +++ b/src/devices/alsa.cpp @@ -53,47 +53,47 @@ alsa::alsa(const char *_name, const char *path): device() start_inactive = 0; strncpy(sysfs_path, path, sizeof(sysfs_path)); - sprintf(devname, "alsa:%s", _name); - sprintf(humanname, "alsa:%s", _name); + snprintf(devname, 4096, "alsa:%s", _name); + snprintf(humanname, 4096, "alsa:%s", _name); strncpy(name, devname, sizeof(name)); rindex = get_result_index(name); guilty[0] = 0; model[0] = 0; vendor[0] = 0; - sprintf(devname, "%s/modelname", path); + snprintf(devname, 4096, "%s/modelname", path); file.open(devname); if (file) { file.getline(model, 4096); file.close(); } - sprintf(devname, "%s/vendor_name", path); + snprintf(devname, 4096, "%s/vendor_name", path); file.open(devname); if (file) { file.getline(vendor, 4096); file.close(); } if (strlen(model) && strlen(vendor)) - sprintf(humanname, _("Audio codec %s: %s (%s)"), name, model, vendor); + snprintf(humanname, 4096, _("Audio codec %s: %s (%s)"), name, model, vendor); else if (strlen(model)) - sprintf(humanname, _("Audio codec %s: %s"), _name, model); + snprintf(humanname, 4096, _("Audio codec %s: %s"), _name, model); else if (strlen(vendor)) - sprintf(humanname, _("Audio codec %s: %s"), _name, vendor); + snprintf(humanname, 4096, _("Audio codec %s: %s"), _name, vendor); } void alsa::start_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "%s/power_off_acct", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power_off_acct", sysfs_path); try { file.open(filename, ios::in); if (file) { file >> start_inactive; } file.close(); - sprintf(filename, "%s/power_on_acct", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power_on_acct", sysfs_path); file.open(filename, ios::in); if (file) { @@ -108,18 +108,18 @@ void alsa::start_measurement(void) void alsa::end_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; double p; - sprintf(filename, "%s/power_off_acct", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power_off_acct", sysfs_path); try { file.open(filename, ios::in); if (file) { file >> end_inactive; } file.close(); - sprintf(filename, "%s/power_on_acct", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power_on_acct", sysfs_path); file.open(filename, ios::in); if (file) { @@ -152,17 +152,17 @@ const char * alsa::device_name(void) static void create_all_alsa_callback(const char *d_name) { - char filename[4096]; + char filename[PATH_MAX]; class alsa *bl; if (strncmp(d_name, "hwC", 3) != 0) return; - sprintf(filename, "/sys/class/sound/card0/%s/power_on_acct", d_name); + snprintf(filename, PATH_MAX, "/sys/class/sound/card0/%s/power_on_acct", d_name); if (access(filename, R_OK) != 0) return; - sprintf(filename, "/sys/class/sound/card0/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/class/sound/card0/%s", d_name); bl = new class alsa(d_name, filename); all_devices.push_back(bl); register_parameter("alsa-codec-power", 0.5); diff --git a/src/devices/alsa.h b/src/devices/alsa.h index f7b4b97..b68203f 100644 --- a/src/devices/alsa.h +++ b/src/devices/alsa.h @@ -30,11 +30,12 @@ #include "../parameters/parameters.h" #include <stdint.h> +#include <limits.h> class alsa: public device { uint64_t start_active, end_active; uint64_t start_inactive, end_inactive; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; char humanname[4096]; char temp_buf[4096]; diff --git a/src/devices/backlight.cpp b/src/devices/backlight.cpp index 73bdac9..d12cf98 100644 --- a/src/devices/backlight.cpp +++ b/src/devices/backlight.cpp @@ -28,6 +28,7 @@ #include <stdio.h> #include <sys/types.h> #include <dirent.h> +#include <limits.h> using namespace std; @@ -54,17 +55,17 @@ backlight::backlight(const char *_name, const char *path): device() void backlight::start_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "%s/max_brightness", sysfs_path); + snprintf(filename, PATH_MAX, "%s/max_brightness", sysfs_path); file.open(filename, ios::in); if (file) { file >> max_level; } file.close(); - sprintf(filename, "%s/actual_brightness", sysfs_path); + snprintf(filename, PATH_MAX, "%s/actual_brightness", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_level; @@ -76,7 +77,7 @@ static int dpms_screen_on(void) { DIR *dir; struct dirent *entry; - char filename[4096]; + char filename[PATH_MAX]; char line[4096]; ifstream file; @@ -90,7 +91,7 @@ static int dpms_screen_on(void) if (strncmp(entry->d_name, "card", 4) != 0) continue; - sprintf(filename, "/sys/class/drm/card0/%s/enabled", entry->d_name); + snprintf(filename, PATH_MAX, "/sys/class/drm/card0/%s/enabled", entry->d_name); file.open(filename, ios::in); if (!file) continue; @@ -98,7 +99,7 @@ static int dpms_screen_on(void) file.close(); if (strcmp(line, "enabled") != 0) continue; - sprintf(filename, "/sys/class/drm/card0/%s/dpms", entry->d_name); + snprintf(filename, PATH_MAX, "/sys/class/drm/card0/%s/dpms", entry->d_name); file.open(filename, ios::in); if (!file) continue; @@ -115,13 +116,13 @@ static int dpms_screen_on(void) void backlight::end_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; char powername[4096]; ifstream file; double p; int _backlight = 0; - sprintf(filename, "%s/actual_brightness", sysfs_path); + snprintf(filename, PATH_MAX, "%s/actual_brightness", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_level; @@ -136,7 +137,7 @@ void backlight::end_measurement(void) } report_utilization(name, p); - sprintf(powername, "%s-power", name); + snprintf(powername, 4096, "%s-power", name); report_utilization(powername, _backlight); } @@ -157,8 +158,8 @@ const char * backlight::device_name(void) static void create_all_backlights_callback(const char *d_name) { class backlight *bl; - char filename[4096]; - sprintf(filename, "/sys/class/backlight/%s", d_name); + char filename[PATH_MAX]; + snprintf(filename, PATH_MAX, "/sys/class/backlight/%s", d_name); bl = new class backlight(d_name, filename); all_devices.push_back(bl); } diff --git a/src/devices/backlight.h b/src/devices/backlight.h index 3d5377a..1dac778 100644 --- a/src/devices/backlight.h +++ b/src/devices/backlight.h @@ -25,13 +25,14 @@ #ifndef _INCLUDE_GUARD_BACKLIGHT_H #define _INCLUDE_GUARD_BACKLIGHT_H +#include <limits.h> #include "device.h" class backlight: public device { int min_level, max_level; int start_level, end_level; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; int r_index; int r_index_power; diff --git a/src/devices/i915-gpu.cpp b/src/devices/i915-gpu.cpp index a2cfaa5..c63e11f 100644 --- a/src/devices/i915-gpu.cpp +++ b/src/devices/i915-gpu.cpp @@ -29,6 +29,7 @@ #include <sys/types.h> #include <dirent.h> #include <unistd.h> +#include <limits.h> using namespace std; @@ -73,7 +74,7 @@ double i915gpu::utilization(void) void create_i915_gpu(void) { - char filename[4096]; + char filename[PATH_MAX]; class i915gpu *gpu; gpu_rapl_device *rapl_dev; diff --git a/src/devices/network.h b/src/devices/network.h index 45dc130..7fb4cc6 100644 --- a/src/devices/network.h +++ b/src/devices/network.h @@ -26,6 +26,7 @@ #define _INCLUDE_GUARD_NETWORK_H #include <sys/time.h> +#include <limits.h> #include "device.h" #include "../parameters/parameters.h" @@ -38,7 +39,7 @@ class network: public device { int start_speed; /* 0 is "no link" */ int end_speed; /* 0 is "no link" */ - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; char humanname[4096]; int index_up; diff --git a/src/devices/rfkill.cpp b/src/devices/rfkill.cpp index 01aec23..7dea12e 100644 --- a/src/devices/rfkill.cpp +++ b/src/devices/rfkill.cpp @@ -29,6 +29,7 @@ #include <sys/types.h> #include <libgen.h> #include <unistd.h> +#include <limits.h> using namespace std; @@ -43,7 +44,7 @@ using namespace std; rfkill::rfkill(char *_name, char *path): device() { char line[4096]; - char filename[4096]; + char filename[PATH_MAX]; char devname[128]; start_soft = 0; start_hard = 0; @@ -51,27 +52,27 @@ rfkill::rfkill(char *_name, char *path): device() end_hard = 0; strncpy(sysfs_path, path, sizeof(sysfs_path)); register_sysfs_path(sysfs_path); - sprintf(devname, "radio:%s", _name); - sprintf(humanname, "radio:%s", _name); + snprintf(devname, 128, "radio:%s", _name); + snprintf(humanname, 4096, "radio:%s", _name); strncpy(name, devname, sizeof(name)); register_parameter(devname); index = get_param_index(devname); rindex = get_result_index(name); memset(line, 0, 4096); - sprintf(filename, "%s/device/driver", path); + snprintf(filename, PATH_MAX, "%s/device/driver", path); if (readlink(filename, line, 4096) > 0) { - sprintf(humanname, _("Radio device: %s"), basename(line)); + snprintf(humanname, 4096, _("Radio device: %s"), basename(line)); } - sprintf(filename, "%s/device/device/driver", path); + snprintf(filename, PATH_MAX, "%s/device/device/driver", path); if (readlink(filename, line, 4096) > 0) { - sprintf(humanname, _("Radio device: %s"), basename(line)); + snprintf(humanname, 4096, _("Radio device: %s"), basename(line)); } } void rfkill::start_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; start_hard = 1; @@ -79,14 +80,14 @@ void rfkill::start_measurement(void) end_hard = 1; end_soft = 1; - sprintf(filename, "%s/hard", sysfs_path); + snprintf(filename, PATH_MAX, "%s/hard", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_hard; } file.close(); - sprintf(filename, "%s/soft", sysfs_path); + snprintf(filename, PATH_MAX, "%s/soft", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_soft; @@ -96,16 +97,16 @@ void rfkill::start_measurement(void) void rfkill::end_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "%s/hard", sysfs_path); + snprintf(filename, PATH_MAX, "%s/hard", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_hard; } file.close(); - sprintf(filename, "%s/soft", sysfs_path); + snprintf(filename, PATH_MAX, "%s/soft", sysfs_path); file.open(filename, ios::in); if (file) { file >> end_soft; @@ -137,20 +138,20 @@ const char * rfkill::device_name(void) static void create_all_rfkills_callback(const char *d_name) { - char filename[4096]; - char name[4096]; + char filename[PATH_MAX]; + char name[4096] = {0}; class rfkill *bl; ifstream file; - sprintf(filename, "/sys/class/rfkill/%s/name", d_name); - strcpy(name, d_name); + snprintf(filename, PATH_MAX, "/sys/class/rfkill/%s/name", d_name); + strncpy(name, d_name, 4095); file.open(filename, ios::in); if (file) { file.getline(name, 100); file.close(); } - sprintf(filename, "/sys/class/rfkill/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/class/rfkill/%s", d_name); bl = new class rfkill(name, filename); all_devices.push_back(bl); } diff --git a/src/devices/rfkill.h b/src/devices/rfkill.h index c24e03b..429ba18 100644 --- a/src/devices/rfkill.h +++ b/src/devices/rfkill.h @@ -25,6 +25,7 @@ #ifndef _INCLUDE_GUARD_RFKILL_H #define _INCLUDE_GUARD_RFKILL_H +#include <limits.h> #include "device.h" #include "../parameters/parameters.h" @@ -32,7 +33,7 @@ class rfkill: public device { int start_soft, end_soft; int start_hard, end_hard; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; char humanname[4096]; int index; diff --git a/src/devices/runtime_pm.cpp b/src/devices/runtime_pm.cpp index eede027..0f9c5a4 100644 --- a/src/devices/runtime_pm.cpp +++ b/src/devices/runtime_pm.cpp @@ -30,6 +30,7 @@ #include <unistd.h> #include <sys/types.h> #include <dirent.h> +#include <limits.h> #include "../parameters/parameters.h" #include "../lib.h" @@ -57,7 +58,7 @@ runtime_pmdevice::runtime_pmdevice(const char *_name, const char *path) : device void runtime_pmdevice::start_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; before_suspended_time = 0; @@ -65,14 +66,14 @@ void runtime_pmdevice::start_measurement(void) after_suspended_time = 0; after_active_time = 0; - sprintf(filename, "%s/power/runtime_suspended_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_suspended_time", sysfs_path); file.open(filename, ios::in); if (!file) return; file >> before_suspended_time; file.close(); - sprintf(filename, "%s/power/runtime_active_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_active_time", sysfs_path); file.open(filename, ios::in); if (!file) return; @@ -82,17 +83,17 @@ void runtime_pmdevice::start_measurement(void) void runtime_pmdevice::end_measurement(void) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; - sprintf(filename, "%s/power/runtime_suspended_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_suspended_time", sysfs_path); file.open(filename, ios::in); if (!file) return; file >> after_suspended_time; file.close(); - sprintf(filename, "%s/power/runtime_active_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_active_time", sysfs_path); file.open(filename, ios::in); if (!file) return; @@ -146,11 +147,11 @@ void runtime_pmdevice::set_human_name(char *_name) int device_has_runtime_pm(const char *sysfs_path) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; unsigned long value; - sprintf(filename, "%s/power/runtime_suspended_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_suspended_time", sysfs_path); file.open(filename, ios::in); if (!file) return 0; @@ -159,7 +160,7 @@ int device_has_runtime_pm(const char *sysfs_path) if (value) return 1; - sprintf(filename, "%s/power/runtime_active_time", sysfs_path); + snprintf(filename, PATH_MAX, "%s/power/runtime_active_time", sysfs_path); file.open(filename, ios::in); if (!file) return 0; @@ -177,9 +178,9 @@ static void do_bus(const char *bus) struct dirent *entry; DIR *dir; - char filename[4096]; + char filename[PATH_MAX]; - sprintf(filename, "/sys/bus/%s/devices/", bus); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/", bus); dir = opendir(filename); if (!dir) return; @@ -200,25 +201,25 @@ static void do_bus(const char *bus) char dev_name[4096]; bool is_adapter = false; - sprintf(filename, "/sys/bus/%s/devices/%s/new_device", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/new_device", bus, entry->d_name); if (access(filename, W_OK) == 0) is_adapter = true; - sprintf(filename, "/sys/bus/%s/devices/%s/name", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/name", bus, entry->d_name); file.open(filename, ios::in); if (file) { getline(file, devname); file.close(); } - sprintf(dev_name, _("I2C %s (%s): %s"), (is_adapter ? _("Adapter") : _("Device")), entry->d_name, devname.c_str()); + snprintf(dev_name, 4096, _("I2C %s (%s): %s"), (is_adapter ? _("Adapter") : _("Device")), entry->d_name, devname.c_str()); dev->set_human_name(dev_name); } if (strcmp(bus, "pci") == 0) { uint16_t vendor = 0, device = 0; - sprintf(filename, "/sys/bus/%s/devices/%s/vendor", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/vendor", bus, entry->d_name); file.open(filename, ios::in); if (file) { @@ -227,7 +228,7 @@ static void do_bus(const char *bus) } - sprintf(filename, "/sys/bus/%s/devices/%s/device", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/device", bus, entry->d_name); file.open(filename, ios::in); if (file) { file >> hex >> device; @@ -236,7 +237,7 @@ static void do_bus(const char *bus) if (vendor && device) { char devname[4096]; - sprintf(devname, _("PCI Device: %s"), + snprintf(devname, 4096, _("PCI Device: %s"), pci_id_to_name(vendor, device, filename, 4095)); dev->set_human_name(devname); } diff --git a/src/devices/runtime_pm.h b/src/devices/runtime_pm.h index ea09dac..77bf398 100644 --- a/src/devices/runtime_pm.h +++ b/src/devices/runtime_pm.h @@ -25,6 +25,7 @@ #ifndef _INCLUDE_GUARD_RUNTIMEPM_H #define _INCLUDE_GUARD_RUNTIMEPM_H +#include <limits.h> #include "device.h" #include "../parameters/parameters.h" @@ -32,7 +33,7 @@ class runtime_pmdevice: public device { uint64_t before_suspended_time, before_active_time; uint64_t after_suspended_time, after_active_time; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; char humanname[4096]; int index; diff --git a/src/devices/thinkpad-fan.cpp b/src/devices/thinkpad-fan.cpp index 9f470e4..d9bb026 100644 --- a/src/devices/thinkpad-fan.cpp +++ b/src/devices/thinkpad-fan.cpp @@ -30,6 +30,7 @@ #include <dirent.h> #include <math.h> #include <unistd.h> +#include <limits.h> #include "../lib.h" @@ -74,7 +75,7 @@ double thinkpad_fan::utilization(void) void create_thinkpad_fan(void) { - char filename[4096]; + char filename[PATH_MAX]; class thinkpad_fan *fan; strcpy(filename, "/sys/devices/platform/thinkpad_hwmon/fan1_input"); diff --git a/src/devices/thinkpad-light.cpp b/src/devices/thinkpad-light.cpp index e5fde10..945161a 100644 --- a/src/devices/thinkpad-light.cpp +++ b/src/devices/thinkpad-light.cpp @@ -30,6 +30,7 @@ #include <dirent.h> #include <math.h> #include <unistd.h> +#include <limits.h> #include "../lib.h" @@ -72,7 +73,7 @@ double thinkpad_light::utilization(void) void create_thinkpad_light(void) { - char filename[4096]; + char filename[PATH_MAX]; class thinkpad_light *light; strcpy(filename, "/sys/devices/platform/thinkpad_acpi/leds/tpacpi::thinklight/brightness"); diff --git a/src/devices/usb.cpp b/src/devices/usb.cpp index 2c5d38b..eb8c718 100644 --- a/src/devices/usb.cpp +++ b/src/devices/usb.cpp @@ -28,6 +28,7 @@ #include <stdio.h> #include <unistd.h> #include <sys/types.h> +#include <limits.h> #include "../lib.h" #include "../parameters/parameters.h" @@ -38,7 +39,7 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): device() { ifstream file; - char filename[4096]; + char filename[PATH_MAX]; char vendor[4096]; char product[4096]; @@ -59,7 +60,7 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de /* root ports and hubs should count as 0 power ... their activity is derived */ - sprintf(filename, "%s/bDeviceClass", path); + snprintf(filename, PATH_MAX, "%s/bDeviceClass", path); file.open(filename, ios::in); if (file) { int dclass = 0; @@ -72,7 +73,7 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de vendor[0] = 0; product[0] = 0; - sprintf(filename, "%s/manufacturer", path); + snprintf(filename, PATH_MAX, "%s/manufacturer", path); file.open(filename, ios::in); if (file) { file.getline(vendor, 2047); @@ -80,18 +81,18 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de vendor[0] = 0; file.close(); }; - sprintf(filename, "%s/product", path); + snprintf(filename, PATH_MAX, "%s/product", path); file.open(filename, ios::in); if (file) { file.getline(product, 2040); file.close(); }; if (strlen(vendor) && strlen(product)) - sprintf(humanname, _("USB device: %s (%s)"), product, vendor); + snprintf(humanname, 4096, _("USB device: %s (%s)"), product, vendor); else if (strlen(product)) - sprintf(humanname, _("USB device: %s"), product); + snprintf(humanname, 4096, _("USB device: %s"), product); else if (strlen(vendor)) - sprintf(humanname, _("USB device: %s"), vendor); + snprintf(humanname, 4096, _("USB device: %s"), vendor); } @@ -99,21 +100,21 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de void usbdevice::start_measurement(void) { ifstream file; - char fullpath[4096]; + char fullpath[PATH_MAX]; active_before = 0; active_after = 0; connected_before = 0; connected_after = 0; - sprintf(fullpath, "%s/power/active_duration", sysfs_path); + snprintf(fullpath, PATH_MAX, "%s/power/active_duration", sysfs_path); file.open(fullpath, ios::in); if (file) { file >> active_before; } file.close(); - sprintf(fullpath, "%s/power/connected_duration", sysfs_path); + snprintf(fullpath, PATH_MAX, "%s/power/connected_duration", sysfs_path); file.open(fullpath, ios::in); if (file) { file >> connected_before; @@ -124,16 +125,16 @@ void usbdevice::start_measurement(void) void usbdevice::end_measurement(void) { ifstream file; - char fullpath[4096]; + char fullpath[PATH_MAX]; - sprintf(fullpath, "%s/power/active_duration", sysfs_path); + snprintf(fullpath, PATH_MAX, "%s/power/active_duration", sysfs_path); file.open(fullpath, ios::in); if (file) { file >> active_after; } file.close(); - sprintf(fullpath, "%s/power/connected_duration", sysfs_path); + snprintf(fullpath, PATH_MAX, "%s/power/connected_duration", sysfs_path); file.open(fullpath, ios::in); if (file) { file >> connected_after; @@ -186,31 +187,31 @@ double usbdevice::power_usage(struct result_bundle *result, struct parameter_bun static void create_all_usb_devices_callback(const char *d_name) { - char filename[4096]; + char filename[PATH_MAX]; ifstream file; class usbdevice *usb; - char device_name[4096]; + char device_name[PATH_MAX]; char vendorid[64], devid[64]; char devid_name[4096]; - sprintf(filename, "/sys/bus/usb/devices/%s", d_name); - sprintf(device_name, "%s/power/active_duration", filename); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s", d_name); + snprintf(device_name, PATH_MAX, "%s/power/active_duration", filename); if (access(device_name, R_OK) != 0) return; - sprintf(device_name, "%s/idVendor", filename); + snprintf(device_name, PATH_MAX, "%s/idVendor", filename); file.open(device_name, ios::in); if (file) file.getline(vendorid, 64); file.close(); - sprintf(device_name, "%s/idProduct", filename); + snprintf(device_name, PATH_MAX, "%s/idProduct", filename); file.open(device_name, ios::in); if (file) file.getline(devid, 64); file.close(); - sprintf(devid_name, "usb-device-%s-%s", vendorid, devid); - sprintf(device_name, "usb-device-%s-%s-%s", d_name, vendorid, devid); + snprintf(devid_name, 4096, "usb-device-%s-%s", vendorid, devid); + snprintf(device_name, PATH_MAX, "usb-device-%s-%s-%s", d_name, vendorid, devid); if (result_device_exists(device_name)) return; diff --git a/src/devices/usb.h b/src/devices/usb.h index 39a746a..097df51 100644 --- a/src/devices/usb.h +++ b/src/devices/usb.h @@ -25,6 +25,7 @@ #ifndef _INCLUDE_GUARD_USB_H #define _INCLUDE_GUARD_USB_H +#include <limits.h> #include "device.h" #include "../parameters/parameters.h" @@ -32,7 +33,7 @@ class usbdevice: public device { int active_before, active_after; int connected_before, connected_after; - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char name[4096]; char devname[4096]; char humanname[4096]; diff --git a/src/devlist.cpp b/src/devlist.cpp index 7f599ac..787f5f6 100644 --- a/src/devlist.cpp +++ b/src/devlist.cpp @@ -38,6 +38,7 @@ #include <dirent.h> #include <string.h> #include <ctype.h> +#include <limits.h> using namespace std; @@ -93,8 +94,8 @@ void collect_open_devices(void) { struct dirent *entry; DIR *dir; - char filename[4096]; - char link[4096]; + char filename[PATH_MAX]; + char link[PATH_MAX]; unsigned int i; vector<struct devuser *> *target; @@ -137,9 +138,9 @@ void collect_open_devices(void) break; if (!isdigit(entry2->d_name[0])) continue; - sprintf(filename, "/proc/%s/fd/%s", entry->d_name, entry2->d_name); - memset(link, 0, 4096); - ret = readlink(filename, link, 4095); + snprintf(filename, PATH_MAX, "/proc/%s/fd/%s", entry->d_name, entry2->d_name); + memset(link, 0, PATH_MAX); + ret = readlink(filename, link, PATH_MAX - 1); if (ret < 0) continue; diff --git a/src/lib.cpp b/src/lib.cpp index 437803b..5148bd8 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -34,6 +34,7 @@ #include <stdio.h> #include <math.h> #include <stdlib.h> +#include <limits.h> #include "lib.h" @@ -236,10 +237,10 @@ string read_sysfs_string(const char *format, const char *param) ifstream file; char content[4096]; char *c; - char filename[8192]; + char filename[PATH_MAX]; - snprintf(filename, 8191, format, param); + snprintf(filename, PATH_MAX, format, param); file.open(filename, ios::in); if (!file) diff --git a/src/main.cpp b/src/main.cpp index dc69fca..16acc73 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,7 @@ #include <unistd.h> #include <locale.h> #include <sys/resource.h> +#include <limits.h> #include "cpu/cpu.h" #include "process/process.h" @@ -388,8 +389,8 @@ int main(int argc, char **argv) { int option_index; int c; - char filename[4096]; - char workload[4096] = {0,}; + char filename[PATH_MAX]; + char workload[PATH_MAX] = {0}; int iterations = 1, auto_tune = 0; set_new_handler(out_of_memory); diff --git a/src/measurement/acpi.cpp b/src/measurement/acpi.cpp index b9b10fb..a55109b 100644 --- a/src/measurement/acpi.cpp +++ b/src/measurement/acpi.cpp @@ -29,6 +29,7 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> +#include <limits.h> using namespace std; @@ -51,7 +52,7 @@ present voltage: 12001 mV void acpi_power_meter::measure(void) { - char filename[4096]; + char filename[PATH_MAX]; char line[4096]; ifstream file; @@ -71,7 +72,7 @@ void acpi_power_meter::measure(void) voltage = 0; capacity = 0; - sprintf(filename, "/proc/acpi/battery/%s/state", battery_name); + snprintf(filename, PATH_MAX, "/proc/acpi/battery/%s/state", battery_name); file.open(filename, ios::in); if (!file) diff --git a/src/measurement/sysfs.cpp b/src/measurement/sysfs.cpp index ed42ed4..794f88f 100644 --- a/src/measurement/sysfs.cpp +++ b/src/measurement/sysfs.cpp @@ -27,6 +27,7 @@ #include "../lib.h" #include <string.h> #include <stdio.h> +#include <limits.h> sysfs_power_meter::sysfs_power_meter(const char *power_supply_name) { @@ -37,10 +38,10 @@ sysfs_power_meter::sysfs_power_meter(const char *power_supply_name) bool sysfs_power_meter::get_sysfs_attr(const char *attribute, int *value) { - char filename[4096]; + char filename[PATH_MAX]; bool ok; - snprintf(filename, sizeof(filename), "/sys/class/power_supply/%s/%s", name, attribute); + snprintf(filename, PATH_MAX, "/sys/class/power_supply/%s/%s", name, attribute); *value = read_sysfs(filename, &ok); return ok; diff --git a/src/parameters/parameters.cpp b/src/parameters/parameters.cpp index ec119b6..1c214f9 100644 --- a/src/parameters/parameters.cpp +++ b/src/parameters/parameters.cpp @@ -30,6 +30,7 @@ #include <math.h> #include <vector> #include <unistd.h> +#include <limits.h> struct parameter_bundle all_parameters; @@ -450,7 +451,7 @@ int global_power_valid(void) /* find the directory to store powertop results/parameters based on distribution*/ char* get_param_directory(const char *filename) { - static char tempfilename[4096]; + static char tempfilename[PATH_MAX]; if (access("/var/cache/powertop", W_OK ) == 0) sprintf(tempfilename, "/var/cache/powertop/%s", filename); diff --git a/src/report/report.cpp b/src/report/report.cpp index cd3c961..3572200 100644 --- a/src/report/report.cpp +++ b/src/report/report.cpp @@ -35,6 +35,7 @@ #include <string.h> #include <malloc.h> #include <unistd.h> +#include <limits.h> #include "report-data-html.h" using namespace std; @@ -168,7 +169,7 @@ static void system_info(void) void init_report_output(char *filename_str, int iterations) { size_t period; - char file_prefix[4096]; + char file_prefix[PATH_MAX]; char file_postfix[8]; time_t stamp; char datestr[200]; @@ -177,17 +178,17 @@ void init_report_output(char *filename_str, int iterations) sprintf(file_postfix, "%s", (reporttype == REPORT_HTML ? "html" : "csv")); period=mystring.find_last_of("."); - sprintf(file_prefix, "%s",mystring.substr(0,period).c_str()); + snprintf(file_prefix, PATH_MAX, "%s",mystring.substr(0,period).c_str()); memset(&datestr, 0, 200); memset(&stamp, 0, sizeof(time_t)); stamp=time(NULL); strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M%S", localtime(&stamp)); if (iterations != 1) - sprintf(reportout.filename, "%s-%s.%s", + snprintf(reportout.filename, PATH_MAX, "%s-%s.%s", file_prefix, datestr,file_postfix); else - sprintf(reportout.filename, "%s.%s", + snprintf(reportout.filename, PATH_MAX, "%s.%s", file_prefix, file_postfix); reportout.report_file = fopen(reportout.filename, "wm"); diff --git a/src/report/report.h b/src/report/report.h index 77bf2d7..c1aee1b 100644 --- a/src/report/report.h +++ b/src/report/report.h @@ -27,6 +27,7 @@ #include <string> #include <stdio.h> +#include <limits.h> #include "report-maker.h" @@ -34,7 +35,7 @@ using namespace std; struct reportstream { FILE *report_file; - char filename[4096]; + char filename[PATH_MAX]; }; extern report_type reporttype; diff --git a/src/tuning/runtime.cpp b/src/tuning/runtime.cpp index 3201fdd..dcceb1a 100644 --- a/src/tuning/runtime.cpp +++ b/src/tuning/runtime.cpp @@ -34,6 +34,7 @@ #include <unistd.h> #include <sys/types.h> #include <dirent.h> +#include <limits.h> #include "../lib.h" #include "../devices/runtime_pm.h" @@ -123,9 +124,9 @@ void add_runtime_tunables(const char *bus) { struct dirent *entry; DIR *dir; - char filename[4096]; + char filename[PATH_MAX]; - sprintf(filename, "/sys/bus/%s/devices/", bus); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/", bus); dir = opendir(filename); if (!dir) return; @@ -139,13 +140,13 @@ void add_runtime_tunables(const char *bus) if (entry->d_name[0] == '.') continue; - sprintf(filename, "/sys/bus/%s/devices/%s/power/control", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/power/control", bus, entry->d_name); if (access(filename, R_OK) != 0) continue; - sprintf(filename, "/sys/bus/%s/devices/%s", bus, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s", bus, entry->d_name); runtime = new class runtime_tunable(filename, bus, entry->d_name); diff --git a/src/tuning/runtime.h b/src/tuning/runtime.h index a3c3e20..b292a0f 100644 --- a/src/tuning/runtime.h +++ b/src/tuning/runtime.h @@ -26,12 +26,13 @@ #define _INCLUDE_GUARD_RUNTIME_TUNE_H #include <vector> +#include <limits.h> #include "tunable.h" using namespace std; class runtime_tunable : public tunable { - char runtime_path[4096]; + char runtime_path[PATH_MAX]; public: runtime_tunable(const char *runtime_path, const char *bus, const char *dev); diff --git a/src/tuning/tuningi2c.cpp b/src/tuning/tuningi2c.cpp index 60a085b..d207ca0 100644 --- a/src/tuning/tuningi2c.cpp +++ b/src/tuning/tuningi2c.cpp @@ -27,6 +27,7 @@ #include <iostream> #include <fstream> #include <ctype.h> +#include <limits.h> #include "../lib.h" #include "../devices/runtime_pm.h" @@ -34,10 +35,10 @@ i2c_tunable::i2c_tunable(const char *path, const char *name, bool is_adapter) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown")) { ifstream file; - char filename[4096]; + char filename[PATH_MAX]; string devname; - sprintf(filename, "%s/name", path); + snprintf(filename, PATH_MAX, "%s/name", path); file.open(filename, ios::in); if (file) { getline(file, devname); @@ -45,20 +46,20 @@ i2c_tunable::i2c_tunable(const char *path, const char *name, bool is_adapter) : } if (is_adapter) { - sprintf(i2c_path, "%s/device/power/control", path); - sprintf(filename, "%s/device", path); + snprintf(i2c_path, PATH_MAX, "%s/device/power/control", path); + snprintf(filename, PATH_MAX, "%s/device", path); } else { - sprintf(i2c_path, "%s/power/control", path); - sprintf(filename, "%s/device", path); + snprintf(i2c_path, PATH_MAX, "%s/power/control", path); + snprintf(filename, PATH_MAX, "%s/device", path); } if (device_has_runtime_pm(filename)) - sprintf(desc, _("Runtime PM for I2C %s %s (%s)"), (is_adapter ? _("Adapter") : _("Device")), name, (devname.empty() ? "" : devname.c_str())); + snprintf(desc, 4096, _("Runtime PM for I2C %s %s (%s)"), (is_adapter ? _("Adapter") : _("Device")), name, (devname.empty() ? "" : devname.c_str())); else - sprintf(desc, _("I2C %s %s has no runtime power management"), (is_adapter ? _("Adapter") : _("Device")), name); + snprintf(desc, 4096, _("I2C %s %s has no runtime power management"), (is_adapter ? _("Adapter") : _("Device")), name); - sprintf(toggle_good, "echo 'auto' > '%s';", i2c_path); - sprintf(toggle_bad, "echo 'on' > '%s';", i2c_path); + snprintf(toggle_good, 4096, "echo 'auto' > '%s';", i2c_path); + snprintf(toggle_bad, 4096, "echo 'on' > '%s';", i2c_path); } int i2c_tunable::good_bad(void) @@ -101,20 +102,20 @@ const char *i2c_tunable::toggle_script(void) static void add_i2c_callback(const char *d_name) { class i2c_tunable *i2c; - char filename[4096]; + char filename[PATH_MAX]; bool is_adapter = false; - sprintf(filename, "/sys/bus/i2c/devices/%s/new_device", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/i2c/devices/%s/new_device", d_name); if (access(filename, W_OK) == 0) is_adapter = true; - sprintf(filename, "/sys/bus/i2c/devices/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/i2c/devices/%s", d_name); i2c = new class i2c_tunable(filename, d_name, is_adapter); if (is_adapter) - sprintf(filename, "/sys/bus/i2c/devices/%s/device", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/i2c/devices/%s/device", d_name); else - sprintf(filename, "/sys/bus/i2c/devices/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/i2c/devices/%s", d_name); if (device_has_runtime_pm(filename)) all_tunables.push_back(i2c); diff --git a/src/tuning/tuningi2c.h b/src/tuning/tuningi2c.h index a970faf..8fd8784 100644 --- a/src/tuning/tuningi2c.h +++ b/src/tuning/tuningi2c.h @@ -21,13 +21,14 @@ #define _INCLUDE_GUARD_I2C_TUNE_H #include <vector> +#include <limits.h> #include "tunable.h" using namespace std; class i2c_tunable : public tunable { - char i2c_path[4096]; + char i2c_path[PATH_MAX]; public: i2c_tunable(const char *path, const char *name, bool is_adapter); diff --git a/src/tuning/tuningsysfs.cpp b/src/tuning/tuningsysfs.cpp index 0859a0c..8d55969 100644 --- a/src/tuning/tuningsysfs.cpp +++ b/src/tuning/tuningsysfs.cpp @@ -37,6 +37,7 @@ #include <unistd.h> #include <sys/types.h> #include <dirent.h> +#include <limits.h> #include "../lib.h" @@ -115,11 +116,11 @@ void add_sysfs_tunable(const char *str, const char *_sysfs_path, const char *_ta static void add_sata_tunables_callback(const char *d_name) { - char filename[4096]; + char filename[PATH_MAX]; char msg[4096]; - sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); - sprintf(msg, _("Enable SATA link power management for %s"), d_name); + snprintf(filename, PATH_MAX, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); + snprintf(msg, 4096, _("Enable SATA link power management for %s"), d_name); add_sysfs_tunable(msg, filename,"min_power"); } diff --git a/src/tuning/tuningsysfs.h b/src/tuning/tuningsysfs.h index ad89717..57b9de7 100644 --- a/src/tuning/tuningsysfs.h +++ b/src/tuning/tuningsysfs.h @@ -26,13 +26,14 @@ #define _INCLUDE_GUARD_SYSFS_TUNE_H #include <vector> +#include <limits.h> #include "tunable.h" using namespace std; class sysfs_tunable : public tunable { - char sysfs_path[4096]; + char sysfs_path[PATH_MAX]; char target_value[4096]; char bad_value[4096]; public: diff --git a/src/tuning/tuningusb.cpp b/src/tuning/tuningusb.cpp index d2a0c11..12f9ce2 100644 --- a/src/tuning/tuningusb.cpp +++ b/src/tuning/tuningusb.cpp @@ -32,17 +32,18 @@ #include <utility> #include <iostream> #include <fstream> +#include <limits.h> #include "../lib.h" usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown")) { ifstream file; - char filename[4096]; + char filename[PATH_MAX]; char vendor[2048]; char product[2048]; string str1, str2; - sprintf(usb_path, "%s/power/control", path); + snprintf(usb_path, PATH_MAX, "%s/power/control", path); vendor[0] = 0; product[0] = 0; @@ -50,9 +51,9 @@ usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, str1 = read_sysfs_string("%s/idVendor", path); str2 = read_sysfs_string("%s/idProduct", path); - sprintf(desc, _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1.c_str(), str2.c_str()); + snprintf(desc, 4096, _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1.c_str(), str2.c_str()); - sprintf(filename, "%s/manufacturer", path); + snprintf(filename, PATH_MAX, "%s/manufacturer", path); file.open(filename, ios::in); if (file) { file.getline(vendor, 2047); @@ -60,21 +61,21 @@ usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, vendor[0] = 0; file.close(); }; - sprintf(filename, "%s/product", path); + snprintf(filename, PATH_MAX, "%s/product", path); file.open(filename, ios::in); if (file) { file.getline(product, 2040); file.close(); }; if (strlen(vendor) && strlen(product)) - sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, vendor); + snprintf(desc, 4096, _("Autosuspend for USB device %s [%s]"), product, vendor); else if (strlen(product)) - sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, name); + snprintf(desc, 4096, _("Autosuspend for USB device %s [%s]"), product, name); else if (strlen(vendor)) - sprintf(desc, _("Autosuspend for USB device %s [%s]"), vendor, name); + snprintf(desc, 4096, _("Autosuspend for USB device %s [%s]"), vendor, name); - sprintf(toggle_good, "echo 'auto' > '%s';", usb_path); - sprintf(toggle_bad, "echo 'on' > '%s';", usb_path); + snprintf(toggle_good, 4096, "echo 'auto' > '%s';", usb_path); + snprintf(toggle_bad, 4096, "echo 'on' > '%s';", usb_path); } int usb_tunable::good_bad(void) @@ -117,26 +118,26 @@ const char *usb_tunable::toggle_script(void) static void add_usb_callback(const char *d_name) { class usb_tunable *usb; - char filename[4096]; + char filename[PATH_MAX]; DIR *dir; - sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/power/control", d_name); if (access(filename, R_OK) != 0) return; - sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/power/active_duration", d_name); if (access(filename, R_OK)!=0) return; /* every interface of this device should support autosuspend */ - sprintf(filename, "/sys/bus/usb/devices/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s", d_name); if ((dir = opendir(filename))) { struct dirent *entry; while ((entry = readdir(dir))) { /* dirname: <busnum>-<devnum>...:<config num>-<interface num> */ if (!isdigit(entry->d_name[0])) continue; - sprintf(filename, "/sys/bus/usb/devices/%s/%s/supports_autosuspend", d_name, entry->d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/%s/supports_autosuspend", d_name, entry->d_name); if (access(filename, R_OK) == 0 && read_sysfs(filename) == 0) break; } @@ -145,7 +146,7 @@ static void add_usb_callback(const char *d_name) return; } - sprintf(filename, "/sys/bus/usb/devices/%s", d_name); + snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s", d_name); usb = new class usb_tunable(filename, d_name); all_tunables.push_back(usb); } diff --git a/src/tuning/tuningusb.h b/src/tuning/tuningusb.h index a257904..4e27e3a 100644 --- a/src/tuning/tuningusb.h +++ b/src/tuning/tuningusb.h @@ -26,13 +26,14 @@ #define _INCLUDE_GUARD_USB_TUNE_H #include <vector> +#include <limits.h> #include "tunable.h" using namespace std; class usb_tunable : public tunable { - char usb_path[4096]; + char usb_path[PATH_MAX]; public: usb_tunable(const char *usb_path, const char *path); |