Skip to content

Commit ac4ee13

Browse files
authored
fix memory leak for remove_all(). (#201)
* fix memory leak for remove_all(). Signed-off-by: Tomoya Fujita <[email protected]>
1 parent 10939f9 commit ac4ee13

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/filesystem_helper.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#endif
6565

6666
#include "rcutils/env.h"
67+
#include "rcpputils/scope_exit.hpp"
6768
#include "rcpputils/split.hpp"
6869

6970
namespace rcpputils
@@ -474,6 +475,10 @@ bool remove_all(const path & p)
474475
return 0 == ret && false == file_options.fAnyOperationsAborted;
475476
#else
476477
DIR * dir = opendir(p.string().c_str());
478+
if (dir == nullptr) {
479+
return false;
480+
}
481+
RCPPUTILS_SCOPE_EXIT(closedir(dir));
477482
struct dirent * directory_entry;
478483
while ((directory_entry = readdir(dir)) != nullptr) {
479484
// Make sure to not call ".." or "." entries in directory (might delete everything)
@@ -488,7 +493,6 @@ bool remove_all(const path & p)
488493
}
489494
}
490495
}
491-
closedir(dir);
492496
// directory is empty now, call remove
493497
rcpputils::fs::remove(p);
494498
return !rcpputils::fs::exists(p);

0 commit comments

Comments
 (0)