From ac4ee13bd59f552a823d78590279ee49443c7c2b Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Fri, 15 Nov 2024 09:35:45 -0700 Subject: [PATCH] fix memory leak for remove_all(). (#201) * fix memory leak for remove_all(). Signed-off-by: Tomoya Fujita --- src/filesystem_helper.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/filesystem_helper.cpp b/src/filesystem_helper.cpp index b91cca4..5a40da4 100644 --- a/src/filesystem_helper.cpp +++ b/src/filesystem_helper.cpp @@ -64,6 +64,7 @@ #endif #include "rcutils/env.h" +#include "rcpputils/scope_exit.hpp" #include "rcpputils/split.hpp" namespace rcpputils @@ -474,6 +475,10 @@ bool remove_all(const path & p) return 0 == ret && false == file_options.fAnyOperationsAborted; #else DIR * dir = opendir(p.string().c_str()); + if (dir == nullptr) { + return false; + } + RCPPUTILS_SCOPE_EXIT(closedir(dir)); struct dirent * directory_entry; while ((directory_entry = readdir(dir)) != nullptr) { // Make sure to not call ".." or "." entries in directory (might delete everything) @@ -488,7 +493,6 @@ bool remove_all(const path & p) } } } - closedir(dir); // directory is empty now, call remove rcpputils::fs::remove(p); return !rcpputils::fs::exists(p);