All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [Powertop] [Patches] snprintf, PATH_MAX + improvement of handling reporting filenames
@ 2015-09-18  0:30 Alexandra Yates
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandra Yates @ 2015-09-18  0:30 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 1272 bytes --]

Your patch was added.

On 05/12/2015 09:35 AM, Jaroslav Skarvada wrote:
> 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
>
>
>
> _______________________________________________
> PowerTop mailing list
> PowerTop(a)lists.01.org
> https://lists.01.org/mailman/listinfo/powertop
>

-- 
Thank you,
<Alexandra>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Powertop] [Patches] snprintf, PATH_MAX + improvement of handling reporting filenames
@ 2015-09-18  0:27 Alexandra Yates
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandra Yates @ 2015-09-18  0:27 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 338 bytes --]

Your patch was added

On 05/12/2015 10:15 AM, Jaroslav Skarvada wrote:
>
> Patch adding more buffer limits
>
> regards
>
> Jaroslav
>
>
>
> _______________________________________________
> PowerTop mailing list
> PowerTop(a)lists.01.org
> https://lists.01.org/mailman/listinfo/powertop
>

-- 
Thank you,
<Alexandra>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Powertop] [Patches] snprintf, PATH_MAX + improvement of handling reporting filenames
@ 2015-05-12 17:15 Jaroslav Skarvada
  0 siblings, 0 replies; 4+ messages in thread
From: Jaroslav Skarvada @ 2015-05-12 17:15 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 58 bytes --]


Patch adding more buffer limits

regards

Jaroslav

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0003-More-sprintf-fixes.patch --]
[-- Type: text/x-patch, Size: 13787 bytes --]

From 323374324466361bf69997b7c40d358ce0bb2385 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <jskarvad@redhat.com>
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

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
---
 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(-)

diff --git a/src/cpu/abstract_cpu.cpp b/src/cpu/abstract_cpu.cpp
index 0d15b3f..17acb71 100644
--- a/src/cpu/abstract_cpu.cpp
+++ b/src/cpu/abstract_cpu.cpp
@@ -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;
diff --git a/src/cpu/cpu.cpp b/src/cpu/cpu.cpp
index 4e040c1..ee87c04 100644
--- a/src/cpu/cpu.cpp
+++ b/src/cpu/cpu.cpp
@@ -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;
diff --git a/src/cpu/cpu_linux.cpp b/src/cpu/cpu_linux.cpp
index e1ff165..6d0fa62 100644
--- a/src/cpu/cpu_linux.cpp
+++ b/src/cpu/cpu_linux.cpp
@@ -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);
 
diff --git a/src/devices/ahci.cpp b/src/devices/ahci.cpp
index a394f98..05c8e06 100644
--- a/src/devices/ahci.cpp
+++ b/src/devices/ahci.cpp
@@ -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;
diff --git a/src/devices/devfreq.cpp b/src/devices/devfreq.cpp
index f652a57..afa9bb6 100644
--- a/src/devices/devfreq.cpp
+++ b/src/devices/devfreq.cpp
@@ -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)
diff --git a/src/devlist.cpp b/src/devlist.cpp
index 787f5f6..bf48f23 100644
--- a/src/devlist.cpp
+++ b/src/devlist.cpp
@@ -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)
diff --git a/src/lib.cpp b/src/lib.cpp
index 5148bd8..29f109f 100644
--- a/src/lib.cpp
+++ b/src/lib.cpp
@@ -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,
diff --git a/src/main.cpp b/src/main.cpp
index 2709e71..2a2a6e9 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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();
diff --git a/src/parameters/parameters.cpp b/src/parameters/parameters.cpp
index 1c214f9..511cdcf 100644
--- a/src/parameters/parameters.cpp
+++ b/src/parameters/parameters.cpp
@@ -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;
 };
diff --git a/src/tuning/ethernet.cpp b/src/tuning/ethernet.cpp
index 3d36112..da04711 100644
--- a/src/tuning/ethernet.cpp
+++ b/src/tuning/ethernet.cpp
@@ -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);
 
 }
 
diff --git a/src/tuning/runtime.cpp b/src/tuning/runtime.cpp
index dcceb1a..e226695 100644
--- a/src/tuning/runtime.cpp
+++ b/src/tuning/runtime.cpp
@@ -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)
diff --git a/src/tuning/tuningsysfs.cpp b/src/tuning/tuningsysfs.cpp
index 8d55969..811977d 100644
--- a/src/tuning/tuningsysfs.cpp
+++ b/src/tuning/tuningsysfs.cpp
@@ -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)
diff --git a/src/tuning/wifi.cpp b/src/tuning/wifi.cpp
index 77cdfcc..2763b43 100644
--- a/src/tuning/wifi.cpp
+++ b/src/tuning/wifi.cpp
@@ -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)
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [Powertop] [Patches] snprintf, PATH_MAX + improvement of handling reporting filenames
@ 2015-05-12 16:35 Jaroslav Skarvada
  0 siblings, 0 replies; 4+ messages in thread
From: Jaroslav Skarvada @ 2015-05-12 16:35 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 937 bytes --]

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-PATH_MAX-from-limits.h-and-snprintf-insteaf-of-s.patch --]
[-- Type: text/x-patch, Size: 53869 bytes --]

From b0d84471c1d87f1386b94232a9392ed6fe004bb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <jskarvad@redhat.com>
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

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
---
 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(-)

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 1f3647a..a50539d 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"
@@ -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);
 
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);
 
-- 
2.1.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Improve-handling-of-reporting-filenames.patch --]
[-- Type: text/x-patch, Size: 3402 bytes --]

From cd511d5b8edf2f1a53dcc4bd839bdb676e60258b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <jskarvad@redhat.com>
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.

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
---
 src/main.cpp          | 10 ++++++++++
 src/report/report.cpp | 35 +++++++++++++++++------------------
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index 16acc73..2709e71 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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);
diff --git a/src/report/report.cpp b/src/report/report.cpp
index 3572200..a09a3a8 100644
--- a/src/report/report.cpp
+++ b/src/report/report.cpp
@@ -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"),
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-09-18  0:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-18  0:30 [Powertop] [Patches] snprintf, PATH_MAX + improvement of handling reporting filenames Alexandra Yates
  -- strict thread matches above, loose matches on Subject: below --
2015-09-18  0:27 Alexandra Yates
2015-05-12 17:15 Jaroslav Skarvada
2015-05-12 16:35 Jaroslav Skarvada

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.