Use unique_ptr<DIR> to safely release resources.
Test: builds, boots
Bug: 66995913
Change-Id: Ib580501fc979b63295b180250581dc7527de76b2
diff --git a/Loop.cpp b/Loop.cpp
index 3736d6a..335ca13 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -110,17 +110,16 @@
int Loop::destroyAll() {
ATRACE_NAME("Loop::destroyAll");
- DIR* dir;
- struct dirent* de;
-
std::string root = "/dev/block/";
- if (!(dir = opendir(root.c_str()))) {
+ auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
+ if (!dirp) {
PLOG(ERROR) << "Failed to opendir";
return -1;
}
// Poke through all devices looking for loops
- while ((de = readdir(dir))) {
+ struct dirent* de;
+ while ((de = readdir(dirp.get()))) {
auto test = std::string(de->d_name);
if (!android::base::StartsWith(test, "loop")) continue;
@@ -151,7 +150,6 @@
}
}
- closedir(dir);
return 0;
}