From: Gunnar Beutner Date: Wed, 5 Feb 2014 12:53:56 +0000 (+0100) Subject: Fix memory leak in Utility::GlobRecursive. X-Git-Tag: v0.0.7~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=984ffb74219a44f82b5cb8b6177ebf60208b79e0;p=icinga2 Fix memory leak in Utility::GlobRecursive. Fixes #5604 --- diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index e55c46c79..de010ac4d 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -477,10 +477,13 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo while (dirp) { dirent ent, *pent; - if (readdir_r(dirp, &ent, &pent) < 0) + if (readdir_r(dirp, &ent, &pent) < 0) { + closedir(dirp); + BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("readdir_r") << boost::errinfo_errno(errno)); + } if (!pent) break; @@ -492,18 +495,24 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo struct stat statbuf; - if (lstat(cpath.CStr(), &statbuf) < 0) + if (lstat(cpath.CStr(), &statbuf) < 0) { + closedir(dirp); + BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("lstat") << boost::errinfo_errno(errno)); + } if (S_ISDIR(statbuf.st_mode)) GlobRecursive(cpath, pattern, callback, type); - if (stat(cpath.CStr(), &statbuf) < 0) + if (stat(cpath.CStr(), &statbuf) < 0) { + closedir(dirp); + BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("stat") << boost::errinfo_errno(errno)); + } if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode)) continue; @@ -519,6 +528,8 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo callback(cpath); } + + closedir(dirp); #endif /* _WIN32 */ return true;