fixes issues #144, #145:

* adds a try catch block surrounding read_file in the method getcmdline.
  To avoid crashes if a pid is removed while reading was not finished.
This commit is contained in:
Andreas Wieland
2019-06-12 14:38:27 +02:00
parent ea8ef8af27
commit 9dc99c9e14

View File

@@ -121,9 +121,21 @@ std::string getcmdline(pid_t pid) {
char filename[maxfilenamelen];
std::snprintf(filename, maxfilenamelen, "/proc/%d/cmdline", pid);
std::string cmdline;
bool replace_null = false;
std::string cmdline = read_file(filename);
try {
cmdline = read_file(filename);
} catch (int e) {
std::fprintf(stderr, "An exception occurred. Exception Nr %i \n", e);
cmdline = "";
}
if (cmdline.empty() || cmdline[cmdline.length() - 1] != '\0') {
// invalid content of cmdline file. Add null char to allow further
// processing.
cmdline.append(1, '\0');
return cmdline;
}
// join parameters, keep prgname separate, don't overwrite trailing null
for (size_t idx = 0; idx < (cmdline.length() - 1); idx++) {
@@ -134,13 +146,6 @@ std::string getcmdline(pid_t pid) {
replace_null = true;
}
}
if (cmdline.length() == 0 || (cmdline[cmdline.length() - 1] != 0x00)) {
// invalid content of cmdline file. Add null char to allow further
// processing.
cmdline.append("\0");
}
return cmdline;
}