From 5540b4406c2ca9c5a3654cabc1ab7e88afde58b5 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Sat, 24 Feb 2018 18:09:21 -0700 Subject: [PATCH] Use unique_ptr to safely release resources. Test: builds, boots Bug: 66995913 Change-Id: Ib580501fc979b63295b180250581dc7527de76b2 --- Loop.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Loop.cpp b/Loop.cpp index 3736d6a..335ca13 100644 --- a/Loop.cpp +++ b/Loop.cpp @@ -110,17 +110,16 @@ int Loop::destroyByDevice(const char *loopDevice) { 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(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 @@ int Loop::destroyAll() { } } - closedir(dir); return 0; }