diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-22 12:36:41 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-22 12:36:41 +0200 |
commit | b1cc33e1227d6ab47781a47f7a88da24e57f4e17 (patch) | |
tree | bbe61abb08b19dcf5b94ddb3e5afaa094cf98539 | |
parent | b21a17491ab4eab43b871da791a8a8ef84489be1 (diff) | |
download | abrt-b1cc33e1227d6ab47781a47f7a88da24e57f4e17.tar.gz abrt-b1cc33e1227d6ab47781a47f7a88da24e57f4e17.tar.xz abrt-b1cc33e1227d6ab47781a47f7a88da24e57f4e17.zip |
fix several resource leaks on error paths
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | lib/Utils/DebugDump.cpp | 34 | ||||
-rw-r--r-- | src/Hooks/CCpp.cpp | 33 |
2 files changed, 35 insertions, 32 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index 9d40e359..f841e947 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -96,19 +96,25 @@ bool CDebugDump::ExistFileDir(const std::string& pPath) bool CDebugDump::GetAndSetLock(const std::string& pLockFile, const std::string& pPID) { int fd = open(pLockFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0640); - if (fd == -1 && errno == EEXIST) + if (fd == -1) { - char pid[PID_STR_MAX + 1]; - if ((fd = open(pLockFile.c_str(), O_RDONLY)) == -1) + if (errno != EEXIST) + { + throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::GetAndSetLock(): cannot create lock file"); + } + fd = open(pLockFile.c_str(), O_RDONLY); + if (fd == -1) { throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::GetAndSetLock(): cannot get lock status"); } - int r = read(fd, pid, sizeof(pid)); + char pid[PID_STR_MAX + 1]; + int r = read(fd, pid, sizeof(pid) - 1); if (r == -1) { + close(fd); throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::GetAndSetLock(): cannot get a pid"); } - pid[r > PID_STR_MAX ? PID_STR_MAX : r] = '\0'; + pid[r] = '\0'; if (pid == pPID) { close(fd); @@ -127,12 +133,8 @@ bool CDebugDump::GetAndSetLock(const std::string& pLockFile, const std::string& close(fd); return false; } - else if (fd == -1) - { - throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::GetAndSetLock(): cannot create lock file"); - } - if (write(fd, pPID.c_str(), pPID.length()) != pPID.length()) + if (write(fd, pPID.c_str(), pPID.length()) != pPID.length()) { close(fd); remove(pLockFile.c_str()); @@ -244,9 +246,9 @@ void CDebugDump::DeleteFileDir(const std::string& pDir) } DIR *dir = opendir(pDir.c_str()); std::string fullPath; - struct dirent *dent = NULL; if (dir != NULL) { + struct dirent *dent; while ((dent = readdir(dir)) != NULL) { if (std::string(dent->d_name) != "." && std::string(dent->d_name) != "..") @@ -258,6 +260,7 @@ void CDebugDump::DeleteFileDir(const std::string& pDir) } if (remove(fullPath.c_str()) == -1) { + closedir(dir); throw CABRTException(EXCEP_DD_DELETE, "CDebugDump::DeleteFileDir(): Cannot remove file: " + fullPath); } } @@ -280,14 +283,16 @@ bool CDebugDump::IsTextFile(const std::string& pName) throw CABRTException(EXCEP_ERROR, std::string("CDebugDump::IsTextFile(): Cannot open magic cookie: ") + magic_error(m)); } - int r = magic_load(m,NULL); + int r = magic_load(m, NULL); if (r == -1) { + magic_close(m); throw CABRTException(EXCEP_ERROR, std::string("CDebugDump::IsTextFile(): Cannot load magic db: ") + magic_error(m)); } char* ch = (char *) magic_file(m, pName.c_str()); + magic_close(m); if (ch == NULL) { @@ -299,7 +304,6 @@ bool CDebugDump::IsTextFile(const std::string& pName) isText = true; } - magic_close(m); return isText; } @@ -345,7 +349,7 @@ void CDebugDump::SaveTime() { std::stringstream ss; time_t t = time(NULL); - if (((time_t) -1) == t) + if (((time_t) -1) == t) /* isn't it a bit TOO paranoid? :) */ { throw CABRTException(EXCEP_ERROR, "CDebugDump::SaveTime(): Cannot get local time."); } @@ -500,7 +504,7 @@ void CDebugDump::InitGetNextFile() bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool& pIsTextFile) { - static struct dirent *dent = NULL; + struct dirent *dent; if (m_pGetNextFileDir == NULL) { diff --git a/src/Hooks/CCpp.cpp b/src/Hooks/CCpp.cpp index 2a7e60c2..27c813fe 100644 --- a/src/Hooks/CCpp.cpp +++ b/src/Hooks/CCpp.cpp @@ -79,20 +79,23 @@ char* get_cmdline(const char* pid) FILE* fp = fopen(path, "r"); int ch; int ii = 0; - while ((ch = fgetc(fp)) != EOF) + if (fp) { - if (ch == 0) + while ((ch = fgetc(fp)) != EOF && ii < COMMAND_LINE_SIZE-1) { - cmdline[ii] = ' '; - } - else if (isspace(ch) || (isascii(ch) && !iscntrl(ch))) - { - cmdline[ii] = ch; + if (ch == 0) + { + cmdline[ii] = ' '; + } + else if (isspace(ch) || (isascii(ch) && !iscntrl(ch))) + { + cmdline[ii] = ch; + } + ii++; } - ii++; + fclose(fp); } cmdline[ii] = '\0'; - fclose(fp); return strdup(cmdline); } @@ -103,23 +106,19 @@ int daemon_is_ok() char pid[PID_MAX]; char path[PATH_MAX]; struct stat buff; - FILE* fp; - if ((fp = fopen(VAR_RUN_PID_FILE, "r")) == NULL) + FILE* fp = fopen(VAR_RUN_PID_FILE, "r"); + if (fp == NULL) { return 0; } fgets(pid, sizeof(pid), fp); - if (strrchr(pid, '\n') != NULL) - { - char* newline = strrchr(pid, '\n'); - *newline = '\0'; - } + fclose(fp); + *strchrnul(pid, '\n') = '\0'; snprintf(path, sizeof(path), "/proc/%s/stat", pid); if (stat(path, &buff) == -1) { return 0; } - fclose(fp); return 1; } |