Fix file descriptor leak in LinuxProcessList_readCmdlineFile after xread failure

Found by Coverity
This commit is contained in:
Christian Göttsche 2020-11-24 19:54:25 +01:00
parent 21e3063e2e
commit 4af8c63f63
1 changed files with 3 additions and 1 deletions

View File

@ -1023,7 +1023,6 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
xSnprintf(filename, MAX_NAME, "%s/%s/comm", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/comm", dirname, name);
if ((fd = open(filename, O_RDONLY)) != -1 && if ((fd = open(filename, O_RDONLY)) != -1 &&
(amtRead = xread(fd, command, sizeof(command) - 1)) > 0) { (amtRead = xread(fd, command, sizeof(command) - 1)) > 0) {
close(fd);
command[amtRead - 1] = 0; command[amtRead - 1] = 0;
lp->mergedCommand.maxLen += amtRead - 1; /* accomodate comm */ lp->mergedCommand.maxLen += amtRead - 1; /* accomodate comm */
if (!lp->procComm || strcmp(command, lp->procComm)) { if (!lp->procComm || strcmp(command, lp->procComm)) {
@ -1037,6 +1036,9 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
lp->mergedCommand.commChanged = true; lp->mergedCommand.commChanged = true;
} }
if (fd != -1)
close(fd);
/* execve could change /proc/[pid]/exe, so procExe should be udpated */ /* execve could change /proc/[pid]/exe, so procExe should be udpated */
xSnprintf(command, sizeof(command), "%s/%s/exe", dirname, name); xSnprintf(command, sizeof(command), "%s/%s/exe", dirname, name);
if ((amtRead = readlink(command, filename, sizeof(filename) - 1)) > 0) { if ((amtRead = readlink(command, filename, sizeof(filename) - 1)) > 0) {