Re: [Powertop] [bugreport] crash on powertop --debug --html=file.html

Sergey Senozhatsky <sergey.senozhatsky at gmail.com> Fri, 06 Jul 2012 12:00:04 +0300
Newsgroups dev.linux.lists.powertop
Message-ID <20120706090004.GA4186@swordfish>
Hi,

Could you please give it a try? (just a bunch of suspects).
If it fails, it'd be nice to see valgrind output.

---

 src/devlist.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/devlist.cpp b/src/devlist.cpp
index 93f2081..e65981e 100644
--- a/src/devlist.cpp
+++ b/src/devlist.cpp
@@ -89,7 +89,6 @@ void collect_open_devices(void)
 	}
 	target->resize(0);
 
-
 	dir = opendir("/proc/");
 	if (!dir)
 		return;
@@ -105,7 +104,7 @@ void collect_open_devices(void)
 		if (strcmp(entry->d_name, "self") == 0)
 			continue;
 
-		sprintf(filename, "/proc/%s/fd/", entry->d_name);
+		snprintf(filename, 4095, "/proc/%s/fd/", entry->d_name);
 
 		dir2 = opendir(filename);
 		if (!dir2)
@@ -113,12 +112,14 @@ void collect_open_devices(void)
 		while (1) {
 			int ret;
 			struct devuser * dev;
+			std::string comm;
+
 			entry2 = readdir(dir2);
 			if (!entry2)
 				break;
 			if (entry2->d_name[0] == '.')
 				continue;
-			sprintf(filename, "/proc/%s/fd/%s", entry->d_name, entry2->d_name);
+			snprintf(filename, 4095, "/proc/%s/fd/%s", entry->d_name, entry2->d_name);
 			memset(link, 0, 4096);
 			ret = readlink(filename, link, 4095);
 			if (ret < 0)
@@ -147,10 +148,14 @@ void collect_open_devices(void)
 					continue;
 				dev->pid = strtoull(entry->d_name, NULL, 10);
 				strncpy(dev->device, link, 251);
-				sprintf(filename, "/proc/%s/comm", entry->d_name);
-				strncpy(dev->comm, read_sysfs_string("/proc/%s/comm", entry->d_name).c_str(), 31);
+				dev->device[251] = 0x00;
+				comm = read_sysfs_string("/proc/%s/comm", entry->d_name);
+				if (comm.empty())
+					dev->comm[0] = 0x00;
+				else
+					strncpy(dev->comm, read_sysfs_string("/proc/%s/comm", entry->d_name).c_str(), 31);
+				dev->comm[31] = 0x00;
 				target->push_back(dev);
-
 			}
 		}
 		closedir(dir2);