snprintf, PATH_MAX + improvement of handling reporting filenames
A message that is not in the archive (sent off-list or not yet received): [email protected]
Jaroslav Skarvada
PATCH 1/1 snprintf, PATH_MAX + improvement of handling reporting filenames
Hi,
I have noticed there are some sprintf that may lead to buffer overflows, e.g. the following can crash powertop:
# powertop --csv=`printf 'a%.0s' {1..5000}`I attempted to fix hopefully most of them by converting the code to snprintf. For data returned by kernel it's probably unlikely to cause overflows, but why not cover it all. I also tried to unify buffer sizes for paths/filenames to PATH_MAX.
The second patch tries to improve handling of reporting filenames. It works the following way:
powertop --html
generates 'powertop.html' file
powertop --html=myfile.suffix
generates 'myfile.suffix' file
powertop -i 2 --html
generates 'powertop-TIMESTAMPS.html' files
powertop -i 2 --html=myfile.suffix
generates 'myfile-TIMESTAMPS.suffix' files
powertop -i 2 --html=myfile
generates 'myfile-TIMESTAMPS' files
Similarly for CSV.
I think this is more logical behavior
regards
Jaroslav
From b0d84471c1d87f1386b94232a9392ed6fe004bb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <[email protected]>
Date: Tue, 12 May 2015 17:19:34 +0200
Subject: [PATCH 1/2] Use PATH_MAX from limits.h and snprintf insteaf of
sprintf
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
--- src/calibrate/calibrate.cpp | 23 +++++++++++----------- src/cpu/abstract_cpu.cpp | 9 +++++---- src/cpu/intel_cpus.cpp | 9 +++++---- src/devices/ahci.cpp | 43 +++++++++++++++++++++--------------------- src/devices/ahci.h | 3 ++- src/devices/alsa.cpp | 32 +++++++++++++++---------------- src/devices/alsa.h | 3 ++- src/devices/backlight.cpp | 23 +++++++++++----------- src/devices/backlight.h | 3 ++- src/devices/i915-gpu.cpp | 3 ++- src/devices/network.h | 3 ++- src/devices/rfkill.cpp | 37 ++++++++++++++++++------------------ src/devices/rfkill.h | 3 ++- src/devices/runtime_pm.cpp | 35 +++++++++++++++++----------------- src/devices/runtime_pm.h | 3 ++- src/devices/thinkpad-fan.cpp | 3 ++- src/devices/thinkpad-light.cpp | 3 ++- src/devices/usb.cpp | 43 +++++++++++++++++++++--------------------- src/devices/usb.h | 3 ++- src/devlist.cpp | 11 ++++++----- src/lib.cpp | 5 +++-- src/main.cpp | 5 +++-- src/measurement/acpi.cpp | 5 +++-- src/measurement/sysfs.cpp | 5 +++-- src/parameters/parameters.cpp | 3 ++- src/report/report.cpp | 9 +++++---- src/report/report.h | 3 ++- src/tuning/runtime.cpp | 9 +++++---- src/tuning/runtime.h | 3 ++- src/tuning/tuningi2c.cpp | 31 +++++++++++++++--------------- src/tuning/tuningi2c.h | 3 ++- src/tuning/tuningsysfs.cpp | 7 ++++--- src/tuning/tuningsysfs.h | 3 ++- src/tuning/tuningusb.cpp | 33 ++++++++++++++++---------------- src/tuning/tuningusb.h | 3 ++- 35 files changed, 228 insertions(+), 194 deletions(-)
src/calibrate/calibrate.cpp +12 -11
@@ -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;
src/cpu/abstract_cpu.cpp +5 -4
@@ -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();
src/cpu/intel_cpus.cpp +5 -4
@@ -35,6 +35,7 @@ #include <string.h> #include <errno.h> #include <unistd.h> +#include <limits.h> #include "../lib.h" #include "../parameters/parameters.h"
@@ -144,7 +145,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();
@@ -170,7 +171,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);
@@ -491,7 +492,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();
@@ -503,7 +504,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);
src/devices/ahci.cpp +22 -21
146 lines not on this page · raw message
src/devices/ahci.h +2 -1
@@ -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;
src/devices/alsa.cpp +16 -16
@@ -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);
src/devices/alsa.h +2 -1
@@ -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 ];
src/devices/backlight.cpp +12 -11
@@ -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); }
src/devices/backlight.h +2 -1
@@ -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;
src/devices/i915-gpu.cpp +2 -1
@@ -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;
src/devices/network.h +2 -1
@@ -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;
src/devices/rfkill.cpp +19 -18
@@ -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); }
src/devices/rfkill.h +2 -1
@@ -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;
src/devices/runtime_pm.cpp +18 -17
@@ -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); }
src/devices/runtime_pm.h +2 -1
@@ -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;
src/devices/thinkpad-fan.cpp +2 -1
@@ -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");
src/devices/thinkpad-light.cpp +2 -1
@@ -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");
src/devices/usb.cpp +22 -21
135 lines not on this page · raw message
src/devices/usb.h +2 -1
@@ -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 ];
src/devlist.cpp +6 -5
@@ -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;
src/lib.cpp +3 -2
@@ -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)
src/main.cpp +3 -2
@@ -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);
src/measurement/acpi.cpp +3 -2
@@ -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)
src/measurement/sysfs.cpp +3 -2
@@ -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;
src/parameters/parameters.cpp +2 -1
@@ -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);
src/report/report.cpp +5 -4
@@ -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");
src/report/report.h +2 -1
@@ -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;
src/tuning/runtime.cpp +5 -4
@@ -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);
src/tuning/runtime.h +2 -1
@@ -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);
src/tuning/tuningi2c.cpp +16 -15
@@ -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);
src/tuning/tuningi2c.h +2 -1
@@ -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);
src/tuning/tuningsysfs.cpp +4 -3
@@ -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"); }
src/tuning/tuningsysfs.h +2 -1
@@ -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:
src/tuning/tuningusb.cpp +17 -16
@@ -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); }
src/tuning/tuningusb.h +2 -1
@@ -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);
Signature · 1 line
2.1.0
From cd511d5b8edf2f1a53dcc4bd839bdb676e60258b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <[email protected]>
Date: Tue, 12 May 2015 18:06:56 +0200
Subject: [PATCH 2/2] Improve handling of reporting filenames
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
powertop --html
generates 'powertop.html' file
powertop --html=myfile.suffix
generates 'myfile.suffix' file
powertop -i 2 --html
generates 'powertop-TIMESTAMPS.html' files
powertop -i 2 --html=myfile.suffix
generates 'myfile-TIMESTAMPS.suffix' files
powertop -i 2 --html=myfile
generates 'myfile-TIMESTAMPS' files
Similarly for CSV.
--- src/main.cpp | 10 ++++++++++ src/report/report.cpp | 35 +++++++++++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-)
src/main.cpp +10 -0
@@ -420,6 +420,11 @@ int main(int argc, char **argv) case 'C': /* csv report */ reporttype = REPORT_CSV; sprintf(filename, "%s", optarg ? optarg : "powertop.csv"); + if (!strlen(filename)) + { + fprintf(stderr, _("Invalid CSV filename\n")); + exit(1); + } break; case OPT_DEBUG: /* implemented using getopt_long(3) flag */
@@ -431,6 +436,11 @@ int main(int argc, char **argv) case 'r': /* html report */ reporttype = REPORT_HTML; sprintf(filename, "%s", optarg ? optarg : "powertop.html"); + if (!strlen(filename)) + { + fprintf(stderr, _("Invalid HTML filename\n")); + exit(1); + } break; case 'i': iterations = (optarg ? atoi(optarg) : 1);
src/report/report.cpp +17 -18
@@ -169,28 +169,27 @@ static void system_info(void) void init_report_output(char *filename_str, int iterations) { size_t period; - char file_prefix[PATH_MAX]; - char file_postfix[8]; + string filename; time_t stamp; char datestr[200]; - string mystring = string(filename_str); - sprintf(file_postfix, "%s", - (reporttype == REPORT_HTML ? "html" : "csv")); - period=mystring.find_last_of("."); - 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) - snprintf(reportout.filename, PATH_MAX, "%s-%s.%s", - file_prefix, datestr,file_postfix); + if (iterations == 1) + snprintf(reportout.filename, PATH_MAX, "%s", filename_str); else - snprintf(reportout.filename, PATH_MAX, "%s.%s", - file_prefix, file_postfix); - + { + filename = string(filename_str); + period = filename.find_last_of("."); + if (period > filename.length()) + period = filename.length(); + 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)); + snprintf(reportout.filename, PATH_MAX, "%s-%s%s", + filename.substr(0, period).c_str(), datestr, + filename.substr(period).c_str()); + } + reportout.report_file = fopen(reportout.filename, "wm"); if (!reportout.report_file) { fprintf(stderr, _("Cannot open output file %s (%s)\n"),
Signature · 1 line
2.1.0
Jaroslav Skarvada
Patch adding more buffer limits
regards
Jaroslav
From 323374324466361bf69997b7c40d358ce0bb2385 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <[email protected]>
Date: Tue, 12 May 2015 19:12:02 +0200
Subject: [PATCH 3/3] More sprintf fixes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
--- src/cpu/abstract_cpu.cpp | 2 +- src/cpu/cpu.cpp | 6 +++--- src/cpu/cpu_linux.cpp | 18 +++++++++--------- src/devices/ahci.cpp | 12 ++++++------ src/devices/devfreq.cpp | 2 +- src/devlist.cpp | 2 +- src/lib.cpp | 8 ++++---- src/main.cpp | 6 +++--- src/parameters/parameters.cpp | 4 ++-- src/tuning/ethernet.cpp | 2 +- src/tuning/runtime.cpp | 10 +++++----- src/tuning/tuningsysfs.cpp | 4 ++-- src/tuning/wifi.cpp | 4 ++-- 13 files changed, 40 insertions(+), 40 deletions(-)
src/cpu/abstract_cpu.cpp +1 -1
@@ -112,7 +112,7 @@ void abstract_cpu::measurement_start(void) old_idle = true; - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_available_frequencies", number); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_available_frequencies", number); file.open(filename, ios::in); if (file) { file >> max_frequency;
src/cpu/cpu.cpp +3 -3
@@ -151,20 +151,20 @@ static class abstract_cpu * new_cpu(int number, char * vendor, int family, int m static void handle_one_cpu(unsigned int number, char *vendor, int family, int model) { - char filename[1024]; + char filename[PATH_MAX]; ifstream file; unsigned int package_number = 0; unsigned int core_number = 0; class abstract_cpu *package, *core, *cpu; - sprintf(filename, "/sys/devices/system/cpu/cpu%i/topology/core_id", number); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/topology/core_id", number); file.open(filename, ios::in); if (file) { file >> core_number; file.close(); } - sprintf(filename, "/sys/devices/system/cpu/cpu%i/topology/physical_package_id", number); + snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%i/topology/physical_package_id", number); file.open(filename, ios::in); if (file) { file >> package_number;
src/cpu/cpu_linux.cpp +9 -9
@@ -46,7 +46,7 @@ void cpu_linux::parse_cstates_start(void) char filename[256]; int len; - len = sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpuidle", number); + len = snprintf(filename, 256, "/sys/devices/system/cpu/cpu%i/cpuidle", number); dir = opendir(filename); if (!dir)
@@ -67,7 +67,7 @@ void cpu_linux::parse_cstates_start(void) strcpy(linux_name, entry->d_name); strcpy(human_name, linux_name); - sprintf(filename + len, "/%s/name", entry->d_name); + snprintf(filename + len, 256 - len, "/%s/name", entry->d_name); file.open(filename, ios::in); if (file) {
@@ -78,14 +78,14 @@ void cpu_linux::parse_cstates_start(void) if (strcmp(human_name, "C0")==0) strcpy(human_name, _("C0 polling")); - sprintf(filename + len, "/%s/usage", entry->d_name); + snprintf(filename + len, 256 - len, "/%s/usage", entry->d_name); file.open(filename, ios::in); if (file) { file >> usage; file.close(); } - sprintf(filename + len, "/%s/time", entry->d_name); + snprintf(filename + len, 256 - len, "/%s/time", entry->d_name); file.open(filename, ios::in); if (file) {
@@ -112,7 +112,7 @@ void cpu_linux::parse_pstates_start(void) if (children[i]) children[i]->wiggle(); - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); + snprintf(filename, 256, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", first_cpu); file.open(filename, ios::in);
@@ -145,7 +145,7 @@ void cpu_linux::parse_cstates_end(void) ifstream file; int len; - len = sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpuidle", number); + len = snprintf(filename, 256, "/sys/devices/system/cpu/cpu%i/cpuidle", number); dir = opendir(filename); if (!dir)
@@ -167,14 +167,14 @@ void cpu_linux::parse_cstates_end(void) strcpy(human_name, linux_name); - sprintf(filename + len, "/%s/usage", entry->d_name); + snprintf(filename + len, 256 - len, "/%s/usage", entry->d_name); file.open(filename, ios::in); if (file) { file >> usage; file.close(); } - sprintf(filename + len, "/%s/time", entry->d_name); + snprintf(filename + len, 256 - len, "/%s/time", entry->d_name); file.open(filename, ios::in); if (file) {
@@ -194,7 +194,7 @@ void cpu_linux::parse_pstates_end(void) char filename[256]; ifstream file; - sprintf(filename, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", number); + snprintf(filename, 256, "/sys/devices/system/cpu/cpu%i/cpufreq/stats/time_in_state", number); file.open(filename, ios::in);
src/devices/ahci.cpp +6 -6
@@ -51,7 +51,7 @@ static string disk_name(char *path, char *target, char *shortname) char pathname[PATH_MAX]; string diskname = ""; - sprintf(pathname, "%s/%s", path, target); + snprintf(pathname, PATH_MAX, "%s/%s", path, target); dir = opendir(pathname); if (!dir) return diskname;
@@ -65,7 +65,7 @@ static string disk_name(char *path, char *target, char *shortname) if (!strchr(dirent->d_name, ':')) continue; - sprintf(line, "%s/%s/model", pathname, dirent->d_name); + snprintf(line, PATH_MAX, "%s/%s/model", pathname, dirent->d_name); file = fopen(line, "r"); if (file) { if (fgets(line, 4096, file) == NULL) {
@@ -92,7 +92,7 @@ static string model_name(char *path, char *shortname) struct dirent *dirent; char pathname[PATH_MAX]; - sprintf(pathname, "%s/device", path); + snprintf(pathname, PATH_MAX, "%s/device", path); dir = opendir(pathname); if (!dir)
@@ -168,20 +168,20 @@ void ahci::start_measurement(void) file >> start_active; } file.close(); - sprintf(filename, "%s/ahci_alpm_partial", sysfs_path); + snprintf(filename, PATH_MAX, "%s/ahci_alpm_partial", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_partial; } file.close(); - sprintf(filename, "%s/ahci_alpm_slumber", sysfs_path); + snprintf(filename, PATH_MAX, "%s/ahci_alpm_slumber", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_slumber; } file.close(); - sprintf(filename, "%s/ahci_alpm_devslp", sysfs_path); + snprintf(filename, PATH_MAX, "%s/ahci_alpm_devslp", sysfs_path); file.open(filename, ios::in); if (file) { file >> start_devslp;
src/devices/devfreq.cpp +1 -1
@@ -123,7 +123,7 @@ void devfreq::parse_devfreq_trans_stat(char *dname) ifstream file; char filename[256]; - sprintf(filename, "/sys/class/devfreq/%s/trans_stat", dir_name); + snprintf(filename, 256, "/sys/class/devfreq/%s/trans_stat", dir_name); file.open(filename); if (!file)
src/devlist.cpp +1 -1
@@ -125,7 +125,7 @@ void collect_open_devices(void) if (strcmp(entry->d_name, "self") == 0) continue; - sprintf(filename, "/proc/%s/fd/", entry->d_name); + snprintf(filename, PATH_MAX, "/proc/%s/fd/", entry->d_name); dir2 = opendir(filename); if (!dir2)
src/lib.cpp +4 -4
@@ -475,10 +475,10 @@ int read_msr(int cpu, uint64_t offset, uint64_t *value) int fd; char msr_path[256]; - sprintf(msr_path, "/dev/cpu/%d/msr", cpu); + snprintf(msr_path, 256, "/dev/cpu/%d/msr", cpu); if (access(msr_path, R_OK) != 0){ - sprintf(msr_path, "/dev/msr%d", cpu); + snprintf(msr_path, 256, "/dev/msr%d", cpu); if (access(msr_path, R_OK) != 0){ fprintf(stderr,
@@ -507,10 +507,10 @@ int write_msr(int cpu, uint64_t offset, uint64_t value) int fd; char msr_path[256]; - sprintf(msr_path, "/dev/cpu/%d/msr", cpu); + snprintf(msr_path, 256, "/dev/cpu/%d/msr", cpu); if (access(msr_path, R_OK) != 0){ - sprintf(msr_path, "/dev/msr%d", cpu); + snprintf(msr_path, 256, "/dev/msr%d", cpu); if (access(msr_path, R_OK) != 0){ fprintf(stderr,
src/main.cpp +3 -3
@@ -419,7 +419,7 @@ int main(int argc, char **argv) break; case 'C': /* csv report */ reporttype = REPORT_CSV; - sprintf(filename, "%s", optarg ? optarg : "powertop.csv"); + snprintf(filename, PATH_MAX, "%s", optarg ? optarg : "powertop.csv"); if (!strlen(filename)) { fprintf(stderr, _("Invalid CSV filename\n"));
@@ -435,7 +435,7 @@ int main(int argc, char **argv) break; case 'r': /* html report */ reporttype = REPORT_HTML; - sprintf(filename, "%s", optarg ? optarg : "powertop.html"); + snprintf(filename, PATH_MAX, "%s", optarg ? optarg : "powertop.html"); if (!strlen(filename)) { fprintf(stderr, _("Invalid HTML filename\n"));
@@ -453,7 +453,7 @@ int main(int argc, char **argv) time_out = (optarg ? atoi(optarg) : 20); break; case 'w': /* measure workload */ - sprintf(workload, "%s", optarg ? optarg : ""); + snprintf(workload, PATH_MAX, "%s", optarg ? optarg : ""); break; case 'V': print_version();
src/parameters/parameters.cpp +2 -2
@@ -454,9 +454,9 @@ char* get_param_directory(const char *filename) static char tempfilename[PATH_MAX]; if (access("/var/cache/powertop", W_OK ) == 0) - sprintf(tempfilename, "/var/cache/powertop/%s", filename); + snprintf(tempfilename, PATH_MAX, "/var/cache/powertop/%s", filename); if (access("/data/local/powertop", W_OK ) == 0) - sprintf(tempfilename, "/data/local/powertop/%s", filename); + snprintf(tempfilename, PATH_MAX, "/data/local/powertop/%s", filename); return tempfilename; };
src/tuning/ethernet.cpp +1 -1
@@ -52,7 +52,7 @@ ethernet_tunable::ethernet_tunable(const char *iface) : tunable("", 0.3, _("Good memset(interf, 0, sizeof(interf)); strncpy(interf, iface, sizeof(interf)); sprintf(desc, _("Wake-on-lan status for device %s"), iface); - sprintf(toggle_good, "ethtool -s %s wol d;", iface); + snprintf(toggle_good, 4096, "ethtool -s %s wol d;", iface); }
src/tuning/runtime.cpp +5 -5
@@ -50,10 +50,10 @@ runtime_tunable::runtime_tunable(const char *path, const char *bus, const char * sprintf(desc, _("%s device %s has no runtime power management"), bus, dev); if (strcmp(bus, "pci") == 0) { - char filename[4096]; + char filename[PATH_MAX]; uint16_t vendor = 0, device = 0; - sprintf(filename, "/sys/bus/%s/devices/%s/vendor", bus, dev); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/vendor", bus, dev); file.open(filename, ios::in); if (file) {
@@ -62,7 +62,7 @@ runtime_tunable::runtime_tunable(const char *path, const char *bus, const char * } - sprintf(filename, "/sys/bus/%s/devices/%s/device", bus, dev); + snprintf(filename, PATH_MAX, "/sys/bus/%s/devices/%s/device", bus, dev); file.open(filename, ios::in); if (file) { file >> hex >> device;
@@ -78,8 +78,8 @@ runtime_tunable::runtime_tunable(const char *path, const char *bus, const char * } - sprintf(toggle_good, "echo 'auto' > '%s';", runtime_path); - sprintf(toggle_bad, "echo 'on' > '%s';", runtime_path); + snprintf(toggle_good, 4096, "echo 'auto' > '%s';", runtime_path); + snprintf(toggle_bad, 4096, "echo 'on' > '%s';", runtime_path); } int runtime_tunable::good_bad(void)
src/tuning/tuningsysfs.cpp +2 -2
@@ -47,8 +47,8 @@ sysfs_tunable::sysfs_tunable(const char *str, const char *_sysfs_path, const cha strcpy(sysfs_path, _sysfs_path); strcpy(target_value, _target_content); bad_value[0] = 0; - sprintf(toggle_good, "echo '%s' > '%s';", target_value, sysfs_path); - sprintf(toggle_bad, "echo '%s' > '%s';", bad_value, sysfs_path); + snprintf(toggle_good, 4096, "echo '%s' > '%s';", target_value, sysfs_path); + snprintf(toggle_bad, 4096, "echo '%s' > '%s';", bad_value, sysfs_path); } int sysfs_tunable::good_bad(void)
src/tuning/wifi.cpp +2 -2
@@ -47,8 +47,8 @@ wifi_tunable::wifi_tunable(const char *_iface) : tunable("", 1.5, _("Good"), _(" strcpy(iface, _iface); sprintf(desc, _("Wireless Power Saving for interface %s"), iface); - sprintf(toggle_good, "iw dev %s set power_save on", iface); - sprintf(toggle_bad, "iw dev %s set power_save off", iface); + snprintf(toggle_good, 4096, "iw dev %s set power_save on", iface); + snprintf(toggle_bad, 4096, "iw dev %s set power_save off", iface); } int wifi_tunable::good_bad(void)
Signature · 1 line
2.1.0
Alexandra Yates
Your patch was added
Quoted 14 lines · Jaroslav Skarvada
Patch adding more buffer limits
regards
Jaroslav
_______________________________________________
PowerTop mailing list
PowerTop(a)lists.01.org
https://lists.01.org/mailman/listinfo/powertop
Signature · 2 lines
Thank you, <Alexandra>
Alexandra Yates
Your patch was added.
Quoted 46 lines · Jaroslav Skarvada
Hi,
I have noticed there are some sprintf that may lead to buffer overflows, e.g. the following can crash powertop:
# powertop --csv=`printf 'a%.0s' {1..5000}`I attempted to fix hopefully most of them by converting the code to snprintf. For data returned by kernel it's probably unlikely to cause overflows, but why not cover it all. I also tried to unify buffer sizes for paths/filenames to
PATH_MAX.The second patch tries to improve handling of reporting filenames. It works the following way:
powertop --html
generates 'powertop.html' filepowertop --html=myfile.suffix
generates 'myfile.suffix' filepowertop -i 2 --html
generates 'powertop-TIMESTAMPS.html' filespowertop -i 2 --html=myfile.suffix
generates 'myfile-TIMESTAMPS.suffix' filespowertop -i 2 --html=myfile
generates 'myfile-TIMESTAMPS' filesSimilarly for CSV.
I think this is more logical behavior
regards
Jaroslav
_______________________________________________
PowerTop mailing list
PowerTop(a)lists.01.org
https://lists.01.org/mailman/listinfo/powertop
Signature · 2 lines
Thank you, <Alexandra>